Merge pull request #13786 from ziglang/tty-detection

stage2: make --color on affect progress bar too
This commit is contained in:
Veikka Tuominen 2023-01-04 16:54:46 +02:00 committed by GitHub
commit 83fd45d820
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 30 deletions

View File

@ -760,17 +760,6 @@ fn writeEscaped(out: anytype, input: []const u8) !void {
//#define VT_BOLD "\x1b[0;1m" //#define VT_BOLD "\x1b[0;1m"
//#define VT_RESET "\x1b[0m" //#define VT_RESET "\x1b[0m"
const TermState = enum {
Start,
Escape,
LBracket,
Number,
AfterNumber,
Arg,
ArgNumber,
ExpectEnd,
};
test "term color" { test "term color" {
const input_bytes = "A\x1b[32;1mgreen\x1b[0mB"; const input_bytes = "A\x1b[32;1mgreen\x1b[0mB";
const result = try termColor(std.testing.allocator, input_bytes); const result = try termColor(std.testing.allocator, input_bytes);
@ -787,61 +776,80 @@ fn termColor(allocator: Allocator, input: []const u8) ![]u8 {
var first_number: usize = undefined; var first_number: usize = undefined;
var second_number: usize = undefined; var second_number: usize = undefined;
var i: usize = 0; var i: usize = 0;
var state = TermState.Start; var state: enum {
start,
escape,
lbracket,
number,
after_number,
arg,
arg_number,
expect_end,
} = .start;
var last_new_line: usize = 0;
var open_span_count: usize = 0; var open_span_count: usize = 0;
while (i < input.len) : (i += 1) { while (i < input.len) : (i += 1) {
const c = input[i]; const c = input[i];
switch (state) { switch (state) {
TermState.Start => switch (c) { .start => switch (c) {
'\x1b' => state = TermState.Escape, '\x1b' => state = .escape,
'\n' => {
try out.writeByte(c);
last_new_line = buf.items.len;
},
else => try out.writeByte(c), else => try out.writeByte(c),
}, },
TermState.Escape => switch (c) { .escape => switch (c) {
'[' => state = TermState.LBracket, '[' => state = .lbracket,
else => return error.UnsupportedEscape, else => return error.UnsupportedEscape,
}, },
TermState.LBracket => switch (c) { .lbracket => switch (c) {
'0'...'9' => { '0'...'9' => {
number_start_index = i; number_start_index = i;
state = TermState.Number; state = .number;
}, },
else => return error.UnsupportedEscape, else => return error.UnsupportedEscape,
}, },
TermState.Number => switch (c) { .number => switch (c) {
'0'...'9' => {}, '0'...'9' => {},
else => { else => {
first_number = std.fmt.parseInt(usize, input[number_start_index..i], 10) catch unreachable; first_number = std.fmt.parseInt(usize, input[number_start_index..i], 10) catch unreachable;
second_number = 0; second_number = 0;
state = TermState.AfterNumber; state = .after_number;
i -= 1; i -= 1;
}, },
}, },
TermState.AfterNumber => switch (c) { .after_number => switch (c) {
';' => state = TermState.Arg, ';' => state = .arg,
'D' => state = .start,
'K' => {
buf.items.len = last_new_line;
state = .start;
},
else => { else => {
state = TermState.ExpectEnd; state = .expect_end;
i -= 1; i -= 1;
}, },
}, },
TermState.Arg => switch (c) { .arg => switch (c) {
'0'...'9' => { '0'...'9' => {
number_start_index = i; number_start_index = i;
state = TermState.ArgNumber; state = .arg_number;
}, },
else => return error.UnsupportedEscape, else => return error.UnsupportedEscape,
}, },
TermState.ArgNumber => switch (c) { .arg_number => switch (c) {
'0'...'9' => {}, '0'...'9' => {},
else => { else => {
second_number = std.fmt.parseInt(usize, input[number_start_index..i], 10) catch unreachable; second_number = std.fmt.parseInt(usize, input[number_start_index..i], 10) catch unreachable;
state = TermState.ExpectEnd; state = .expect_end;
i -= 1; i -= 1;
}, },
}, },
TermState.ExpectEnd => switch (c) { .expect_end => switch (c) {
'm' => { 'm' => {
state = TermState.Start; state = .start;
while (open_span_count != 0) : (open_span_count -= 1) { while (open_span_count != 0) : (open_span_count -= 1) {
try out.writeAll("</span>"); try out.writeAll("</span>");
} }

View File

@ -2358,7 +2358,16 @@ pub fn update(comp: *Compilation) !void {
var progress: std.Progress = .{ .dont_print_on_dumb = true }; var progress: std.Progress = .{ .dont_print_on_dumb = true };
const main_progress_node = progress.start("", 0); const main_progress_node = progress.start("", 0);
defer main_progress_node.end(); defer main_progress_node.end();
if (comp.color == .off) progress.terminal = null; switch (comp.color) {
.off => {
progress.terminal = null;
},
.on => {
progress.terminal = std.io.getStdErr();
progress.supports_ansi_escape_codes = true;
},
.auto => {},
}
try comp.performAllTheWork(main_progress_node); try comp.performAllTheWork(main_progress_node);