authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-30 18:35:55+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-04-30 18:35:55+03:00
log7192ca14b7d5db62f3c38c49d36b07d6be86c6b9
tree146506f17259ac8fd95518cdc6852a09627e5477
parent155029b709ac3056dc2e6fb620fb4a99d4252c7e
parent61ba52b9e306f967ea38cd4494dfd3318d3266b8
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5216 from alexnask/windows_ansi_codes

Progress will now use ANSI escape codes on windows for terminals that support them

1 files changed, 7 insertions(+), 3 deletions(-)

lib/std/progress.zig+7-3
......@@ -13,6 +13,9 @@ pub const Progress = struct {
1313 /// not print on update()
1414 terminal: ?std.fs.File = undefined,
1515
16 /// Whether the terminal supports ANSI escape codes.
17 supports_ansi_escape_codes: bool = false,
18
1619 root: Node = undefined,
1720
1821 /// Keeps track of how much time has passed since the beginning.
......@@ -103,6 +106,7 @@ pub const Progress = struct {
103106 self.terminal = null;
104107 if (stderr.supportsAnsiEscapeCodes()) {
105108 self.terminal = stderr;
109 self.supports_ansi_escape_codes = true;
106110 } else if (std.builtin.os.tag == .windows and stderr.isTty()) {
107111 self.terminal = stderr;
108112 }
......@@ -138,10 +142,10 @@ pub const Progress = struct {
138142 // restore the cursor position by moving the cursor
139143 // `columns_written` cells to the left, then clear the rest of the
140144 // line
141 if (std.builtin.os.tag != .windows) {
145 if (self.supports_ansi_escape_codes) {
142146 end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[{}D", .{self.columns_written}) catch unreachable).len;
143147 end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[0K", .{}) catch unreachable).len;
144 } else {
148 } else if (std.builtin.os.tag == .windows) {
145149 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
146150 if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE)
147151 unreachable;
......@@ -174,7 +178,7 @@ pub const Progress = struct {
174178
175179 if (windows.kernel32.SetConsoleCursorPosition(file.handle, cursor_pos) != windows.TRUE)
176180 unreachable;
177 }
181 } else unreachable;
178182
179183 self.columns_written = 0;
180184 }