From b0724a350f07c5e2e8fab572951ffaaa92860b2c Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Fri, 12 Mar 2021 17:14:50 +0100 Subject: [PATCH] Handle some weird edge cases of Win32 API Sometimes the viewport srWindow may report an invalid rectangle where the top row is below the bottom one. --- lib/std/Progress.zig | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/std/Progress.zig b/lib/std/Progress.zig index b1f8dff1c1..3f462f5c08 100644 --- a/lib/std/Progress.zig +++ b/lib/std/Progress.zig @@ -210,13 +210,15 @@ fn refreshWithHeldLock(self: *Progress) void { saved_cursor_pos = info.dwCursorPosition; - const window_height = @intCast(windows.DWORD, info.srWindow.Bottom - info.srWindow.Top) + 1; - const window_width = @intCast(windows.DWORD, info.srWindow.Right - info.srWindow.Left) + 1; + const window_height = @intCast(windows.DWORD, info.srWindow.Bottom - info.srWindow.Top + 1); + const window_width = @intCast(windows.DWORD, info.srWindow.Right - info.srWindow.Left + 1); // Number of terminal cells to clear, starting from the cursor position // and ending at the window bottom right corner. - const fill_chars = window_width * (window_height - - @intCast(windows.DWORD, info.dwCursorPosition.Y - info.srWindow.Top)) - - @intCast(windows.DWORD, info.dwCursorPosition.X - info.srWindow.Left); + const fill_chars = if (window_width == 0 or window_height == 0) 0 else chars: { + break :chars window_width * (window_height - + @intCast(windows.DWORD, info.dwCursorPosition.Y - info.srWindow.Top)) - + @intCast(windows.DWORD, info.dwCursorPosition.X - info.srWindow.Left); + }; var written: windows.DWORD = undefined; if (windows.kernel32.FillConsoleOutputAttribute(