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