FTXUI 6.1.9
C++ functional terminal UI.
载入中...
搜索中...
未找到
screen.cpp
浏览该文件的文档.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// 本源代码的使用受 MIT 许可协议的约束,该协议可在 LICENSE 文件中找到。
3#include <cstddef> // for size_t
4#include <cstdint>
5#include <iostream> // for operator<<, stringstream, basic_ostream, flush, cout, ostream
6#include <limits>
7#include <map> // for _Rb_tree_const_iterator, map, operator!=, operator==
8#include <sstream> // IWYU pragma: keep
9#include <utility> // for pair
10
11#include "ftxui/screen/image.hpp" // for Image
12#include "ftxui/screen/pixel.hpp" // for Pixel
14#include "ftxui/screen/string.hpp" // for string_width
15#include "ftxui/screen/terminal.hpp" // for Dimensions, Size
16
17#if defined(_WIN32)
18#define WIN32_LEAN_AND_MEAN
19#ifndef NOMINMAX
20#define NOMINMAX
21#endif
22#include <windows.h>
23#endif
24
25// 用于提示表达式可能为假的宏。
26#if !defined(FTXUI_UNLIKELY)
27#if defined(COMPILER_GCC) || defined(__clang__)
28#define FTXUI_UNLIKELY(x) __builtin_expect(!!(x), 0)
29#else
30#define FTXUI_UNLIKELY(x) (x)
31#endif // defined(COMPILER_GCC)
32#endif // !defined(FTXUI_UNLIKELY)
33
34#if !defined(FTXUI_LIKELY)
35#if defined(COMPILER_GCC) || defined(__clang__)
36#define FTXUI_LIKELY(x) __builtin_expect(!!(x), 1)
37#else
38#define FTXUI_LIKELY(x) (x)
39#endif // defined(COMPILER_GCC)
40#endif // !defined(FTXUI_LIKELY)
41
42namespace ftxui {
43
44namespace {
45
46#if defined(_WIN32)
47void WindowsEmulateVT100Terminal() {
48 static bool done = false;
49 if (done) {
50 return;
51 }
52 done = true;
53
54 // Enable VT processing on stdout and stdin
55 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
56
57 DWORD out_mode = 0;
58 GetConsoleMode(stdout_handle, &out_mode);
59
60 // https://docs.microsoft.com/en-us/windows/console/setconsolemode
61 const int enable_virtual_terminal_processing = 0x0004;
62 const int disable_newline_auto_return = 0x0008;
63 out_mode |= enable_virtual_terminal_processing;
64 out_mode |= disable_newline_auto_return;
65
66 SetConsoleMode(stdout_handle, out_mode);
67}
68#endif
69
70// NOLINTNEXTLINE(readability-function-cognitive-complexity)
71void UpdatePixelStyle(const Screen* screen,
72 std::stringstream& ss,
73 const Pixel& prev,
74 const Pixel& next) {
75 // 参见 https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
76 if (FTXUI_UNLIKELY(next.hyperlink != prev.hyperlink)) {
77 ss << "\x1B]8;;" << screen->Hyperlink(next.hyperlink) << "\x1B\\";
78 }
79
80 // 粗体
81 if (FTXUI_UNLIKELY((next.bold ^ prev.bold) | (next.dim ^ prev.dim))) {
82 // 粗体和暗色重置:
83 ss << ((prev.bold && !next.bold) || (prev.dim && !next.dim) ? "\x1B[22m"
84 : "");
85 ss << (next.bold ? "\x1B[1m" : ""); // 粗体设置
86 ss << (next.dim ? "\x1B[2m" : ""); // 暗色设置
87 }
88
89 // 下划线
90 if (FTXUI_UNLIKELY(next.underlined != prev.underlined ||
91 next.underlined_double != prev.underlined_double)) {
92 ss << (next.underlined ? "\x1B[4m" // 下划线
93 : next.underlined_double ? "\x1B[21m" // 双下划线
94 : "\x1B[24m"); // 下划线重置
95 }
96
97 // 闪烁
98 if (FTXUI_UNLIKELY(next.blink != prev.blink)) {
99 ss << (next.blink ? "\x1B[5m" // 闪烁设置
100 : "\x1B[25m"); // 闪烁重置
101 }
102
103 // 反转
104 if (FTXUI_UNLIKELY(next.inverted != prev.inverted)) {
105 ss << (next.inverted ? "\x1B[7m" // 反转设置
106 : "\x1B[27m"); // 反转重置
107 }
108
109 // 斜体
110 if (FTXUI_UNLIKELY(next.italic != prev.italic)) {
111 ss << (next.italic ? "\x1B[3m" // 斜体设置
112 : "\x1B[23m"); // 斜体重置
113 }
114
115 // 删除线
116 if (FTXUI_UNLIKELY(next.strikethrough != prev.strikethrough)) {
117 ss << (next.strikethrough ? "\x1B[9m" // 删除线
118 : "\x1B[29m"); // 删除线重置
119 }
120
121 if (FTXUI_UNLIKELY(next.foreground_color != prev.foreground_color ||
122 next.background_color != prev.background_color)) {
123 ss << "\x1B[" + next.foreground_color.Print(false) + "m";
124 ss << "\x1B[" + next.background_color.Print(true) + "m";
125 }
126}
127
128struct TileEncoding {
129 std::uint8_t left : 2;
130 std::uint8_t top : 2;
131 std::uint8_t right : 2;
132 std::uint8_t down : 2;
133 std::uint8_t round : 1;
134
135 // clang-format off
136 bool operator<(const TileEncoding& other) const {
137 if (left < other.left) { return true; }
138 if (left > other.left) { return false; }
139 if (top < other.top) { return true; }
140 if (top > other.top) { return false; }
141 if (right < other.right) { return true; }
142 if (right > other.right) { return false; }
143 if (down < other.down) { return true; }
144 if (down > other.down) { return false; }
145 if (round < other.round) { return true; }
146 if (round > other.round) { return false; }
147 return false;
148 }
149 // clang-format on
150};
151
152// clang-format off
153const std::map<std::string, TileEncoding> tile_encoding = { // NOLINT
154 {"─", {1, 0, 1, 0, 0}},
155 {"━", {2, 0, 2, 0, 0}},
156 {"╍", {2, 0, 2, 0, 0}},
157
158 {"│", {0, 1, 0, 1, 0}},
159 {"┃", {0, 2, 0, 2, 0}},
160 {"╏", {0, 2, 0, 2, 0}},
161
162 {"┌", {0, 0, 1, 1, 0}},
163 {"┍", {0, 0, 2, 1, 0}},
164 {"┎", {0, 0, 1, 2, 0}},
165 {"┏", {0, 0, 2, 2, 0}},
166
167 {"┐", {1, 0, 0, 1, 0}},
168 {"┑", {2, 0, 0, 1, 0}},
169 {"┒", {1, 0, 0, 2, 0}},
170 {"┓", {2, 0, 0, 2, 0}},
171
172 {"└", {0, 1, 1, 0, 0}},
173 {"┕", {0, 1, 2, 0, 0}},
174 {"┖", {0, 2, 1, 0, 0}},
175 {"┗", {0, 2, 2, 0, 0}},
176
177 {"┘", {1, 1, 0, 0, 0}},
178 {"┙", {2, 1, 0, 0, 0}},
179 {"┚", {1, 2, 0, 0, 0}},
180 {"┛", {2, 2, 0, 0, 0}},
181
182 {"├", {0, 1, 1, 1, 0}},
183 {"┝", {0, 1, 2, 1, 0}},
184 {"┞", {0, 2, 1, 1, 0}},
185 {"┟", {0, 1, 1, 2, 0}},
186 {"┠", {0, 2, 1, 2, 0}},
187 {"┡", {0, 2, 2, 1, 0}},
188 {"┢", {0, 1, 2, 2, 0}},
189 {"┣", {0, 2, 2, 2, 0}},
190
191 {"┤", {1, 1, 0, 1, 0}},
192 {"┥", {2, 1, 0, 1, 0}},
193 {"┦", {1, 2, 0, 1, 0}},
194 {"┧", {1, 1, 0, 2, 0}},
195 {"┨", {1, 2, 0, 2, 0}},
196 {"┩", {2, 2, 0, 1, 0}},
197 {"┪", {2, 1, 0, 2, 0}},
198 {"┫", {2, 2, 0, 2, 0}},
199
200 {"┬", {1, 0, 1, 1, 0}},
201 {"┭", {2, 0, 1, 1, 0}},
202 {"┮", {1, 0, 2, 1, 0}},
203 {"┯", {2, 0, 2, 1, 0}},
204 {"┰", {1, 0, 1, 2, 0}},
205 {"┱", {2, 0, 1, 2, 0}},
206 {"┲", {1, 0, 2, 2, 0}},
207 {"┳", {2, 0, 2, 2, 0}},
208
209 {"┴", {1, 1, 1, 0, 0}},
210 {"┵", {2, 1, 1, 0, 0}},
211 {"┶", {1, 1, 2, 0, 0}},
212 {"┷", {2, 1, 2, 0, 0}},
213 {"┸", {1, 2, 1, 0, 0}},
214 {"┹", {2, 2, 1, 0, 0}},
215 {"┺", {1, 2, 2, 0, 0}},
216 {"┻", {2, 2, 2, 0, 0}},
217
218 {"┼", {1, 1, 1, 1, 0}},
219 {"┽", {2, 1, 1, 1, 0}},
220 {"┾", {1, 1, 2, 1, 0}},
221 {"┿", {2, 1, 2, 1, 0}},
222 {"╀", {1, 2, 1, 1, 0}},
223 {"╁", {1, 1, 1, 2, 0}},
224 {"╂", {1, 2, 1, 2, 0}},
225 {"╃", {2, 2, 1, 1, 0}},
226 {"╄", {1, 2, 2, 1, 0}},
227 {"╅", {2, 1, 1, 2, 0}},
228 {"╆", {1, 1, 2, 2, 0}},
229 {"╇", {2, 2, 2, 1, 0}},
230 {"╈", {2, 1, 2, 2, 0}},
231 {"╉", {2, 2, 1, 2, 0}},
232 {"╊", {1, 2, 2, 2, 0}},
233 {"╋", {2, 2, 2, 2, 0}},
234
235 {"═", {3, 0, 3, 0, 0}},
236 {"║", {0, 3, 0, 3, 0}},
237
238 {"╒", {0, 0, 3, 1, 0}},
239 {"╓", {0, 0, 1, 3, 0}},
240 {"╔", {0, 0, 3, 3, 0}},
241
242 {"╕", {3, 0, 0, 1, 0}},
243 {"╖", {1, 0, 0, 3, 0}},
244 {"╗", {3, 0, 0, 3, 0}},
245
246 {"╘", {0, 1, 3, 0, 0}},
247 {"╙", {0, 3, 1, 0, 0}},
248 {"╚", {0, 3, 3, 0, 0}},
249
250 {"╛", {3, 1, 0, 0, 0}},
251 {"╜", {1, 3, 0, 0, 0}},
252 {"╝", {3, 3, 0, 0, 0}},
253
254 {"╞", {0, 1, 3, 1, 0}},
255 {"╟", {0, 3, 1, 3, 0}},
256 {"╠", {0, 3, 3, 3, 0}},
257
258 {"╡", {3, 1, 0, 1, 0}},
259 {"╢", {1, 3, 0, 3, 0}},
260 {"╣", {3, 3, 0, 3, 0}},
261
262 {"╤", {3, 0, 3, 1, 0}},
263 {"╥", {1, 0, 1, 3, 0}},
264 {"╦", {3, 0, 3, 3, 0}},
265
266 {"╧", {3, 1, 3, 0, 0}},
267 {"╨", {1, 3, 1, 0, 0}},
268 {"╩", {3, 3, 3, 0, 0}},
269
270 {"╪", {3, 1, 3, 1, 0}},
271 {"╫", {1, 3, 1, 3, 0}},
272 {"╬", {3, 3, 3, 3, 0}},
273
274 {"╭", {0, 0, 1, 1, 1}},
275 {"╮", {1, 0, 0, 1, 1}},
276 {"╯", {1, 1, 0, 0, 1}},
277 {"╰", {0, 1, 1, 0, 1}},
278
279 {"╴", {1, 0, 0, 0, 0}},
280 {"╵", {0, 1, 0, 0, 0}},
281 {"╶", {0, 0, 1, 0, 0}},
282 {"╷", {0, 0, 0, 1, 0}},
283
284 {"╸", {2, 0, 0, 0, 0}},
285 {"╹", {0, 2, 0, 0, 0}},
286 {"╺", {0, 0, 2, 0, 0}},
287 {"╻", {0, 0, 0, 2, 0}},
288
289 {"╼", {1, 0, 2, 0, 0}},
290 {"╽", {0, 1, 0, 2, 0}},
291 {"╾", {2, 0, 1, 0, 0}},
292 {"╿", {0, 2, 0, 1, 0}},
293};
294// clang-format on
295
296template <class A, class B>
297std::map<B, A> InvertMap(const std::map<A, B> input) {
298 std::map<B, A> output;
299 for (const auto& it : input) {
300 output[it.second] = it.first;
301 }
302 return output;
303}
304
305const std::map<TileEncoding, std::string> tile_encoding_inverse = // NOLINT
306 InvertMap(tile_encoding);
307
308void UpgradeLeftRight(std::string& left, std::string& right) {
309 const auto it_left = tile_encoding.find(left);
310 if (it_left == tile_encoding.end()) {
311 return;
312 }
313 const auto it_right = tile_encoding.find(right);
314 if (it_right == tile_encoding.end()) {
315 return;
316 }
317
318 if (it_left->second.right == 0 && it_right->second.left != 0) {
319 TileEncoding encoding_left = it_left->second;
320 encoding_left.right = it_right->second.left;
321 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
322 if (it_left_upgrade != tile_encoding_inverse.end()) {
323 left = it_left_upgrade->second;
324 }
325 }
326
327 if (it_right->second.left == 0 && it_left->second.right != 0) {
328 TileEncoding encoding_right = it_right->second;
329 encoding_right.left = it_left->second.right;
330 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
331 if (it_right_upgrade != tile_encoding_inverse.end()) {
332 right = it_right_upgrade->second;
333 }
334 }
335}
336
337void UpgradeTopDown(std::string& top, std::string& down) {
338 const auto it_top = tile_encoding.find(top);
339 if (it_top == tile_encoding.end()) {
340 return;
341 }
342 const auto it_down = tile_encoding.find(down);
343 if (it_down == tile_encoding.end()) {
344 return;
345 }
346
347 if (it_top->second.down == 0 && it_down->second.top != 0) {
348 TileEncoding encoding_top = it_top->second;
349 encoding_top.down = it_down->second.top;
350 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
351 if (it_top_down != tile_encoding_inverse.end()) {
352 top = it_top_down->second;
353 }
354 }
355
356 if (it_down->second.top == 0 && it_top->second.down != 0) {
357 TileEncoding encoding_down = it_down->second;
358 encoding_down.top = it_top->second.down;
359 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
360 if (it_down_top != tile_encoding_inverse.end()) {
361 down = it_down_top->second;
362 }
363 }
364}
365
366bool ShouldAttemptAutoMerge(Pixel& pixel) {
367 return pixel.automerge && pixel.character.size() == 3;
368}
369
370} // namespace
371
372/// 固定维度。
373/// @see Fit
374/// @see Full
375Dimensions Dimension::Fixed(int v) {
376 return {v, v};
377}
378
379/// 使用终端维度。
380/// @see Fixed
381/// @see Fit
382Dimensions Dimension::Full() {
383 return Terminal::Size();
384}
385
386// static
387/// 创建一个具有给定 x 轴和 y 轴维度的屏幕。
389 return {width.dimx, height.dimy};
390}
391
392// static
393/// 创建一个具有给定维度的屏幕。
395 return {dimension.dimx, dimension.dimy};
396}
397
398Screen::Screen(int dimx, int dimy) : Image{dimx, dimy} {
399#if defined(_WIN32)
400 // 此调用的位置有些奇怪,但我们可以假定任何实例化 Screen 对象的
401 // 人最终都希望向控制台输出一些内容。如果不是这种情况,请改用 Image
402 // 的实例。由于我们所有输入/输出操作都需要 UTF8,我们在此处将
403 // 切换到 UTF8 编码
404 SetConsoleOutputCP(CP_UTF8);
405 SetConsoleCP(CP_UTF8);
406 WindowsEmulateVT100Terminal();
407#endif
408}
409
410/// 生成一个可用于在终端上打印 Screen 的 std::string。
411/// @note 不要忘记刷新 stdout。或者,您可以使用
412/// Screen::Print();
413std::string Screen::ToString() const {
414 std::stringstream ss;
415
416 const Pixel default_pixel;
417 const Pixel* previous_pixel_ref = &default_pixel;
418
419 for (int y = 0; y < dimy_; ++y) {
420 // 两行之间的新行。
421 if (y != 0) {
422 UpdatePixelStyle(this, ss, *previous_pixel_ref, default_pixel);
423 previous_pixel_ref = &default_pixel;
424 ss << "\r\n";
425 }
426
427 // 打印全宽字符后,我们需要跳过下一个单元格。
428 bool previous_fullwidth = false;
429 for (const auto& pixel : pixels_[y]) {
430 if (!previous_fullwidth) {
431 UpdatePixelStyle(this, ss, *previous_pixel_ref, pixel);
432 previous_pixel_ref = &pixel;
433 if (pixel.character.empty()) {
434 ss << " ";
435 } else {
436 ss << pixel.character;
437 }
438 }
439 previous_fullwidth = (string_width(pixel.character) == 2);
440 }
441 }
442
443 // 将样式重置为默认值:
444 UpdatePixelStyle(this, ss, *previous_pixel_ref, default_pixel);
445
446 return ss.str();
447}
448
449// 将屏幕打印到终端。
450void Screen::Print() const {
451 std::cout << ToString() << '\0' << std::flush;
452}
453
454/// @brief 返回一个字符串,用于将光标位置重置到屏幕开头。
455///
456/// ```cpp
457/// std::string reset_position;
458/// while(true) {
459/// auto document = render();
460/// auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
461/// Render(screen, document);
462/// std::cout << reset_position << screen.ToString() << std::flush;
463/// reset_position = screen.ResetPosition();
464///
465/// using namespace std::chrono_literals;
466/// std::this_thread::sleep_for(0.01s);
467/// }
468/// ```
469///
470/// @return 用于将光标位置重置到屏幕开头的字符串。
471std::string Screen::ResetPosition(bool clear) const {
472 std::stringstream ss;
473 if (clear) {
474 ss << "\r"; // 左移;
475 ss << "\x1b[2K"; // 清除屏幕;
476 for (int y = 1; y < dimy_; ++y) {
477 ss << "\x1B[1A"; // 上移;
478 ss << "\x1B[2K"; // 清除行;
479 }
480 } else {
481 ss << "\r"; // 左移;
482 for (int y = 1; y < dimy_; ++y) {
483 ss << "\x1B[1A"; // 上移;
484 }
485 }
486 return ss.str();
487}
488
489/// @brief 清除屏幕上的所有像素。
491 Image::Clear();
492
493 cursor_.x = dimx_ - 1;
494 cursor_.y = dimy_ - 1;
495
496 hyperlinks_ = {
497 "",
498 };
499}
500
501// clang-format off
503 // 合并方框字符。
504 for (int y = 0; y < dimy_; ++y) {
505 for (int x = 0; x < dimx_; ++x) {
506 // 方框绘制字符占用 3 个字节。
507 Pixel& cur = pixels_[y][x];
508 if (!ShouldAttemptAutoMerge(cur)) {
509 continue;
510 }
511
512 if (x > 0) {
513 Pixel& left = pixels_[y][x-1];
514 if (ShouldAttemptAutoMerge(left)) {
515 UpgradeLeftRight(left.character, cur.character);
516 }
517 }
518 if (y > 0) {
519 Pixel& top = pixels_[y-1][x];
520 if (ShouldAttemptAutoMerge(top)) {
521 UpgradeTopDown(top.character, cur.character);
522 }
523 }
524 }
525 }
526}
527// clang-format on
528
529std::uint8_t Screen::RegisterHyperlink(const std::string& link) {
530 for (std::size_t i = 0; i < hyperlinks_.size(); ++i) {
531 if (hyperlinks_[i] == link) {
532 return i;
533 }
534 }
535 if (hyperlinks_.size() == std::numeric_limits<std::uint8_t>::max()) {
536 return 0;
537 }
538 hyperlinks_.push_back(link);
539 return hyperlinks_.size() - 1;
540}
541
542const std::string& Screen::Hyperlink(std::uint8_t id) const {
543 if (id >= hyperlinks_.size()) {
544 return hyperlinks_[0];
545 }
546 return hyperlinks_[id];
547}
548
549/// @brief 返回当前选择样式。
550/// @see SetSelectionStyle
554
555/// @brief 设置当前选择样式。
556/// @see GetSelectionStyle
558 selection_style_ = std::move(decorator);
559}
560
561} // namespace ftxui
std::function< void(Pixel &)> SelectionStyle
const SelectionStyle & GetSelectionStyle() const
返回当前选择样式。
const std::string & Hyperlink(uint8_t id) const
std::string ToString() const
static Screen Create(Dimensions dimension)
创建一个具有给定维度的屏幕。
uint8_t RegisterHyperlink(const std::string &link)
std::string character
Screen(int dimx, int dimy)
std::string ResetPosition(bool clear=false) const
返回一个字符串,用于将光标位置重置到屏幕开头。
void Clear()
清除屏幕上的所有像素。
SelectionStyle selection_style_
void SetSelectionStyle(SelectionStyle decorator)
设置当前选择样式。
std::vector< std::string > hyperlinks_
void Print() const
std::vector< std::vector< Pixel > > pixels_
像素的矩形网格。
像素的矩形网格。
Dimensions Size()
获取终端尺寸。
Dimensions 是一个表示终端大小的结构。
一个 Unicode 字符及其相关样式。
Dimensions Fixed(int)
Dimensions Full()
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
int string_width(const std::string &)
std::uint8_t top
#define FTXUI_UNLIKELY(x)
std::uint8_t left
std::uint8_t down
std::uint8_t right
std::uint8_t round