Add API to set terminal fallback size (#230)

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 <jarek@jpelczar.com>
This commit is contained in:
Jarosław Pelczar 2021-10-13 13:44:30 +02:00 committed by GitHub
parent 5e199fcd85
commit 7298636e7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View File

@ -9,6 +9,7 @@ struct Dimensions {
namespace Terminal {
Dimensions Size();
void SetFallbackSize(const Dimensions& fallbackSize);
enum Color {
Palette1,

View File

@ -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) {