FTXUI  0.11.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__)
22static Dimensions fallback_size{140, 43};
23#elif defined(_WIN32)
24// The Microsoft default "cmd" returns errors above.
25static Dimensions fallback_size{80, 80};
26#else
27static Dimensions fallback_size{80, 25};
28#endif
29
31#if defined(__EMSCRIPTEN__)
32 return fallback_size;
33#elif defined(_WIN32)
34 CONSOLE_SCREEN_BUFFER_INFO csbi;
35
36 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
37 return Dimensions{csbi.srWindow.Right - csbi.srWindow.Left + 1,
38 csbi.srWindow.Bottom - csbi.srWindow.Top + 1};
39 }
40
41 return fallback_size;
42
43#else
44 winsize w{};
45 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
46 if (w.ws_col == 0 || w.ws_row == 0) {
47 return fallback_size;
48 }
49 return Dimensions{w.ws_col, w.ws_row};
50#endif
51}
52
53/// @brief Override terminal size in case auto-detection fails
54/// @param fallbackSize Terminal dimensions to fallback to
55void Terminal::SetFallbackSize(const Dimensions& fallbackSize) {
56 fallback_size = fallbackSize;
57}
58
59namespace {
60
61const char* Safe(const char* c) {
62 return c ? c : "";
63}
64
65bool Contains(const std::string& s, const char* key) {
66 return s.find(key) != std::string::npos;
67}
68
69static bool cached = false;
70Terminal::Color cached_supported_color;
71Terminal::Color ComputeColorSupport() {
72#if defined(__EMSCRIPTEN__)
74#endif
75
76 std::string COLORTERM = Safe(std::getenv("COLORTERM"));
77 if (Contains(COLORTERM, "24bit") || Contains(COLORTERM, "truecolor"))
79
80 std::string TERM = Safe(std::getenv("TERM"));
81 if (Contains(COLORTERM, "256") || Contains(TERM, "256"))
83
84#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
85 // Microsoft terminals do not properly declare themselve supporting true
86 // colors: https://github.com/microsoft/terminal/issues/1040
87 // As a fallback, assume microsoft terminal are the ones not setting those
88 // variables, and enable true colors.
89 if (TERM == "" && COLORTERM == "")
91#endif
92
94}
95
96} // namespace
97
99 if (!cached) {
100 cached = true;
101 cached_supported_color = ComputeColorSupport();
102 }
103 return cached_supported_color;
104}
105
106} // namespace ftxui
107
108// Copyright 2020 Arthur Sonzogni. All rights reserved.
109// Use of this source code is governed by the MIT license that can be found in
110// the LICENSE file.
Color ColorSupport()
Definition terminal.cpp:98
void SetFallbackSize(const Dimensions &fallbackSize)
Dimensions Size()
Definition terminal.cpp:30