improve the window size handle method (#292)

1. the default window size should be 80x24 rather than 80x25 in VT100.
2. the ioctl return value result should be checked. Some operating systems don't support TIOCGWINSZ this command.
This commit is contained in:
Man, Jianting (Meco) 2022-01-01 13:24:37 -05:00 committed by GitHub
parent d549cdabb0
commit 071d2bc92b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,18 +19,22 @@
namespace ftxui { namespace ftxui {
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
static Dimensions fallback_size{140, 43}; // This dimension was chosen arbitrarily to be able to display:
#elif defined(_WIN32) // https://arthursonzogni.com/FTXUI/examples
// The Microsoft default "cmd" returns errors above. // This will have to be improved when someone has time to implement and need
static Dimensions fallback_size{80, 80}; // it.
#else static Dimensions fallback_size {140, 43};
static Dimensions fallback_size{80, 25};
#endif
Dimensions Terminal::Size() { Dimensions Terminal::Size() {
#if defined(__EMSCRIPTEN__)
return fallback_size; return fallback_size;
}
#elif defined(_WIN32) #elif defined(_WIN32)
// The terminal size in VT100 was 80x24. It is still used nowadays by
// default in many terminal emulator. That's a good choice for a fallback
// value.
static Dimensions fallback_size {80, 24};
Dimensions Terminal::Size() {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
@ -39,17 +43,26 @@ Dimensions Terminal::Size() {
} }
return fallback_size; return fallback_size;
}
#else #else
// The terminal size in VT100 was 80x24. It is still used nowadays by
// default in many terminal emulator. That's a good choice for a fallback
// value.
static Dimensions fallback_size {80, 24};
Dimensions Terminal::Size() {
winsize w{}; winsize w{};
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); const int status = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
if (w.ws_col == 0 || w.ws_row == 0) { // The ioctl return value result should be checked. Some operating systems
// don't support TIOCGWINSZ.
if (w.ws_col == 0 || w.ws_row == 0 || status < 0) {
return fallback_size; return fallback_size;
} }
return Dimensions{w.ws_col, w.ws_row}; return Dimensions{w.ws_col, w.ws_row};
#endif
} }
#endif
/// @brief Override terminal size in case auto-detection fails /// @brief Override terminal size in case auto-detection fails
/// @param fallbackSize Terminal dimensions to fallback to /// @param fallbackSize Terminal dimensions to fallback to
void Terminal::SetFallbackSize(const Dimensions& fallbackSize) { void Terminal::SetFallbackSize(const Dimensions& fallbackSize) {