FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
terminal.cpp
浏览该文件的文档.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// 本源代码的使用受 MIT 许可的约束,该许可可在 LICENSE 文件中找到。
3#include <cstdlib> // for getenv
4#include <string> // for string, allocator
5
7
8#if defined(_WIN32)
9#define WIN32_LEAN_AND_MEAN
10
11#ifndef NOMINMAX
12#define NOMINMAX
13#endif
14
15#include <windows.h>
16#else
17#include <sys/ioctl.h> // for winsize, ioctl, TIOCGWINSZ
18#include <unistd.h> // for STDOUT_FILENO
19#endif
20
21namespace ftxui {
22
23namespace {
24
25bool g_cached = false; // NOLINT
26Terminal::Color g_cached_supported_color; // NOLINT
27
28Dimensions& FallbackSize() {
29#if defined(__EMSCRIPTEN__)
30 // 选择此维度是为了能够显示:
31 // https://arthursonzogni.com/FTXUI/examples
32 // 当有人有时间实现并需要时,这将会得到改进。
33 constexpr int fallback_width = 140;
34 constexpr int fallback_height = 43;
35#else
36 // VT100 中的终端尺寸是 80x24。它至今仍被许多终端模拟器默认使用。
37 // 这是一个很好的回退值选择。
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 // 选择此维度是为了能够显示:
92 // https://arthursonzogni.com/FTXUI/examples
93 // 当有人有时间实现并需要时,这将会得到改进。
94 return FallbackSize();
95#elif defined(_WIN32)
96 CONSOLE_SCREEN_BUFFER_INFO csbi;
97
98 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
99 return Dimensions{csbi.srWindow.Right - csbi.srWindow.Left + 1,
100 csbi.srWindow.Bottom - csbi.srWindow.Top + 1};
101 }
102
103 return FallbackSize();
104#else
105 winsize w{};
106 const int status = ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); // NOLINT
107 // 应该检查 ioctl 返回值。某些操作系统不支持 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 在自动检测失败时覆盖终端尺寸
116/// @param fallbackSize 回退的终端尺寸
117void SetFallbackSize(const Dimensions& fallbackSize) {
118 FallbackSize() = fallbackSize;
119}
120
121/// @brief 获取终端的颜色支持。
122/// @ingroup screen
124 if (!g_cached) {
125 g_cached = true;
126 g_cached_supported_color = ComputeColorSupport();
127 }
128 return g_cached_supported_color;
129}
130
131/// @brief 在自动检测失败时覆盖终端颜色支持
132/// @ingroup dom
134 g_cached = true;
135 g_cached_supported_color = color;
136}
137
138} // namespace Terminal
139} // namespace ftxui
void SetColorSupport(Color color)
在自动检测失败时覆盖终端颜色支持
Decorator color(Color)
使用前景色进行装饰。
Color
Color 是一个表示终端颜色支持的枚举。
Dimensions Size()
获取终端尺寸。
Color ColorSupport()
获取终端的颜色支持。
Dimensions 是一个表示终端大小的结构。
FTXUI ftxui::Terminal:: 命名空间
void SetFallbackSize(const Dimensions &fallbackSize)
在自动检测失败时覆盖终端尺寸
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase