FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
screen.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 <cstddef> // for size_t
5#include <cstdint>
6#include <iostream> // for cout, flush
7#include <limits>
8#include <map> // for _Rb_tree_const_iterator, map, operator!=, operator==
9#include <string> // for string
10#include <string_view> // for string_view
11#include <utility> // for pair
12
13#include "ftxui/screen/cell.hpp" // for Cell
15#include "ftxui/screen/string.hpp" // for string_width
16#include "ftxui/screen/surface.hpp" // for Surface
17#include "ftxui/screen/terminal.hpp" // for Dimensions, Size
18
19#if defined(_WIN32)
20#define WIN32_LEAN_AND_MEAN
21#ifndef NOMINMAX
22#define NOMINMAX
23#endif
24#include <windows.h>
25#endif
26
27// Macro for hinting that an expression is likely to be false.
28#if !defined(FTXUI_UNLIKELY)
29#if defined(COMPILER_GCC) || defined(__clang__)
30#define FTXUI_UNLIKELY(x) __builtin_expect(!!(x), 0)
31#else
32#define FTXUI_UNLIKELY(x) (x)
33#endif // defined(COMPILER_GCC)
34#endif // !defined(FTXUI_UNLIKELY)
35
36#if !defined(FTXUI_LIKELY)
37#if defined(COMPILER_GCC) || defined(__clang__)
38#define FTXUI_LIKELY(x) __builtin_expect(!!(x), 1)
39#else
40#define FTXUI_LIKELY(x) (x)
41#endif // defined(COMPILER_GCC)
42#endif // !defined(FTXUI_LIKELY)
43
44namespace ftxui {
45
46namespace {
47
48#if defined(_WIN32)
49void WindowsEmulateVT100Terminal() {
50 static bool done = false;
51 if (done) {
52 return;
53 }
54 done = true;
55
56 // Enable VT processing on stdout and stdin
57 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
58
59 DWORD out_mode = 0;
60 GetConsoleMode(stdout_handle, &out_mode);
61
62 // https://docs.microsoft.com/en-us/windows/console/setconsolemode
63 const int enable_virtual_terminal_processing = 0x0004;
64 const int disable_newline_auto_return = 0x0008;
65 out_mode |= enable_virtual_terminal_processing;
66 out_mode |= disable_newline_auto_return;
67
68 SetConsoleMode(stdout_handle, out_mode);
69}
70#endif
71
72// NOLINTNEXTLINE(readability-function-cognitive-complexity)
73void UpdateCellStyle(const Screen* screen,
74 std::string& ss,
75 const Cell& prev,
76 const Cell& next) {
77 // See https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
78 if (FTXUI_UNLIKELY(next.hyperlink != prev.hyperlink)) {
79 ss += "\x1B]8;;";
80 ss += screen->Hyperlink(next.hyperlink);
81 ss += "\x1B\\";
82 }
83
84 // Bold
85 if (FTXUI_UNLIKELY((next.bold ^ prev.bold) | (next.dim ^ prev.dim))) {
86 // BOLD_AND_DIM_RESET:
87 if ((prev.bold && !next.bold) || (prev.dim && !next.dim)) {
88 ss += "\x1B[22m";
89 }
90 if (next.bold) {
91 ss += "\x1B[1m"; // BOLD_SET
92 }
93 if (next.dim) {
94 ss += "\x1B[2m"; // DIM_SET
95 }
96 }
97
98 // Underline
99 if (FTXUI_UNLIKELY(next.underlined != prev.underlined ||
100 next.underlined_double != prev.underlined_double)) {
101 ss += (next.underlined ? "\x1B[4m" // UNDERLINE
102 : next.underlined_double ? "\x1B[21m" // UNDERLINE_DOUBLE
103 : "\x1B[24m"); // UNDERLINE_RESET
104 }
105
106 // Blink
107 if (FTXUI_UNLIKELY(next.blink != prev.blink)) {
108 ss += (next.blink ? "\x1B[5m" // BLINK_SET
109 : "\x1B[25m"); // BLINK_RESET
110 }
111
112 // Inverted
113 if (FTXUI_UNLIKELY(next.inverted != prev.inverted)) {
114 ss += (next.inverted ? "\x1B[7m" // INVERTED_SET
115 : "\x1B[27m"); // INVERTED_RESET
116 }
117
118 // Italics
119 if (FTXUI_UNLIKELY(next.italic != prev.italic)) {
120 ss += (next.italic ? "\x1B[3m" // ITALIC_SET
121 : "\x1B[23m"); // ITALIC_RESET
122 }
123
124 // StrikeThrough
125 if (FTXUI_UNLIKELY(next.strikethrough != prev.strikethrough)) {
126 ss += (next.strikethrough ? "\x1B[9m" // CROSSED_OUT
127 : "\x1B[29m"); // CROSSED_OUT_RESET
128 }
129
130 if (FTXUI_UNLIKELY(next.foreground_color != prev.foreground_color ||
131 next.background_color != prev.background_color)) {
132 ss += "\x1B[";
133 next.foreground_color.PrintTo(ss, false);
134 ss += 'm';
135 ss += "\x1B[";
136 next.background_color.PrintTo(ss, true);
137 ss += 'm';
138 }
139}
140
141struct TileEncoding {
142 std::uint8_t left : 2;
143 std::uint8_t top : 2;
144 std::uint8_t right : 2;
145 std::uint8_t down : 2;
146 std::uint8_t round : 1;
147
148 // clang-format off
149 bool operator<(const TileEncoding& other) const {
150 if (left < other.left) { return true; }
151 if (left > other.left) { return false; }
152 if (top < other.top) { return true; }
153 if (top > other.top) { return false; }
154 if (right < other.right) { return true; }
155 if (right > other.right) { return false; }
156 if (down < other.down) { return true; }
157 if (down > other.down) { return false; }
158 if (round < other.round) { return true; }
159 if (round > other.round) { return false; }
160 return false;
161 }
162 // clang-format on
163};
164
165// clang-format off
166const std::map<std::string, TileEncoding> tile_encoding = { // NOLINT
167 {"─", {1, 0, 1, 0, 0}},
168 {"━", {2, 0, 2, 0, 0}},
169 {"╍", {2, 0, 2, 0, 0}},
170
171 {"│", {0, 1, 0, 1, 0}},
172 {"┃", {0, 2, 0, 2, 0}},
173 {"╏", {0, 2, 0, 2, 0}},
174
175 {"┌", {0, 0, 1, 1, 0}},
176 {"┍", {0, 0, 2, 1, 0}},
177 {"┎", {0, 0, 1, 2, 0}},
178 {"┏", {0, 0, 2, 2, 0}},
179
180 {"┐", {1, 0, 0, 1, 0}},
181 {"┑", {2, 0, 0, 1, 0}},
182 {"┒", {1, 0, 0, 2, 0}},
183 {"┓", {2, 0, 0, 2, 0}},
184
185 {"└", {0, 1, 1, 0, 0}},
186 {"┕", {0, 1, 2, 0, 0}},
187 {"┖", {0, 2, 1, 0, 0}},
188 {"┗", {0, 2, 2, 0, 0}},
189
190 {"┘", {1, 1, 0, 0, 0}},
191 {"┙", {2, 1, 0, 0, 0}},
192 {"┚", {1, 2, 0, 0, 0}},
193 {"┛", {2, 2, 0, 0, 0}},
194
195 {"├", {0, 1, 1, 1, 0}},
196 {"┝", {0, 1, 2, 1, 0}},
197 {"┞", {0, 2, 1, 1, 0}},
198 {"┟", {0, 1, 1, 2, 0}},
199 {"┠", {0, 2, 1, 2, 0}},
200 {"┡", {0, 2, 2, 1, 0}},
201 {"┢", {0, 1, 2, 2, 0}},
202 {"┣", {0, 2, 2, 2, 0}},
203
204 {"┤", {1, 1, 0, 1, 0}},
205 {"┥", {2, 1, 0, 1, 0}},
206 {"┦", {1, 2, 0, 1, 0}},
207 {"┧", {1, 1, 0, 2, 0}},
208 {"┨", {1, 2, 0, 2, 0}},
209 {"┩", {2, 2, 0, 1, 0}},
210 {"┪", {2, 1, 0, 2, 0}},
211 {"┫", {2, 2, 0, 2, 0}},
212
213 {"┬", {1, 0, 1, 1, 0}},
214 {"┭", {2, 0, 1, 1, 0}},
215 {"┮", {1, 0, 2, 1, 0}},
216 {"┯", {2, 0, 2, 1, 0}},
217 {"┰", {1, 0, 1, 2, 0}},
218 {"┱", {2, 0, 1, 2, 0}},
219 {"┲", {1, 0, 2, 2, 0}},
220 {"┳", {2, 0, 2, 2, 0}},
221
222 {"┴", {1, 1, 1, 0, 0}},
223 {"┵", {2, 1, 1, 0, 0}},
224 {"┶", {1, 1, 2, 0, 0}},
225 {"┷", {2, 1, 2, 0, 0}},
226 {"┸", {1, 2, 1, 0, 0}},
227 {"┹", {2, 2, 1, 0, 0}},
228 {"┺", {1, 2, 2, 0, 0}},
229 {"┻", {2, 2, 2, 0, 0}},
230
231 {"┼", {1, 1, 1, 1, 0}},
232 {"┽", {2, 1, 1, 1, 0}},
233 {"┾", {1, 1, 2, 1, 0}},
234 {"┿", {2, 1, 2, 1, 0}},
235 {"╀", {1, 2, 1, 1, 0}},
236 {"╁", {1, 1, 1, 2, 0}},
237 {"╂", {1, 2, 1, 2, 0}},
238 {"╃", {2, 2, 1, 1, 0}},
239 {"╄", {1, 2, 2, 1, 0}},
240 {"╅", {2, 1, 1, 2, 0}},
241 {"╆", {1, 1, 2, 2, 0}},
242 {"╇", {2, 2, 2, 1, 0}},
243 {"╈", {2, 1, 2, 2, 0}},
244 {"╉", {2, 2, 1, 2, 0}},
245 {"╊", {1, 2, 2, 2, 0}},
246 {"╋", {2, 2, 2, 2, 0}},
247
248 {"═", {3, 0, 3, 0, 0}},
249 {"║", {0, 3, 0, 3, 0}},
250
251 {"╒", {0, 0, 3, 1, 0}},
252 {"╓", {0, 0, 1, 3, 0}},
253 {"╔", {0, 0, 3, 3, 0}},
254
255 {"╕", {3, 0, 0, 1, 0}},
256 {"╖", {1, 0, 0, 3, 0}},
257 {"╗", {3, 0, 0, 3, 0}},
258
259 {"╘", {0, 1, 3, 0, 0}},
260 {"╙", {0, 3, 1, 0, 0}},
261 {"╚", {0, 3, 3, 0, 0}},
262
263 {"╛", {3, 1, 0, 0, 0}},
264 {"╜", {1, 3, 0, 0, 0}},
265 {"╝", {3, 3, 0, 0, 0}},
266
267 {"╞", {0, 1, 3, 1, 0}},
268 {"╟", {0, 3, 1, 3, 0}},
269 {"╠", {0, 3, 3, 3, 0}},
270
271 {"╡", {3, 1, 0, 1, 0}},
272 {"╢", {1, 3, 0, 3, 0}},
273 {"╣", {3, 3, 0, 3, 0}},
274
275 {"╤", {3, 0, 3, 1, 0}},
276 {"╥", {1, 0, 1, 3, 0}},
277 {"╦", {3, 0, 3, 3, 0}},
278
279 {"╧", {3, 1, 3, 0, 0}},
280 {"╨", {1, 3, 1, 0, 0}},
281 {"╩", {3, 3, 3, 0, 0}},
282
283 {"╪", {3, 1, 3, 1, 0}},
284 {"╫", {1, 3, 1, 3, 0}},
285 {"╬", {3, 3, 3, 3, 0}},
286
287 {"╭", {0, 0, 1, 1, 1}},
288 {"╮", {1, 0, 0, 1, 1}},
289 {"╯", {1, 1, 0, 0, 1}},
290 {"╰", {0, 1, 1, 0, 1}},
291
292 {"╴", {1, 0, 0, 0, 0}},
293 {"╵", {0, 1, 0, 0, 0}},
294 {"╶", {0, 0, 1, 0, 0}},
295 {"╷", {0, 0, 0, 1, 0}},
296
297 {"╸", {2, 0, 0, 0, 0}},
298 {"╹", {0, 2, 0, 0, 0}},
299 {"╺", {0, 0, 2, 0, 0}},
300 {"╻", {0, 0, 0, 2, 0}},
301
302 {"╼", {1, 0, 2, 0, 0}},
303 {"╽", {0, 1, 0, 2, 0}},
304 {"╾", {2, 0, 1, 0, 0}},
305 {"╿", {0, 2, 0, 1, 0}},
306};
307// clang-format on
308
309template <class A, class B>
310std::map<B, A> InvertMap(const std::map<A, B>& input) {
311 std::map<B, A> output;
312 for (const auto& it : input) {
313 output[it.second] = it.first;
314 }
315 return output;
316}
317
318const std::map<TileEncoding, std::string> tile_encoding_inverse = // NOLINT
319 InvertMap(tile_encoding);
320
321void UpgradeLeftRight(std::string& left, std::string& right) {
322 const auto it_left = tile_encoding.find(left);
323 if (it_left == tile_encoding.end()) {
324 return;
325 }
326 const auto it_right = tile_encoding.find(right);
327 if (it_right == tile_encoding.end()) {
328 return;
329 }
330
331 if (it_left->second.right == 0 && it_right->second.left != 0) {
332 TileEncoding encoding_left = it_left->second;
333 encoding_left.right = it_right->second.left;
334 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
335 if (it_left_upgrade != tile_encoding_inverse.end()) {
336 left = it_left_upgrade->second;
337 }
338 }
339
340 if (it_right->second.left == 0 && it_left->second.right != 0) {
341 TileEncoding encoding_right = it_right->second;
342 encoding_right.left = it_left->second.right;
343 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
344 if (it_right_upgrade != tile_encoding_inverse.end()) {
345 right = it_right_upgrade->second;
346 }
347 }
348}
349
350void UpgradeTopDown(std::string& top, std::string& down) {
351 const auto it_top = tile_encoding.find(top);
352 if (it_top == tile_encoding.end()) {
353 return;
354 }
355 const auto it_down = tile_encoding.find(down);
356 if (it_down == tile_encoding.end()) {
357 return;
358 }
359
360 if (it_top->second.down == 0 && it_down->second.top != 0) {
361 TileEncoding encoding_top = it_top->second;
362 encoding_top.down = it_down->second.top;
363 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
364 if (it_top_down != tile_encoding_inverse.end()) {
365 top = it_top_down->second;
366 }
367 }
368
369 if (it_down->second.top == 0 && it_top->second.down != 0) {
370 TileEncoding encoding_down = it_down->second;
371 encoding_down.top = it_top->second.down;
372 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
373 if (it_down_top != tile_encoding_inverse.end()) {
374 down = it_down_top->second;
375 }
376 }
377}
378
379bool ShouldAttemptAutoMerge(Cell& cell) {
380 return cell.automerge && cell.character.size() == 3;
381}
382
383} // namespace
384
385/// A fixed dimension.
386/// @see Fit
387/// @see Full
388Dimensions Dimension::Fixed(int v) {
389 return {v, v};
390}
391
392/// Use the terminal dimensions.
393/// @see Fixed
394/// @see Fit
395Dimensions Dimension::Full() {
396 return Terminal::Size();
397}
398
399// static
400/// Create a screen with the given dimension along the x-axis and y-axis.
401Screen Screen::Create(Dimensions width, Dimensions height) {
402 return {width.dimx, height.dimy};
403}
404
405// static
406/// Create a screen with the given dimension.
407Screen Screen::Create(Dimensions dimension) {
408 return {dimension.dimx, dimension.dimy};
409}
410
411Screen::Screen(int dimx, int dimy) : Surface{dimx, dimy} {
412#if defined(_WIN32)
413 // The placement of this call is a bit weird, however we can assume that
414 // anybody who instantiates a Screen object eventually wants to output
415 // something to the console. If that is not the case, use an instance of
416 // Surface instead. As we require UTF8 for all input/output operations we will
417 // just switch to UTF8 encoding here
418 SetConsoleOutputCP(CP_UTF8);
419 SetConsoleCP(CP_UTF8);
420 WindowsEmulateVT100Terminal();
421#endif
422}
423
424/// Produce a std::string that can be used to print the Screen on the
425/// terminal.
426/// @note Don't forget to flush stdout. Alternatively, you can use
427/// Screen::Print();
428std::string Screen::ToString() const {
429 // Pre-allocate: ~30 bytes per cell for character + escape codes.
430 std::string ss;
431 ss.reserve(static_cast<size_t>(dimx_) * static_cast<size_t>(dimy_) * 30);
432 ToString(ss);
433 return ss;
434}
435
436/// Produce a std::string that can be used to print the Screen on the
437/// terminal.
438/// @param ss The string to append to.
439void Screen::ToString(std::string& ss) const {
440 const Cell default_cell;
441 const Cell* previous_cell_ref = &default_cell;
442
443 for (int y = 0; y < dimy_; ++y) {
444 // New line in between two lines.
445 if (y != 0) {
446 UpdateCellStyle(this, ss, *previous_cell_ref, default_cell);
447 previous_cell_ref = &default_cell;
448 ss += "\r\n";
449 }
450
451 // After printing a fullwith character, we need to skip the next cell.
452 bool previous_fullwidth = false;
453 if (dimx_ > 0) {
454 const Cell* line_start = &FastCellAt(0, y);
455 const Cell* line_end = line_start + dimx_;
456 for (const Cell* it = line_start; it != line_end; ++it) {
457 const auto& cell = *it;
458 if (!previous_fullwidth) {
459 UpdateCellStyle(this, ss, *previous_cell_ref, cell);
460 previous_cell_ref = &cell;
461 if (cell.character.empty()) {
462 ss += ' ';
463 } else {
464 ss += cell.character;
465 }
466 }
467 if (cell.character.size() <= 1) {
468 previous_fullwidth = false;
469 } else {
470 previous_fullwidth = (string_width(cell.character) == 2);
471 }
472 }
473 }
474 }
475
476 // Reset the style to default:
477 UpdateCellStyle(this, ss, *previous_cell_ref, default_cell);
478}
479
480// Print the Screen to the terminal.
481void Screen::Print() const {
482 std::cout << ToString() << '\0' << std::flush;
483}
484
485/// @brief Return a string to be printed in order to reset the cursor position
486/// to the beginning of the screen.
487///
488/// ```cpp
489/// std::string reset_position;
490/// while(true) {
491/// auto document = render();
492/// auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
493/// Render(screen, document);
494/// std::cout << reset_position << screen.ToString() << std::flush;
495/// reset_position = screen.ResetPosition();
496///
497/// using namespace std::chrono_literals;
498/// std::this_thread::sleep_for(0.01s);
499/// }
500/// ```
501///
502/// @return The string to print in order to reset the cursor position to the
503/// beginning.
504std::string Screen::ResetPosition(bool clear) const {
505 std::string ss;
506 ss.reserve(static_cast<size_t>(dimy_) * 12);
507 ResetPosition(ss, clear);
508 return ss;
509}
510
511/// @brief Append to a string in order to reset the cursor position to the
512/// beginning of the screen.
513/// @param ss The string to append to.
514/// @param clear Whether to clear the screen or not.
515void Screen::ResetPosition(std::string& ss, bool clear) const {
516 if (clear) {
517 ss += '\r'; // MOVE_LEFT;
518 ss += "\x1b[2K"; // CLEAR_SCREEN;
519 for (int y = 1; y < dimy_; ++y) {
520 ss += "\x1B[1A"; // MOVE_UP;
521 ss += "\x1B[2K"; // CLEAR_LINE;
522 }
523 } else {
524 ss += '\r'; // MOVE_LEFT;
525 for (int y = 1; y < dimy_; ++y) {
526 ss += "\x1B[1A"; // MOVE_UP;
527 }
528 }
529}
530
531/// @brief Clear all the cells from the screen.
532void Screen::Clear() {
533 Surface::Clear();
534
535 cursor_.x = dimx_ - 1;
536 cursor_.y = dimy_ - 1;
537
538 hyperlinks_ = {
539 "",
540 };
541}
542
543// clang-format off
544void Screen::ApplyShader() {
545 // Merge box characters together.
546 for (int y = 0; y < dimy_; ++y) {
547 for (int x = 0; x < dimx_; ++x) {
548 // Box drawing character uses exactly 3 byte.
549 Cell& cur = FastCellAt(x, y);
550 if (!ShouldAttemptAutoMerge(cur)) {
551 continue;
552 }
553
554 if (x > 0) {
555 Cell& left = FastCellAt(x - 1, y);
556 if (ShouldAttemptAutoMerge(left)) {
557 UpgradeLeftRight(left.character, cur.character);
558 }
559 }
560 if (y > 0) {
561 Cell& top = FastCellAt(x, y - 1);
562 if (ShouldAttemptAutoMerge(top)) {
563 UpgradeTopDown(top.character, cur.character);
564 }
565 }
566 }
567 }
568}
569// clang-format on
570
571std::uint8_t Screen::RegisterHyperlink(std::string_view link) {
572 for (std::size_t i = 0; i < hyperlinks_.size(); ++i) {
573 if (hyperlinks_[i] == link) {
574 return i;
575 }
576 }
577 if (hyperlinks_.size() == std::numeric_limits<std::uint8_t>::max()) {
578 return 0;
579 }
580 hyperlinks_.emplace_back(link);
581 return hyperlinks_.size() - 1;
582}
583
584const std::string& Screen::Hyperlink(std::uint8_t id) const {
585 if (id >= hyperlinks_.size()) {
586 return hyperlinks_[0];
587 }
588 return hyperlinks_[id];
589}
590
591/// @brief Return the current selection style.
592/// @see SetSelectionStyle
593const Screen::SelectionStyle& Screen::GetSelectionStyle() const {
594 return selection_style_;
595}
596
597/// @brief Set the current selection style.
598/// @see GetSelectionStyle
599void Screen::SetSelectionStyle(SelectionStyle decorator) {
600 selection_style_ = std::move(decorator);
601}
602
603} // namespace ftxui
Dimensions Size()
Get the terminal size.
Definition terminal.cpp:264
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
int y
Definition elements.hpp:126
std::uint8_t top
Definition screen.cpp:143
#define FTXUI_UNLIKELY(x)
Definition screen.cpp:32
std::uint8_t left
Definition screen.cpp:142
std::uint8_t down
Definition screen.cpp:145
std::uint8_t right
Definition screen.cpp:144
std::uint8_t round
Definition screen.cpp:146