7#include <initializer_list>
13#define WIN32_LEAN_AND_MEAN
29bool g_color_support_detected =
false;
30Terminal::Quirks g_quirks = [] {
31 Terminal::Quirks quirks;
33 quirks.block_characters =
false;
34 quirks.cursor_hiding =
false;
35 quirks.component_ascii =
true;
40Dimensions& FallbackSize() {
41#if defined(__EMSCRIPTEN__)
46 constexpr int fallback_width = 140;
47 constexpr int fallback_height = 43;
52 constexpr int fallback_width = 80;
53 constexpr int fallback_height = 24;
55 static Dimensions g_fallback_size{
59 return g_fallback_size;
62const char* Safe(
const char* c) {
63 return (c !=
nullptr) ? c :
"";
66bool Contains(std::string_view s, std::string_view key) {
70 const auto it = std::search(
71 s.begin(), s.end(), key.begin(), key.end(), [](
char a,
char b) {
72 return std::tolower(static_cast<unsigned char>(a)) ==
73 std::tolower(static_cast<unsigned char>(b));
78bool ContainsAny(std::string_view s,
79 std::initializer_list<std::string_view> keys) {
80 for (
const std::string_view key : keys) {
81 if (Contains(s, key)) {
89#if defined(__EMSCRIPTEN__)
93 std::string_view COLORTERM = Safe(std::getenv(
"COLORTERM"));
94 if (ContainsAny(COLORTERM, {
"24bit",
"truecolor"})) {
98 std::string_view TERM_PROGRAM = Safe(std::getenv(
"TERM_PROGRAM"));
99 if (ContainsAny(TERM_PROGRAM, {
"iterm",
"apple_terminal",
"vscode",
"warp",
100 "ghostty",
"wezterm"})) {
104 std::string_view TERM = Safe(std::getenv(
"TERM"));
105 if (ContainsAny(TERM,
106 {
"direct",
"truecolor",
"kitty",
"alacritty",
"foot"})) {
110 if (Contains(TERM,
"xterm") && !ContainsAny(TERM, {
"rxvt",
"urxvt"})) {
114 if (ContainsAny(COLORTERM, {
"256"}) ||
115 ContainsAny(TERM, {
"256",
"xterm",
"screen",
"tmux"}) ||
116 Contains(TERM_PROGRAM,
"iterm")) {
131#if defined(__EMSCRIPTEN__)
136 return FallbackSize();
138 CONSOLE_SCREEN_BUFFER_INFO csbi;
140 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
141 return Dimensions{csbi.srWindow.Right - csbi.srWindow.Left + 1,
142 csbi.srWindow.Bottom - csbi.srWindow.Top + 1};
145 return FallbackSize();
148 const int status = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
151 if (w.ws_col == 0 || w.ws_row == 0 || status < 0) {
152 return FallbackSize();
161 FallbackSize() = fallbackSize;
167 if (!g_color_support_detected) {
168 g_quirks.color_support = ComputeColorSupport();
169 g_color_support_detected =
true;
171 return g_quirks.color_support;
177 g_quirks.color_support =
color;
178 g_color_support_detected =
true;
184 if (!g_color_support_detected) {
185 g_quirks.color_support = ComputeColorSupport();
186 g_color_support_detected =
true;
195 g_color_support_detected =
true;
void SetColorSupport(Color color)
Override terminal color support in case auto-detection fails.
Decorator color(Color)
Decorate using a foreground color.
Color
Color is an enumeration that represents the color support of the terminal.
Quirks GetQuirks()
Get the terminal quirks.
Dimensions Size()
Get the terminal size.
Color ColorSupport()
Get the color support of the terminal.
void SetQuirks(const Quirks &quirks)
Override terminal quirks.
Dimensions is a structure that represents the size of the terminal.
Quirks is a structure that represents various terminal-specific behaviors that may require fallbacks.
The FTXUI ftxui::Terminal:: namespace.
void SetFallbackSize(const Dimensions &fallbackSize)
Override terminal size in case auto-detection fails.
The FTXUI ftxui:: namespace.