authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-03-12 10:15:38+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-03-12 10:15:38+01:00
log3010bfb08af0b47d801d492e4f2e21a988e8399a
tree9150f4b43d0404b645b36748be7612ddd11a7df8
parent4fc2e92876d8aafd087a5f0bdb6ea7a54f195704

Fix Progress printing on Windows systems

The cursor must be restored after the line is printed, not before. Take into account the visible viewport to correctly compute the terminal size.

1 files changed, 14 insertions(+), 6 deletions(-)

lib/std/Progress.zig+14-6
...@@ -209,7 +209,13 @@ fn refreshWithHeldLock(self: *Progress) void {...@@ -209,7 +209,13 @@ fn refreshWithHeldLock(self: *Progress) void {
209 unreachable;209 unreachable;
210210
211 saved_cursor_pos = info.dwCursorPosition;211 saved_cursor_pos = info.dwCursorPosition;
212 const fill_chars = @intCast(windows.DWORD, info.dwSize.X * (info.dwSize.Y - info.dwCursorPosition.Y) - info.dwCursorPosition.X);212
213 const window_height = @intCast(windows.DWORD, info.srWindow.Bottom - info.srWindow.Top) + 1;
214 const window_width = @intCast(windows.DWORD, info.srWindow.Right - info.srWindow.Left) + 1;
215 // Number of terminal cells to clear, starting from the cursor position
216 // and ending at the window bottom right corner.
217 const fill_chars = window_width * (window_width - @intCast(windows.DWORD, info.dwCursorPosition.Y - info.srWindow.Top)) -
218 @intCast(windows.DWORD, info.dwCursorPosition.Y - info.srWindow.Top);
213219
214 var written: windows.DWORD = undefined;220 var written: windows.DWORD = undefined;
215 if (windows.kernel32.FillConsoleOutputAttribute(221 if (windows.kernel32.FillConsoleOutputAttribute(
...@@ -271,11 +277,7 @@ fn refreshWithHeldLock(self: *Progress) void {...@@ -271,11 +277,7 @@ fn refreshWithHeldLock(self: *Progress) void {
271 const seq_after = DECRC;277 const seq_after = DECRC;
272 std.mem.copy(u8, self.output_buffer[end..], seq_after);278 std.mem.copy(u8, self.output_buffer[end..], seq_after);
273 end += seq_after.len;279 end += seq_after.len;
274 } else if (std.builtin.os.tag == .windows) {280 } else if (std.builtin.os.tag != .windows) {
275 if (windows.kernel32.SetConsoleCursorPosition(file.handle, saved_cursor_pos) != windows.TRUE) {
276 unreachable;
277 }
278 } else {
279 self.output_buffer[end] = '\n';281 self.output_buffer[end] = '\n';
280 end += 1;282 end += 1;
281 }283 }
...@@ -284,6 +286,12 @@ fn refreshWithHeldLock(self: *Progress) void {...@@ -284,6 +286,12 @@ fn refreshWithHeldLock(self: *Progress) void {
284 // Stop trying to write to this file once it errors.286 // Stop trying to write to this file once it errors.
285 self.terminal = null;287 self.terminal = null;
286 };288 };
289
290 if (std.builtin.os.tag == .windows) {
291 if (windows.kernel32.SetConsoleCursorPosition(file.handle, saved_cursor_pos) != windows.TRUE)
292 unreachable;
293 }
294
287 self.prev_refresh_timestamp = self.timer.read();295 self.prev_refresh_timestamp = self.timer.read();
288}296}
289297