FTXUI  4.1.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
screen.cpp
Go to the documentation of this file.
1#include <cstdint> // for uint8_t
2#include <iostream> // for operator<<, stringstream, basic_ostream, flush, cout, ostream
3#include <map> // for _Rb_tree_const_iterator, map, operator!=, operator==
4#include <memory> // for allocator
5#include <sstream> // IWYU pragma: keep
6#include <utility> // for pair
7
9#include "ftxui/screen/string.hpp" // for string_width
10#include "ftxui/screen/terminal.hpp" // for Dimensions, Size
11
12#if defined(_WIN32)
13#define WIN32_LEAN_AND_MEAN
14#ifndef NOMINMAX
15#define NOMINMAX
16#endif
17#include <windows.h>
18#endif
19
20namespace ftxui {
21
22namespace {
23
24Pixel& dev_null_pixel() {
25 static Pixel pixel;
26 return pixel;
27}
28
29#if defined(_WIN32)
30void WindowsEmulateVT100Terminal() {
31 static bool done = false;
32 if (done)
33 return;
34 done = true;
35
36 // Enable VT processing on stdout and stdin
37 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
38
39 DWORD out_mode = 0;
40 GetConsoleMode(stdout_handle, &out_mode);
41
42 // https://docs.microsoft.com/en-us/windows/console/setconsolemode
43 const int enable_virtual_terminal_processing = 0x0004;
44 const int disable_newline_auto_return = 0x0008;
45 out_mode |= enable_virtual_terminal_processing;
46 out_mode |= disable_newline_auto_return;
47
48 SetConsoleMode(stdout_handle, out_mode);
49}
50#endif
51
52// NOLINTNEXTLINE(readability-function-cognitive-complexity)
53void UpdatePixelStyle(std::stringstream& ss,
54 Pixel& previous,
55 const Pixel& next) {
56 if (next == previous) {
57 return;
58 }
59
60 if ((!next.bold && previous.bold) || //
61 (!next.dim && previous.dim)) {
62 ss << "\x1B[22m"; // BOLD_RESET and DIM_RESET
63 // We might have wrongfully reset dim or bold because they share the same
64 // resetter. Take it into account so that the side effect will cause it to
65 // be set again below.
66 previous.bold = false;
67 previous.dim = false;
68 }
69
70 if ((!next.underlined && previous.underlined) ||
71 (!next.underlined_double && previous.underlined_double)) {
72 // We might have wrongfully reset underlined or underlinedbold because they
73 // share the same resetter. Take it into account so that the side effect
74 // will cause it to be set again below.
75 ss << "\x1B[24m"; // UNDERLINED_RESET
76 previous.underlined = false;
77 previous.underlined_double = false;
78 }
79
80 if (next.bold && !previous.bold) {
81 ss << "\x1B[1m"; // BOLD_SET
82 }
83
84 if (next.dim && !previous.dim) {
85 ss << "\x1B[2m"; // DIM_SET
86 }
87
88 if (next.underlined && !previous.underlined) {
89 ss << "\x1B[4m"; // UNDERLINED_SET
90 }
91
92 if (next.blink && !previous.blink) {
93 ss << "\x1B[5m"; // BLINK_SET
94 }
95
96 if (!next.blink && previous.blink) {
97 ss << "\x1B[25m"; // BLINK_RESET
98 }
99
100 if (next.inverted && !previous.inverted) {
101 ss << "\x1B[7m"; // INVERTED_SET
102 }
103
104 if (!next.inverted && previous.inverted) {
105 ss << "\x1B[27m"; // INVERTED_RESET
106 }
107
108 if (next.strikethrough && !previous.strikethrough) {
109 ss << "\x1B[9m"; // CROSSED_OUT
110 }
111
112 if (!next.strikethrough && previous.strikethrough) {
113 ss << "\x1B[29m"; // CROSSED_OUT_RESET
114 }
115
116 if (next.underlined_double && !previous.underlined_double) {
117 ss << "\x1B[21m"; // DOUBLE_UNDERLINED_SET
118 }
119
120 if (next.foreground_color != previous.foreground_color ||
121 next.background_color != previous.background_color) {
122 ss << "\x1B[" + next.foreground_color.Print(false) + "m";
123 ss << "\x1B[" + next.background_color.Print(true) + "m";
124 }
125
126 previous = next;
127}
128
129struct TileEncoding {
130 uint8_t left : 2;
131 uint8_t top : 2;
132 uint8_t right : 2;
133 uint8_t down : 2;
134 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
373bool Pixel::operator==(const Pixel& other) const {
374 return character == other.character && //
377 blink == other.blink && //
378 bold == other.bold && //
379 dim == other.dim && //
380 inverted == other.inverted && //
381 underlined == other.underlined && //
382 automerge == other.automerge; //
383}
384
385/// A fixed dimension.
386/// @see Fit
387/// @see Full
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)
412 : stencil{0, dimx - 1, 0, dimy - 1},
413 dimx_(dimx),
414 dimy_(dimy),
415 pixels_(dimy, std::vector<Pixel>(dimx)) {
416#if defined(_WIN32)
417 // The placement of this call is a bit weird, however we can assume that
418 // anybody who instantiates a Screen object eventually wants to output
419 // something to the console.
420 // As we require UTF8 for all input/output operations we will just switch to
421 // UTF8 encoding here
422 SetConsoleOutputCP(CP_UTF8);
423 SetConsoleCP(CP_UTF8);
424 WindowsEmulateVT100Terminal();
425#endif
426}
427
428/// Produce a std::string that can be used to print the Screen on the terminal.
429/// Don't forget to flush stdout. Alternatively, you can use Screen::Print();
430std::string Screen::ToString() {
431 std::stringstream ss;
432
433 Pixel previous_pixel;
434 const Pixel final_pixel;
435
436 for (int y = 0; y < dimy_; ++y) {
437 if (y != 0) {
438 UpdatePixelStyle(ss, previous_pixel, final_pixel);
439 ss << "\r\n";
440 }
441 bool previous_fullwidth = false;
442 for (const auto& pixel : pixels_[y]) {
443 if (!previous_fullwidth) {
444 UpdatePixelStyle(ss, previous_pixel, pixel);
445 ss << pixel.character;
446 }
447 previous_fullwidth = (string_width(pixel.character) == 2);
448 }
449 }
450
451 UpdatePixelStyle(ss, previous_pixel, final_pixel);
452
453 return ss.str();
454}
455
457 std::cout << ToString() << '\0' << std::flush;
458}
459
460/// @brief Access a character a given position.
461/// @param x The character position along the x-axis.
462/// @param y The character position along the y-axis.
463std::string& Screen::at(int x, int y) {
464 return PixelAt(x, y).character;
465}
466
467/// @brief Access a Pixel at a given position.
468/// @param x The pixel position along the x-axis.
469/// @param y The pixel position along the y-axis.
470Pixel& Screen::PixelAt(int x, int y) {
471 return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
472}
473
474/// @brief Return a string to be printed in order to reset the cursor position
475/// to the beginning of the screen.
476///
477/// ```cpp
478/// std::string reset_position;
479/// while(true) {
480/// auto document = render();
481/// auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
482/// Render(screen, document);
483/// std::cout << reset_position << screen.ToString() << std::flush;
484/// reset_position = screen.ResetPosition();
485///
486/// using namespace std::chrono_literals;
487/// std::this_thread::sleep_for(0.01s);
488/// }
489/// ```
490///
491/// @return The string to print in order to reset the cursor position to the
492/// beginning.
493std::string Screen::ResetPosition(bool clear) const {
494 std::stringstream ss;
495 if (clear) {
496 ss << "\r"; // MOVE_LEFT;
497 ss << "\x1b[2K"; // CLEAR_SCREEN;
498 for (int y = 1; y < dimy_; ++y) {
499 ss << "\x1B[1A"; // MOVE_UP;
500 ss << "\x1B[2K"; // CLEAR_LINE;
501 }
502 } else {
503 ss << "\r"; // MOVE_LEFT;
504 for (int y = 1; y < dimy_; ++y) {
505 ss << "\x1B[1A"; // MOVE_UP;
506 }
507 }
508 return ss.str();
509}
510
511/// @brief Clear all the pixel from the screen.
513 for (auto& line : pixels_) {
514 for (auto& cell : line) {
515 cell = Pixel();
516 }
517 }
518 cursor_.x = dimx_ - 1;
519 cursor_.y = dimy_ - 1;
520}
521
522// clang-format off
524 // Merge box characters togethers.
525 for (int y = 0; y < dimy_; ++y) {
526 for (int x = 0; x < dimx_; ++x) {
527 // Box drawing character uses exactly 3 byte.
528 Pixel& cur = pixels_[y][x];
529 if (!ShouldAttemptAutoMerge(cur)) {
530 continue;
531 }
532
533 if (x > 0) {
534 Pixel& left = pixels_[y][x-1];
535 if (ShouldAttemptAutoMerge(left)) {
536 UpgradeLeftRight(left.character, cur.character);
537 }
538 }
539 if (y > 0) {
540 Pixel& top = pixels_[y-1][x];
541 if (ShouldAttemptAutoMerge(top)) {
542 UpgradeTopDown(top.character, cur.character);
543 }
544 }
545 }
546 }
547}
548
549// clang-format on
550
551} // namespace ftxui
552
553// Copyright 2020 Arthur Sonzogni. All rights reserved.
554// Use of this source code is governed by the MIT license that can be found in
555// the LICENSE file.
A rectangular grid of Pixel.
Definition screen.hpp:57
void ApplyShader()
Definition screen.cpp:523
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:407
Pixel & PixelAt(int x, int y)
Access a Pixel at a given position.
Definition screen.cpp:470
std::string & at(int x, int y)
Access a character a given position.
Definition screen.cpp:463
Screen(int dimx, int dimy)
Definition screen.cpp:411
std::string ToString()
Definition screen.cpp:430
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:493
void Print()
Definition screen.cpp:456
Cursor cursor_
Definition screen.hpp:108
void Clear()
Clear all the pixel from the screen.
Definition screen.cpp:512
std::vector< std::vector< Pixel > > pixels_
Definition screen.hpp:107
Dimensions Fixed(int)
Dimensions Full()
Dimensions Size()
Definition terminal.cpp:87
int string_width(const std::string &)
Definition string.cpp:1700
bool Contain(int x, int y) const
Definition box.cpp:32
A unicode character and its associated style.
Definition screen.hpp:16
bool operator==(const Pixel &other) const
Definition screen.cpp:373
bool inverted
Definition screen.hpp:31
Color foreground_color
Definition screen.hpp:25
Color background_color
Definition screen.hpp:24
std::string character
Definition screen.hpp:21
bool underlined
Definition screen.hpp:32
bool automerge
Definition screen.hpp:35