Skip to content

Commit

Permalink
Add API to set terminal fallback size (#230)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jarekpelczar authored Oct 13, 2021
1 parent 5e199fc commit 7298636
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/ftxui/screen/terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct Dimensions {

namespace Terminal {
Dimensions Size();
void SetFallbackSize(const Dimensions& fallbackSize);

enum Color {
Palette1,
Expand Down
22 changes: 18 additions & 4 deletions src/ftxui/screen/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down

0 comments on commit 7298636

Please sign in to comment.