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