From 273d2de099491278e7248b791f36395ab1a2e11a Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Wed, 29 Apr 2020 19:49:02 +0300 Subject: [PATCH 1/2] Progress will now use ANSI escape codes on windows for terminals that support it --- lib/std/progress.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/std/progress.zig b/lib/std/progress.zig index bde67f1a23..a66a8e097f 100644 --- a/lib/std/progress.zig +++ b/lib/std/progress.zig @@ -13,6 +13,9 @@ pub const Progress = struct { /// not print on update() terminal: ?std.fs.File = undefined, + /// Whether the terminal supports ANSI escape codes. + supports_ansi_escape_codes: bool = false, + root: Node = undefined, /// Keeps track of how much time has passed since the beginning. @@ -103,6 +106,7 @@ pub const Progress = struct { self.terminal = null; if (stderr.supportsAnsiEscapeCodes()) { self.terminal = stderr; + self.supports_ansi_escape_codes = true; } else if (std.builtin.os.tag == .windows and stderr.isTty()) { self.terminal = stderr; } @@ -138,10 +142,10 @@ pub const Progress = struct { // restore the cursor position by moving the cursor // `columns_written` cells to the left, then clear the rest of the // line - if (std.builtin.os.tag != .windows) { + if (self.supports_ansi_escape_codes) { end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[{}D", .{self.columns_written}) catch unreachable).len; end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[0K", .{}) catch unreachable).len; - } else { + } else if (std.builtin.os.tag == .windows) { var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) unreachable; From 61ba52b9e306f967ea38cd4494dfd3318d3266b8 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Wed, 29 Apr 2020 20:07:23 +0300 Subject: [PATCH 2/2] Add unreachable branch --- lib/std/progress.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/progress.zig b/lib/std/progress.zig index a66a8e097f..def438da79 100644 --- a/lib/std/progress.zig +++ b/lib/std/progress.zig @@ -178,7 +178,7 @@ pub const Progress = struct { if (windows.kernel32.SetConsoleCursorPosition(file.handle, cursor_pos) != windows.TRUE) unreachable; - } + } else unreachable; self.columns_written = 0; }