From 429d402dc493a8d83e06e785f51b73c303eb598d Mon Sep 17 00:00:00 2001 From: Tyler Schneider Date: Mon, 27 Nov 2023 19:11:41 -0700 Subject: [PATCH] Fixed console rendering crash on wide consoles --- src/common/platform/posix/sdl/i_system.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/common/platform/posix/sdl/i_system.cpp b/src/common/platform/posix/sdl/i_system.cpp index 58a6a2f379d..d1f4654675f 100644 --- a/src/common/platform/posix/sdl/i_system.cpp +++ b/src/common/platform/posix/sdl/i_system.cpp @@ -218,20 +218,21 @@ void RedrawProgressBar(int CurPos, int MaxPos) CleanProgressBar(); struct winsize sizeOfWindow; ioctl(STDOUT_FILENO, TIOCGWINSZ, &sizeOfWindow); + int windowColClamped = std::min((int)sizeOfWindow.ws_col, 512); double progVal = std::clamp((double)CurPos / (double)MaxPos,0.0,1.0); - int curProgVal = std::clamp(int(sizeOfWindow.ws_col * progVal),0,(int)sizeOfWindow.ws_col); + int curProgVal = std::clamp(int(windowColClamped * progVal),0,windowColClamped); char progressBuffer[512]; memset(progressBuffer,'.',512); - progressBuffer[sizeOfWindow.ws_col - 1] = 0; + progressBuffer[windowColClamped - 1] = 0; int lengthOfStr = 0; while (curProgVal-- > 0) { progressBuffer[lengthOfStr++] = '='; - if (lengthOfStr >= sizeOfWindow.ws_col - 1) break; + if (lengthOfStr >= windowColClamped - 1) break; } - fprintf(stdout, "\0337\033[%d;%dH\033[2K[%s\033[%d;%dH]\0338", sizeOfWindow.ws_row, 0, progressBuffer, sizeOfWindow.ws_row, sizeOfWindow.ws_col); + fprintf(stdout, "\0337\033[%d;%dH\033[2K[%s\033[%d;%dH]\0338", sizeOfWindow.ws_row, 0, progressBuffer, sizeOfWindow.ws_row, windowColClamped); fflush(stdout); ProgressBarCurPos = CurPos; ProgressBarMaxPos = MaxPos;