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