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.
402 return {width.dimx, height.dimy};
403}
404
405// static
406/// Create a screen with the given 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 for (const auto& cell : cells_[y]) {
454 if (!previous_fullwidth) {
455 UpdateCellStyle(this, ss, *previous_cell_ref, cell);
456 previous_cell_ref = &cell;
457 if (cell.character.empty()) {
458 ss += ' ';
459 } else {
460 ss += cell.character;
461 }
462 }
463 if (cell.character.size() <= 1) {
464 previous_fullwidth = false;
465 } else {
466 previous_fullwidth = (string_width(cell.character) == 2);
467 }
468 }
469 }
470
471 // Reset the style to default:
472 UpdateCellStyle(this, ss, *previous_cell_ref, default_cell);
473}
474
475// Print the Screen to the terminal.
476void Screen::Print() const {
477 std::cout << ToString() << '\0' << std::flush;
478}
479
480/// @brief Return a string to be printed in order to reset the cursor position
481/// to the beginning of the screen.
482///
483/// ```cpp
484/// std::string reset_position;
485/// while(true) {
486/// auto document = render();
487/// auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
488/// Render(screen, document);
489/// std::cout << reset_position << screen.ToString() << std::flush;
490/// reset_position = screen.ResetPosition();
491///
492/// using namespace std::chrono_literals;
493/// std::this_thread::sleep_for(0.01s);
494/// }
495/// ```
496///
497/// @return The string to print in order to reset the cursor position to the
498/// beginning.
499std::string Screen::ResetPosition(bool clear) const {
500 std::string ss;
501 ss.reserve(static_cast<size_t>(dimy_) * 12);
502 ResetPosition(ss, clear);
503 return ss;
504}
505
506/// @brief Append to a string in order to reset the cursor position to the
507/// beginning of the screen.
508/// @param ss The string to append to.
509/// @param clear Whether to clear the screen or not.
510void Screen::ResetPosition(std::string& ss, bool clear) const {
511 if (clear) {
512 ss += '\r'; // MOVE_LEFT;
513 ss += "\x1b[2K"; // CLEAR_SCREEN;
514 for (int y = 1; y < dimy_; ++y) {
515 ss += "\x1B[1A"; // MOVE_UP;
516 ss += "\x1B[2K"; // CLEAR_LINE;
517 }
518 } else {
519 ss += '\r'; // MOVE_LEFT;
520 for (int y = 1; y < dimy_; ++y) {
521 ss += "\x1B[1A"; // MOVE_UP;
522 }
523 }
524}
525
526/// @brief Clear all the cells from the screen.
529
530 cursor_.x = dimx_ - 1;
531 cursor_.y = dimy_ - 1;
532
533 hyperlinks_ = {
534 "",
535 };
536}
537
538// clang-format off
540 // Merge box characters togethers.
541 for (int y = 0; y < dimy_; ++y) {
542 for (int x = 0; x < dimx_; ++x) {
543 // Box drawing character uses exactly 3 byte.
544 Cell& cur = cells_[y][x];
545 if (!ShouldAttemptAutoMerge(cur)) {
546 continue;
547 }
548
549 if (x > 0) {
550 Cell& left = cells_[y][x-1];
551 if (ShouldAttemptAutoMerge(left)) {
552 UpgradeLeftRight(left.character, cur.character);
553 }
554 }
555 if (y > 0) {
556 Cell& top = cells_[y-1][x];
557 if (ShouldAttemptAutoMerge(top)) {
558 UpgradeTopDown(top.character, cur.character);
559 }
560 }
561 }
562 }
563}
564// clang-format on
565
566std::uint8_t Screen::RegisterHyperlink(std::string_view link) {
567 for (std::size_t i = 0; i < hyperlinks_.size(); ++i) {
568 if (hyperlinks_[i] == link) {
569 return i;
570 }
571 }
572 if (hyperlinks_.size() == std::numeric_limits<std::uint8_t>::max()) {
573 return 0;
574 }
575 hyperlinks_.emplace_back(link);
576 return hyperlinks_.size() - 1;
577}
578
579const std::string& Screen::Hyperlink(std::uint8_t id) const {
580 if (id >= hyperlinks_.size()) {
581 return hyperlinks_[0];
582 }
583 return hyperlinks_[id];
584}
585
586/// @brief Return the current selection style.
587/// @see SetSelectionStyle
591
592/// @brief Set the current selection style.
593/// @see GetSelectionStyle
595 selection_style_ = std::move(decorator);
596}
597
598} // namespace ftxui
void ApplyShader()
Definition screen.cpp:539
const SelectionStyle & GetSelectionStyle() const
Return the current selection style.
Definition screen.cpp:588
const std::string & Hyperlink(uint8_t id) const
Definition screen.cpp:579
std::string ToString() const
Definition screen.cpp:428
uint8_t RegisterHyperlink(std::string_view link)
Definition screen.cpp:566
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:407
std::string character
Definition cell.hpp:48
std::function< void(Cell &)> SelectionStyle
Definition screen.hpp:76
Screen(int dimx, int dimy)
Definition screen.cpp:411
std::string ResetPosition(bool clear=false) const
Return a string to be printed in order to reset the cursor position to the beginning of the screen.
Definition screen.cpp:499
Cursor cursor_
Definition screen.hpp:81
void Clear()
Clear all the cells from the screen.
Definition screen.cpp:527
std::vector< std::vector< Cell > > cells_
Definition surface.hpp:53
SelectionStyle selection_style_
Definition screen.hpp:85
void SetSelectionStyle(SelectionStyle decorator)
Set the current selection style.
Definition screen.cpp:594
std::vector< std::string > hyperlinks_
Definition screen.hpp:82
void Print() const
Definition screen.cpp:476
A rectangular grid of Cell.
Definition screen.hpp:26
A rectangular grid of Cell.
Definition surface.hpp:20
Dimensions Size()
Get the terminal size.
Definition terminal.cpp:130
A Unicode character and its associated style.
Definition cell.hpp:18
Dimensions is a structure that represents the size of the terminal.
Definition terminal.hpp:11
Dimensions Fixed(int)
Dimensions Full()
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
int string_width(std::string_view)
Definition string.cpp:1337
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