authorgravatar for matt.chudleigh@gmail.comMatt Chudleigh <matt.chudleigh@gmail.com> 2021-06-18 11:49:03-07:00
committergravatar for matt.chudleigh@gmail.comMatt Chudleigh <matt.chudleigh@gmail.com> 2021-06-18 16:19:20-07:00
log24b1a0027fc663cbb648c265ba19067b108d0dcb
tree88ba541afcc067e87cc2ec76c6713838ebd563a9
parenta6c2e44ae7ab3009da8e079148cba7182d627fc4

Fix crash when compiling with cygwin/msys on windows


1 files changed, 15 insertions(+), 5 deletions(-)

lib/std/Progress.zig+15-5
......@@ -22,6 +22,10 @@ const Progress = @This();
2222/// not print on update()
2323terminal: ?std.fs.File = undefined,
2424
25/// Is this a windows API terminal (note: this is not the same as being run on windows
26/// because other terminals exist like MSYS/git-bash)
27is_windows_terminal: bool = false,
28
2529/// Whether the terminal supports ANSI escape codes.
2630supports_ansi_escape_codes: bool = false,
2731
......@@ -143,6 +147,7 @@ pub fn start(self: *Progress, name: []const u8, estimated_total_items: usize) !*
143147 self.terminal = stderr;
144148 self.supports_ansi_escape_codes = true;
145149 } else if (std.builtin.os.tag == .windows and stderr.isTty()) {
150 self.is_windows_terminal = true;
146151 self.terminal = stderr;
147152 } else if (std.builtin.os.tag != .windows) {
148153 // we are in a "dumb" terminal like in acme or writing to a file
......@@ -192,8 +197,9 @@ const DECRC = "\x1b8";
192197// supported by some terminals (eg. Terminal.app).
193198
194199fn refreshWithHeldLock(self: *Progress) void {
195 const is_dumb = !self.supports_ansi_escape_codes and !(std.builtin.os.tag == .windows);
200 const is_dumb = !self.supports_ansi_escape_codes and !self.is_windows_terminal;
196201 if (is_dumb and self.dont_print_on_dumb) return;
202
197203 const file = self.terminal orelse return;
198204
199205 var end: usize = 0;
......@@ -206,6 +212,8 @@ fn refreshWithHeldLock(self: *Progress) void {
206212 std.mem.copy(u8, self.output_buffer[end..], seq_before);
207213 end += seq_before.len;
208214 } else if (std.builtin.os.tag == .windows) winapi: {
215 std.debug.assert(self.is_windows_terminal);
216
209217 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
210218 if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE)
211219 unreachable;
......@@ -282,7 +290,7 @@ fn refreshWithHeldLock(self: *Progress) void {
282290 const seq_after = DECRC;
283291 std.mem.copy(u8, self.output_buffer[end..], seq_after);
284292 end += seq_after.len;
285 } else if (std.builtin.os.tag != .windows) {
293 } else if (!self.is_windows_terminal) {
286294 self.output_buffer[end] = '\n';
287295 end += 1;
288296 }
......@@ -293,8 +301,10 @@ fn refreshWithHeldLock(self: *Progress) void {
293301 };
294302
295303 if (std.builtin.os.tag == .windows) {
296 if (windows.kernel32.SetConsoleCursorPosition(file.handle, saved_cursor_pos) != windows.TRUE)
297 unreachable;
304 if (self.is_windows_terminal) {
305 const res = windows.kernel32.SetConsoleCursorPosition(file.handle, saved_cursor_pos);
306 std.debug.assert(res == windows.TRUE);
307 }
298308 }
299309
300310 self.prev_refresh_timestamp = self.timer.read();
......@@ -318,7 +328,7 @@ fn bufWrite(self: *Progress, end: *usize, comptime format: []const u8, args: any
318328 end.* = self.output_buffer.len;
319329 },
320330 }
321 const bytes_needed_for_esc_codes_at_end = if (std.builtin.os.tag == .windows) 0 else 11;
331 const bytes_needed_for_esc_codes_at_end: u8 = if (self.is_windows_terminal) 0 else 11;
322332 const max_end = self.output_buffer.len - bytes_needed_for_esc_codes_at_end;
323333 if (end.* > max_end) {
324334 const suffix = "... ";