From 53f0c44d6c4cacd0444c9f0d72e97eb2a76251f6 Mon Sep 17 00:00:00 2001 From: PoweredByPie Date: Sat, 18 May 2024 16:05:14 -0700 Subject: [PATCH 1/2] Implement `updateWindowSize` for Windows --- src/libutil/terminal.cc | 15 +++++++++++++-- src/libutil/terminal.hh | 4 ---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/libutil/terminal.cc b/src/libutil/terminal.cc index 4dc280f8c..5d5ff7dcb 100644 --- a/src/libutil/terminal.cc +++ b/src/libutil/terminal.cc @@ -4,6 +4,8 @@ #if _WIN32 # include +# define WIN32_LEAN_AND_MEAN +# include # define isatty _isatty #else # include @@ -97,17 +99,26 @@ std::string filterANSIEscapes(std::string_view s, bool filterAll, unsigned int w static Sync> windowSize{{0, 0}}; -#ifndef _WIN32 void updateWindowSize() { + #ifndef _WIN32 struct winsize ws; if (ioctl(2, TIOCGWINSZ, &ws) == 0) { auto windowSize_(windowSize.lock()); windowSize_->first = ws.ws_row; windowSize_->second = ws.ws_col; } + #else + CONSOLE_SCREEN_BUFFER_INFO info; + // From https://stackoverflow.com/a/12642749 + if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info) != 0) { + auto windowSize_(windowSize.lock()); + // From https://github.com/libuv/libuv/blob/v1.48.0/src/win/tty.c#L1130 + windowSize_->first = info.srWindow.Bottom - info.srWindow.Top + 1; + windowSize_->second = info.dwSize.X; + } + #endif } -#endif std::pair getWindowSize() diff --git a/src/libutil/terminal.hh b/src/libutil/terminal.hh index 628833283..9d8d0c743 100644 --- a/src/libutil/terminal.hh +++ b/src/libutil/terminal.hh @@ -21,16 +21,12 @@ std::string filterANSIEscapes(std::string_view s, bool filterAll = false, unsigned int width = std::numeric_limits::max()); -#ifndef _WIN32 - /** * Recalculate the window size, updating a global variable. Used in the * `SIGWINCH` signal handler. */ void updateWindowSize(); -#endif - /** * @return the number of rows and columns of the terminal. * From d48bbda2e702879b2296b897ec805167af178ebc Mon Sep 17 00:00:00 2001 From: John Ericson Date: Mon, 20 May 2024 08:34:49 -0400 Subject: [PATCH 2/2] Update the `updateWindowSize` documentation --- src/libutil/terminal.hh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libutil/terminal.hh b/src/libutil/terminal.hh index 9d8d0c743..902e75945 100644 --- a/src/libutil/terminal.hh +++ b/src/libutil/terminal.hh @@ -22,8 +22,9 @@ std::string filterANSIEscapes(std::string_view s, unsigned int width = std::numeric_limits::max()); /** - * Recalculate the window size, updating a global variable. Used in the - * `SIGWINCH` signal handler. + * Recalculate the window size, updating a global variable. + * + * Used in the `SIGWINCH` signal handler on Unix, for example. */ void updateWindowSize();