mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-29 16:39:34 +08:00
Add clang-tidy. (#368)
This commit is contained in:
@@ -18,89 +18,57 @@
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
// This dimension was chosen arbitrarily to be able to display:
|
||||
// https://arthursonzogni.com/FTXUI/examples
|
||||
// This will have to be improved when someone has time to implement and need
|
||||
// it.
|
||||
static Dimensions fallback_size{140, 43};
|
||||
Dimensions Terminal::Size() {
|
||||
return fallback_size;
|
||||
}
|
||||
|
||||
#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;
|
||||
|
||||
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
|
||||
return Dimensions{csbi.srWindow.Right - csbi.srWindow.Left + 1,
|
||||
csbi.srWindow.Bottom - csbi.srWindow.Top + 1};
|
||||
}
|
||||
|
||||
return fallback_size;
|
||||
}
|
||||
|
||||
#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{};
|
||||
const int status = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||
// 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 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 {
|
||||
Dimensions& FallbackSize() {
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
// This dimension was chosen arbitrarily to be able to display:
|
||||
// https://arthursonzogni.com/FTXUI/examples
|
||||
// This will have to be improved when someone has time to implement and need
|
||||
// it.
|
||||
constexpr int fallback_width = 140;
|
||||
constexpr int fallback_height = 43;
|
||||
#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.
|
||||
constexpr int fallback_width = 80;
|
||||
constexpr int fallback_height = 24;
|
||||
#endif
|
||||
static Dimensions g_fallback_size{fallback_width, fallback_height};
|
||||
return g_fallback_size;
|
||||
}
|
||||
|
||||
const char* Safe(const char* c) {
|
||||
return c ? c : "";
|
||||
return (c != nullptr) ? c : "";
|
||||
}
|
||||
|
||||
bool Contains(const std::string& s, const char* key) {
|
||||
return s.find(key) != std::string::npos;
|
||||
}
|
||||
|
||||
static bool cached = false;
|
||||
Terminal::Color cached_supported_color;
|
||||
Terminal::Color ComputeColorSupport() {
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
return Terminal::Color::TrueColor;
|
||||
#endif
|
||||
|
||||
std::string COLORTERM = Safe(std::getenv("COLORTERM"));
|
||||
if (Contains(COLORTERM, "24bit") || Contains(COLORTERM, "truecolor"))
|
||||
std::string COLORTERM = Safe(std::getenv("COLORTERM")); // NOLINT
|
||||
if (Contains(COLORTERM, "24bit") || Contains(COLORTERM, "truecolor")) {
|
||||
return Terminal::Color::TrueColor;
|
||||
}
|
||||
|
||||
std::string TERM = Safe(std::getenv("TERM"));
|
||||
if (Contains(COLORTERM, "256") || Contains(TERM, "256"))
|
||||
std::string TERM = Safe(std::getenv("TERM")); // NOLINT
|
||||
if (Contains(COLORTERM, "256") || Contains(TERM, "256")) {
|
||||
return Terminal::Color::Palette256;
|
||||
}
|
||||
|
||||
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
|
||||
// Microsoft terminals do not properly declare themselve supporting true
|
||||
// colors: https://github.com/microsoft/terminal/issues/1040
|
||||
// As a fallback, assume microsoft terminal are the ones not setting those
|
||||
// variables, and enable true colors.
|
||||
if (TERM == "" && COLORTERM == "")
|
||||
if (TERM == "" && COLORTERM == "") {
|
||||
return Terminal::Color::TrueColor;
|
||||
}
|
||||
#endif
|
||||
|
||||
return Terminal::Color::Palette16;
|
||||
@@ -108,7 +76,43 @@ Terminal::Color ComputeColorSupport() {
|
||||
|
||||
} // namespace
|
||||
|
||||
Dimensions Terminal::Size() {
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
// This dimension was chosen arbitrarily to be able to display:
|
||||
// https://arthursonzogni.com/FTXUI/examples
|
||||
// This will have to be improved when someone has time to implement and need
|
||||
// it.
|
||||
return FallbackSize();
|
||||
#elif defined(_WIN32)
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
|
||||
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
|
||||
return Dimensions{csbi.srWindow.Right - csbi.srWindow.Left + 1,
|
||||
csbi.srWindow.Bottom - csbi.srWindow.Top + 1};
|
||||
}
|
||||
|
||||
return FallbackSize();
|
||||
#else
|
||||
winsize w{};
|
||||
const int status = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); // NOLINT
|
||||
// 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 FallbackSize();
|
||||
}
|
||||
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) {
|
||||
FallbackSize() = fallbackSize;
|
||||
}
|
||||
|
||||
Terminal::Color Terminal::ColorSupport() {
|
||||
static bool cached = false;
|
||||
static Terminal::Color cached_supported_color;
|
||||
if (!cached) {
|
||||
cached = true;
|
||||
cached_supported_color = ComputeColorSupport();
|
||||
|
Reference in New Issue
Block a user