FTXUI  3.0.0
C++ functional terminal UI.
terminal.cpp
Go to the documentation of this file.
1 #include <cstdlib> // for getenv
2 #include <string> // for string, allocator
3 
5 
6 #if defined(_WIN32)
7 #define WIN32_LEAN_AND_MEAN
8 
9 #ifndef NOMINMAX
10 #define NOMINMAX
11 #endif
12 
13 #include <Windows.h>
14 #else
15 #include <sys/ioctl.h> // for winsize, ioctl, TIOCGWINSZ
16 #include <unistd.h> // for STDOUT_FILENO
17 #endif
18 
19 namespace ftxui {
20 
21 namespace {
22 Dimensions& FallbackSize() {
23 #if defined(__EMSCRIPTEN__)
24  // This dimension was chosen arbitrarily to be able to display:
25  // https://arthursonzogni.com/FTXUI/examples
26  // This will have to be improved when someone has time to implement and need
27  // it.
28  constexpr int fallback_width = 140;
29  constexpr int fallback_height = 43;
30 #else
31  // The terminal size in VT100 was 80x24. It is still used nowadays by
32  // default in many terminal emulator. That's a good choice for a fallback
33  // value.
34  constexpr int fallback_width = 80;
35  constexpr int fallback_height = 24;
36 #endif
37  static Dimensions g_fallback_size{fallback_width, fallback_height};
38  return g_fallback_size;
39 }
40 
41 const char* Safe(const char* c) {
42  return (c != nullptr) ? c : "";
43 }
44 
45 bool Contains(const std::string& s, const char* key) {
46  return s.find(key) != std::string::npos;
47 }
48 
49 Terminal::Color ComputeColorSupport() {
50 #if defined(__EMSCRIPTEN__)
52 #endif
53 
54  std::string COLORTERM = Safe(std::getenv("COLORTERM")); // NOLINT
55  if (Contains(COLORTERM, "24bit") || Contains(COLORTERM, "truecolor")) {
57  }
58 
59  std::string TERM = Safe(std::getenv("TERM")); // NOLINT
60  if (Contains(COLORTERM, "256") || Contains(TERM, "256")) {
62  }
63 
64 #if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
65  // Microsoft terminals do not properly declare themselve supporting true
66  // colors: https://github.com/microsoft/terminal/issues/1040
67  // As a fallback, assume microsoft terminal are the ones not setting those
68  // variables, and enable true colors.
69  if (TERM == "" && COLORTERM == "") {
71  }
72 #endif
73 
75 }
76 
77 } // namespace
78 
80 #if defined(__EMSCRIPTEN__)
81  // This dimension was chosen arbitrarily to be able to display:
82  // https://arthursonzogni.com/FTXUI/examples
83  // This will have to be improved when someone has time to implement and need
84  // it.
85  return FallbackSize();
86 #elif defined(_WIN32)
87  CONSOLE_SCREEN_BUFFER_INFO csbi;
88 
89  if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
90  return Dimensions{csbi.srWindow.Right - csbi.srWindow.Left + 1,
91  csbi.srWindow.Bottom - csbi.srWindow.Top + 1};
92  }
93 
94  return FallbackSize();
95 #else
96  winsize w{};
97  const int status = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); // NOLINT
98  // The ioctl return value result should be checked. Some operating systems
99  // don't support TIOCGWINSZ.
100  if (w.ws_col == 0 || w.ws_row == 0 || status < 0) {
101  return FallbackSize();
102  }
103  return Dimensions{w.ws_col, w.ws_row};
104 #endif
105 }
106 
107 /// @brief Override terminal size in case auto-detection fails
108 /// @param fallbackSize Terminal dimensions to fallback to
109 void Terminal::SetFallbackSize(const Dimensions& fallbackSize) {
110  FallbackSize() = fallbackSize;
111 }
112 
114  static bool cached = false;
115  static Terminal::Color cached_supported_color;
116  if (!cached) {
117  cached = true;
118  cached_supported_color = ComputeColorSupport();
119  }
120  return cached_supported_color;
121 }
122 
123 } // namespace ftxui
124 
125 // Copyright 2020 Arthur Sonzogni. All rights reserved.
126 // Use of this source code is governed by the MIT license that can be found in
127 // the LICENSE file.
Color ColorSupport()
Definition: terminal.cpp:113
void SetFallbackSize(const Dimensions &fallbackSize)
Override terminal size in case auto-detection fails.
Definition: terminal.cpp:109
Dimensions Size()
Definition: terminal.cpp:79