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
signature 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 {...@@ -13,6 +13,9 @@ pub const Progress = struct {
13 /// not print on update()13 /// not print on update()
14 terminal: ?std.fs.File = undefined,14 terminal: ?std.fs.File = undefined,
1515
16 /// Whether the terminal supports ANSI escape codes.
17 supports_ansi_escape_codes: bool = false,
18
16 root: Node = undefined,19 root: Node = undefined,
1720
18 /// Keeps track of how much time has passed since the beginning.21 /// Keeps track of how much time has passed since the beginning.
...@@ -103,6 +106,7 @@ pub const Progress = struct {...@@ -103,6 +106,7 @@ pub const Progress = struct {
103 self.terminal = null;106 self.terminal = null;
104 if (stderr.supportsAnsiEscapeCodes()) {107 if (stderr.supportsAnsiEscapeCodes()) {
105 self.terminal = stderr;108 self.terminal = stderr;
109 self.supports_ansi_escape_codes = true;
106 } else if (std.builtin.os.tag == .windows and stderr.isTty()) {110 } else if (std.builtin.os.tag == .windows and stderr.isTty()) {
107 self.terminal = stderr;111 self.terminal = stderr;
108 }112 }
...@@ -138,10 +142,10 @@ pub const Progress = struct {...@@ -138,10 +142,10 @@ pub const Progress = struct {
138 // restore the cursor position by moving the cursor142 // restore the cursor position by moving the cursor
139 // `columns_written` cells to the left, then clear the rest of the143 // `columns_written` cells to the left, then clear the rest of the
140 // line144 // line
141 if (std.builtin.os.tag != .windows) {145 if (self.supports_ansi_escape_codes) {
142 end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[{}D", .{self.columns_written}) catch unreachable).len;146 end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[{}D", .{self.columns_written}) catch unreachable).len;
143 end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[0K", .{}) catch unreachable).len;147 end += (std.fmt.bufPrint(self.output_buffer[end..], "\x1b[0K", .{}) catch unreachable).len;
144 } else {148 } else if (std.builtin.os.tag == .windows) {
145 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;149 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
146 if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE)150 if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE)
147 unreachable;151 unreachable;
...@@ -174,7 +178,7 @@ pub const Progress = struct {...@@ -174,7 +178,7 @@ pub const Progress = struct {
174178
175 if (windows.kernel32.SetConsoleCursorPosition(file.handle, cursor_pos) != windows.TRUE)179 if (windows.kernel32.SetConsoleCursorPosition(file.handle, cursor_pos) != windows.TRUE)
176 unreachable;180 unreachable;
177 }181 } else unreachable;
178182
179 self.columns_written = 0;183 self.columns_written = 0;
180 }184 }