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