FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
terminal.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <cstdlib> // for getenv
5#include <string> // for string, allocator
6
8
9#if defined(_WIN32)
10#define WIN32_LEAN_AND_MEAN
11
12#ifndef NOMINMAX
13#define NOMINMAX
14#endif
15
16#include <windows.h>
17#else
18#include <sys/ioctl.h> // for winsize, ioctl, TIOCGWINSZ
19#include <unistd.h> // for STDOUT_FILENO
20#endif
21
22namespace ftxui {
23
24namespace {
25
26bool g_cached = false; // NOLINT
27Terminal::Color g_cached_supported_color; // NOLINT
28
29Dimensions& FallbackSize() {
30#if defined(__EMSCRIPTEN__)
31 // 選擇此尺寸是為了能夠顯示:
32 // https://arthursonzogni.com/FTXUI/examples
33 // 當有人有時間實作並需要時,這將會改進。
34 constexpr int fallback_width = 140;
35 constexpr int fallback_height = 43;
36#else
37 // VT100 中的終端機大小為 80x24。它至今仍被許多終端機模擬器預設使用。這是一個很好的後備值。
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 // 微軟終端機沒有正確宣告自己支援真彩色:https://github.com/microsoft/terminal/issues/1040
73 // 作為後備方案,假設微軟終端機是那些沒有設定這些變量的終端機,並啟用真彩色。
74 if (TERM.empty() && COLORTERM.empty()) {
76 }
77#endif
78
80}
81
82} // namespace
83
84namespace Terminal {
85
86/// @brief 獲取終端機大小。
87/// @return 終端機大小。
88/// @ingroup screen
90#if defined(__EMSCRIPTEN__)
91 // This dimension was chosen arbitrarily to be able to display:
92 // https://arthursonzogni.com/FTXUI/examples
93 // This will have to be improved when someone has time to implement and need
94 // it.
95 return FallbackSize();
96#elif defined(_WIN32)
97 CONSOLE_SCREEN_BUFFER_INFO csbi;
98
99 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
100 return Dimensions{csbi.srWindow.Right - csbi.srWindow.Left + 1,
101 csbi.srWindow.Bottom - csbi.srWindow.Top + 1};
102 }
103
104 return FallbackSize();
105#else
106 winsize w{};
107 const int status = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); // NOLINT
108 // The ioctl return value result should be checked. Some operating systems
109 // don't support TIOCGWINSZ.
110 if (w.ws_col == 0 || w.ws_row == 0 || status < 0) {
111 return FallbackSize();
112 }
113 return Dimensions{w.ws_col, w.ws_row};
114#endif
115}
116
117/// @brief 在自動偵測失敗時覆寫終端機大小
118/// @param fallbackSize 要回退到的終端機尺寸
119void SetFallbackSize(const Dimensions& fallbackSize) {
120 FallbackSize() = fallbackSize;
121}
122
123/// @brief 獲取終端機的顏色支援。
124/// @ingroup screen
126 if (!g_cached) {
127 g_cached = true;
128 g_cached_supported_color = ComputeColorSupport();
129 }
130 return g_cached_supported_color;
131}
132
133/// @brief 在自動偵測失敗時覆寫終端機顏色支援
134/// @ingroup dom
136 g_cached = true;
137 g_cached_supported_color = color;
138}
139
140} // namespace Terminal
141} // namespace ftxui
void SetColorSupport(Color color)
在自動偵測失敗時覆寫終端機顏色支援
Definition terminal.cpp:135
Decorator color(Color)
使用前景顏色進行裝飾。
Color
Color 是一個列舉,表示終端機的色彩支援
Definition terminal.hpp:22
Dimensions Size()
獲取終端機大小。
Definition terminal.cpp:89
Color ColorSupport()
獲取終端機的顏色支援。
Definition terminal.cpp:125
Dimensions 是一個表示終端機大小的結構
Definition terminal.hpp:11
FTXUI ftxui::Terminal:: 命名空間
void SetFallbackSize(const Dimensions &fallbackSize)
在自動偵測失敗時覆寫終端機大小
Definition terminal.cpp:119
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10