Skip to content

Commit c108e25

Browse files
committed
fix: ansi output resetting on newlines
We reset the terminal colors to write the line header. The problem is we were processing the whole file, which has spans covering multiple lines. Now just write directly to stdout and put the pen backdown. fixes: #2
1 parent 1acc4a9 commit c108e25

1 file changed

Lines changed: 35 additions & 25 deletions

File tree

packages/lcov_format/lib/src/ansi_format.dart

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'dart:convert';
21
import 'dart:io';
32

43
import 'package:ansicolor/ansicolor.dart';
@@ -27,44 +26,55 @@ ansiFormat(
2726
}
2827

2928
printFile(TextSpan fileSpan, LcovRecord node, String fullPath) {
30-
final sb = StringBuffer();
31-
SpanVisitor(
32-
(span, depth) {
33-
if (span.text == null) return;
34-
var text = span.text!;
35-
36-
final argb = span.style?.foreground.argb ?? 0xFFFFFFFF;
37-
final pen = ansiScopes[argb] ??= AnsiPen()
38-
..rgb(
39-
r: ((argb & 0xFF0000) >> 16) / 255,
40-
g: ((argb & 0xFF00) >> 8) / 255,
41-
b: (argb & 0xFF) / 255);
42-
43-
sb.write(pen(text));
44-
},
45-
).visit(fileSpan);
46-
4729
final zero = AnsiPen()
4830
..red(bg: true)
49-
..white();
31+
..black();
5032
final cover = AnsiPen()
5133
..green(bg: true)
52-
..white();
34+
..black();
5335
stdout.writeln('');
5436
stdout.writeln(
5537
'────┤ $fullPath [${node.linesHit} / ${node.linesFound}] = ${node.percent.toStringAsFixed(2)}% ├────');
38+
5639
int lineNumber = 1;
57-
for (final line in LineSplitter.split('$sb')) {
40+
writeLineHeader() {
5841
var hits = node.lines[lineNumber];
5942
if (hits == null) {
60-
stdout.writeln('$ansiDefault${' ' * 8} | $line');
43+
stdout.write('$ansiDefault${' ' * 8} | ');
6144
} else if (hits == 0) {
62-
stdout.writeln('$ansiDefault${zero('$hits'.padLeft(8, ' '))} | $line');
45+
stdout.write('$ansiDefault${zero('$hits'.padLeft(8, ' '))} | ');
6346
} else {
64-
stdout.writeln('$ansiDefault${cover('$hits'.padLeft(8, ' '))} | $line');
47+
stdout.write('$ansiDefault${cover('$hits'.padLeft(8, ' '))} | ');
6548
}
66-
lineNumber++;
6749
}
50+
51+
writeLineHeader();
52+
SpanVisitor(
53+
(span, depth) {
54+
if (span.text == null) {
55+
return;
56+
}
57+
var text = span.text!;
58+
59+
final argb = span.style?.foreground.argb ?? 0xFFFFFFFF;
60+
final pen = ansiScopes[argb] ??= AnsiPen()
61+
..rgb(
62+
r: ((argb & 0xFF0000) >> 16) / 255,
63+
g: ((argb & 0xFF00) >> 8) / 255,
64+
b: (argb & 0xFF) / 255);
65+
66+
stdout.write(pen.down);
67+
for (int i = 0; i < text.length; i++) {
68+
final char = text[i];
69+
stdout.write(char);
70+
if (char == '\n') {
71+
lineNumber++;
72+
writeLineHeader();
73+
stdout.write(pen.down);
74+
}
75+
}
76+
},
77+
).visit(fileSpan);
6878
}
6979

7080
for (var child in node.children.values) {

0 commit comments

Comments
 (0)