From 7298636e7c66ccf34f149512f2cce5c3d728ab9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Pelczar?= Date: Wed, 13 Oct 2021 13:44:30 +0200 Subject: [PATCH] Add API to set terminal fallback size (#230) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In case of embedded systems, the terminal size may not always be detectable (e.g. in case of serial output). Allow application to set up the default size in case autodetection fails. On platform such as Emscripten, there is only "fallback" size. Signed-off-by: Jarosław Pelczar --- include/ftxui/screen/terminal.hpp | 1 + src/ftxui/screen/terminal.cpp | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/ftxui/screen/terminal.hpp b/include/ftxui/screen/terminal.hpp index 9b4364c..9a6b6e6 100644 --- a/include/ftxui/screen/terminal.hpp +++ b/include/ftxui/screen/terminal.hpp @@ -9,6 +9,7 @@ struct Dimensions { namespace Terminal { Dimensions Size(); +void SetFallbackSize(const Dimensions& fallbackSize); enum Color { Palette1, diff --git a/src/ftxui/screen/terminal.cpp b/src/ftxui/screen/terminal.cpp index 4249784..86410d3 100644 --- a/src/ftxui/screen/terminal.cpp +++ b/src/ftxui/screen/terminal.cpp @@ -18,9 +18,18 @@ namespace ftxui { +#if defined(__EMSCRIPTEN__) +static Dimensions fallback_size{140, 43}; +#elif defined(_WIN32) +// The Microsoft default "cmd" returns errors above. +static Dimensions fallback_size{80, 80}; +#else +static Dimensions fallback_size{80, 25}; +#endif + Dimensions Terminal::Size() { #if defined(__EMSCRIPTEN__) - return Dimensions{140, 43}; + return fallback_size; #elif defined(_WIN32) CONSOLE_SCREEN_BUFFER_INFO csbi; @@ -29,19 +38,24 @@ Dimensions Terminal::Size() { csbi.srWindow.Bottom - csbi.srWindow.Top + 1}; } - // The Microsoft default "cmd" returns errors above. - return Dimensions{80, 80}; + return fallback_size; #else winsize w{}; ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); if (w.ws_col == 0 || w.ws_row == 0) { - return Dimensions{80, 25}; + return fallback_size; } return Dimensions{w.ws_col, w.ws_row}; #endif } +/// @brief Override terminal size in case auto-detection fails +/// @param fallbackSize Terminal dimensions to fallback to +void Terminal::SetFallbackSize(const Dimensions& fallbackSize) { + fallback_size = fallbackSize; +} + namespace { const char* Safe(const char* c) {