FTXUI  4.0.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
158 {"│", {0, 1, 0, 1, 0}},
159 {"┃", {0, 2, 0, 2, 0}},
160
161 {"┌", {0, 0, 1, 1, 0}},
162 {"┍", {0, 0, 2, 1, 0}},
163 {"┎", {0, 0, 1, 2, 0}},
164 {"┏", {0, 0, 2, 2, 0}},
165
166 {"┐", {1, 0, 0, 1, 0}},
167 {"┑", {2, 0, 0, 1, 0}},
168 {"┒", {1, 0, 0, 2, 0}},
169 {"┓", {2, 0, 0, 2, 0}},
170
171 {"└", {0, 1, 1, 0, 0}},
172 {"┕", {0, 1, 2, 0, 0}},
173 {"┖", {0, 2, 1, 0, 0}},
174 {"┗", {0, 2, 2, 0, 0}},
175
176 {"┘", {1, 1, 0, 0, 0}},
177 {"┙", {2, 1, 0, 0, 0}},
178 {"┚", {1, 2, 0, 0, 0}},
179 {"┛", {2, 2, 0, 0, 0}},
180
181 {"├", {0, 1, 1, 1, 0}},
182 {"┝", {0, 1, 2, 1, 0}},
183 {"┞", {0, 2, 1, 1, 0}},
184 {"┟", {0, 1, 1, 2, 0}},
185 {"┠", {0, 2, 1, 2, 0}},
186 {"┡", {0, 2, 2, 1, 0}},
187 {"┢", {0, 1, 2, 2, 0}},
188 {"┣", {0, 2, 2, 2, 0}},
189
190 {"┤", {1, 1, 0, 1, 0}},
191 {"┥", {2, 1, 0, 1, 0}},
192 {"┦", {1, 2, 0, 1, 0}},
193 {"┧", {1, 1, 0, 2, 0}},
194 {"┨", {1, 2, 0, 2, 0}},
195 {"┩", {2, 2, 0, 1, 0}},
196 {"┪", {2, 1, 0, 2, 0}},
197 {"┫", {2, 2, 0, 2, 0}},
198
199 {"┬", {1, 0, 1, 1, 0}},
200 {"┭", {2, 0, 1, 1, 0}},
201 {"┮", {1, 0, 2, 1, 0}},
202 {"┯", {2, 0, 2, 1, 0}},
203 {"┰", {1, 0, 1, 2, 0}},
204 {"┱", {2, 0, 1, 2, 0}},
205 {"┲", {1, 0, 2, 2, 0}},
206 {"┳", {2, 0, 2, 2, 0}},
207
208 {"┴", {1, 1, 1, 0, 0}},
209 {"┵", {2, 1, 1, 0, 0}},
210 {"┶", {1, 1, 2, 0, 0}},
211 {"┷", {2, 1, 2, 0, 0}},
212 {"┸", {1, 2, 1, 0, 0}},
213 {"┹", {2, 2, 1, 0, 0}},
214 {"┺", {1, 2, 2, 0, 0}},
215 {"┻", {2, 2, 2, 0, 0}},
216
217 {"┼", {1, 1, 1, 1, 0}},
218 {"┽", {2, 1, 1, 1, 0}},
219 {"┾", {1, 1, 2, 1, 0}},
220 {"┿", {2, 1, 2, 1, 0}},
221 {"╀", {1, 2, 1, 1, 0}},
222 {"╁", {1, 1, 1, 2, 0}},
223 {"╂", {1, 2, 1, 2, 0}},
224 {"╃", {2, 2, 1, 1, 0}},
225 {"╄", {1, 2, 2, 1, 0}},
226 {"╅", {2, 1, 1, 2, 0}},
227 {"╆", {1, 1, 2, 2, 0}},
228 {"╇", {2, 2, 2, 1, 0}},
229 {"╈", {2, 1, 2, 2, 0}},
230 {"╉", {2, 2, 1, 2, 0}},
231 {"╊", {1, 2, 2, 2, 0}},
232 {"╋", {2, 2, 2, 2, 0}},
233
234 {"═", {3, 0, 3, 0, 0}},
235 {"║", {0, 3, 0, 3, 0}},
236
237 {"╒", {0, 0, 3, 1, 0}},
238 {"╓", {0, 0, 1, 3, 0}},
239 {"╔", {0, 0, 3, 3, 0}},
240
241 {"╕", {3, 0, 0, 1, 0}},
242 {"╖", {1, 0, 0, 3, 0}},
243 {"╗", {3, 0, 0, 3, 0}},
244
245 {"╘", {0, 1, 3, 0, 0}},
246 {"╙", {0, 3, 1, 0, 0}},
247 {"╚", {0, 3, 3, 0, 0}},
248
249 {"╛", {3, 1, 0, 0, 0}},
250 {"╜", {1, 3, 0, 0, 0}},
251 {"╝", {3, 3, 0, 0, 0}},
252
253 {"╞", {0, 1, 3, 1, 0}},
254 {"╟", {0, 3, 1, 3, 0}},
255 {"╠", {0, 3, 3, 3, 0}},
256
257 {"╡", {3, 1, 0, 1, 0}},
258 {"╢", {1, 3, 0, 3, 0}},
259 {"╣", {3, 3, 0, 3, 0}},
260
261 {"╤", {3, 0, 3, 1, 0}},
262 {"╥", {1, 0, 1, 3, 0}},
263 {"╦", {3, 0, 3, 3, 0}},
264
265 {"╧", {3, 1, 3, 0, 0}},
266 {"╨", {1, 3, 1, 0, 0}},
267 {"╩", {3, 3, 3, 0, 0}},
268
269 {"╪", {3, 1, 3, 1, 0}},
270 {"╫", {1, 3, 1, 3, 0}},
271 {"╬", {3, 3, 3, 3, 0}},
272
273 {"╭", {0, 0, 1, 1, 1}},
274 {"╮", {1, 0, 0, 1, 1}},
275 {"╯", {1, 1, 0, 0, 1}},
276 {"╰", {0, 1, 1, 0, 1}},
277
278 {"╴", {1, 0, 0, 0, 0}},
279 {"╵", {0, 1, 0, 0, 0}},
280 {"╶", {0, 0, 1, 0, 0}},
281 {"╷", {0, 0, 0, 1, 0}},
282
283 {"╸", {2, 0, 0, 0, 0}},
284 {"╹", {0, 2, 0, 0, 0}},
285 {"╺", {0, 0, 2, 0, 0}},
286 {"╻", {0, 0, 0, 2, 0}},
287
288 {"╼", {1, 0, 2, 0, 0}},
289 {"╽", {0, 1, 0, 2, 0}},
290 {"╾", {2, 0, 1, 0, 0}},
291 {"╿", {0, 2, 0, 1, 0}},
292};
293// clang-format on
294
295template <class A, class B>
296std::map<B, A> InvertMap(const std::map<A, B> input) {
297 std::map<B, A> output;
298 for (const auto& it : input) {
299 output[it.second] = it.first;
300 }
301 return output;
302}
303
304const std::map<TileEncoding, std::string> tile_encoding_inverse = // NOLINT
305 InvertMap(tile_encoding);
306
307void UpgradeLeftRight(std::string& left, std::string& right) {
308 const auto it_left = tile_encoding.find(left);
309 if (it_left == tile_encoding.end()) {
310 return;
311 }
312 const auto it_right = tile_encoding.find(right);
313 if (it_right == tile_encoding.end()) {
314 return;
315 }
316
317 if (it_left->second.right == 0 && it_right->second.left != 0) {
318 TileEncoding encoding_left = it_left->second;
319 encoding_left.right = it_right->second.left;
320 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
321 if (it_left_upgrade != tile_encoding_inverse.end()) {
322 left = it_left_upgrade->second;
323 }
324 }
325
326 if (it_right->second.left == 0 && it_left->second.right != 0) {
327 TileEncoding encoding_right = it_right->second;
328 encoding_right.left = it_left->second.right;
329 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
330 if (it_right_upgrade != tile_encoding_inverse.end()) {
331 right = it_right_upgrade->second;
332 }
333 }
334}
335
336void UpgradeTopDown(std::string& top, std::string& down) {
337 const auto it_top = tile_encoding.find(top);
338 if (it_top == tile_encoding.end()) {
339 return;
340 }
341 const auto it_down = tile_encoding.find(down);
342 if (it_down == tile_encoding.end()) {
343 return;
344 }
345
346 if (it_top->second.down == 0 && it_down->second.top != 0) {
347 TileEncoding encoding_top = it_top->second;
348 encoding_top.down = it_down->second.top;
349 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
350 if (it_top_down != tile_encoding_inverse.end()) {
351 top = it_top_down->second;
352 }
353 }
354
355 if (it_down->second.top == 0 && it_top->second.down != 0) {
356 TileEncoding encoding_down = it_down->second;
357 encoding_down.top = it_top->second.down;
358 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
359 if (it_down_top != tile_encoding_inverse.end()) {
360 down = it_down_top->second;
361 }
362 }
363}
364
365bool ShouldAttemptAutoMerge(Pixel& pixel) {
366 return pixel.automerge && pixel.character.size() == 3;
367}
368
369} // namespace
370
371bool Pixel::operator==(const Pixel& other) const {
372 return character == other.character && //
375 blink == other.blink && //
376 bold == other.bold && //
377 dim == other.dim && //
378 inverted == other.inverted && //
379 underlined == other.underlined && //
380 automerge == other.automerge; //
381}
382
383/// A fixed dimension.
384/// @see Fit
385/// @see Full
387 return {v, v};
388}
389
390/// Use the terminal dimensions.
391/// @see Fixed
392/// @see Fit
393Dimensions Dimension::Full() {
394 return Terminal::Size();
395}
396
397// static
398/// Create a screen with the given dimension along the x-axis and y-axis.
400 return {width.dimx, height.dimy};
401}
402
403// static
404/// Create a screen with the given dimension.
406 return {dimension.dimx, dimension.dimy};
407}
408
409Screen::Screen(int dimx, int dimy)
410 : stencil{0, dimx - 1, 0, dimy - 1},
411 dimx_(dimx),
412 dimy_(dimy),
413 pixels_(dimy, std::vector<Pixel>(dimx)) {
414#if defined(_WIN32)
415 // The placement of this call is a bit weird, however we can assume that
416 // anybody who instantiates a Screen object eventually wants to output
417 // something to the console.
418 // As we require UTF8 for all input/output operations we will just switch to
419 // UTF8 encoding here
420 SetConsoleOutputCP(CP_UTF8);
421 SetConsoleCP(CP_UTF8);
422 WindowsEmulateVT100Terminal();
423#endif
424}
425
426/// Produce a std::string that can be used to print the Screen on the terminal.
427/// Don't forget to flush stdout. Alternatively, you can use Screen::Print();
428std::string Screen::ToString() {
429 std::stringstream ss;
430
431 Pixel previous_pixel;
432 const Pixel final_pixel;
433
434 for (int y = 0; y < dimy_; ++y) {
435 if (y != 0) {
436 UpdatePixelStyle(ss, previous_pixel, final_pixel);
437 ss << "\r\n";
438 }
439 bool previous_fullwidth = false;
440 for (const auto& pixel : pixels_[y]) {
441 if (!previous_fullwidth) {
442 UpdatePixelStyle(ss, previous_pixel, pixel);
443 ss << pixel.character;
444 }
445 previous_fullwidth = (string_width(pixel.character) == 2);
446 }
447 }
448
449 UpdatePixelStyle(ss, previous_pixel, final_pixel);
450
451 return ss.str();
452}
453
455 std::cout << ToString() << '\0' << std::flush;
456}
457
458/// @brief Access a character a given position.
459/// @param x The character position along the x-axis.
460/// @param y The character position along the y-axis.
461std::string& Screen::at(int x, int y) {
462 return PixelAt(x, y).character;
463}
464
465/// @brief Access a Pixel at a given position.
466/// @param x The pixel position along the x-axis.
467/// @param y The pixel position along the y-axis.
468Pixel& Screen::PixelAt(int x, int y) {
469 return stencil.Contain(x, y) ? pixels_[y][x] : dev_null_pixel();
470}
471
472/// @brief Return a string to be printed in order to reset the cursor position
473/// to the beginning of the screen.
474///
475/// ```cpp
476/// std::string reset_position;
477/// while(true) {
478/// auto document = render();
479/// auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
480/// Render(screen, document);
481/// std::cout << reset_position << screen.ToString() << std::flush;
482/// reset_position = screen.ResetPosition();
483///
484/// using namespace std::chrono_literals;
485/// std::this_thread::sleep_for(0.01s);
486/// }
487/// ```
488///
489/// @return The string to print in order to reset the cursor position to the
490/// beginning.
491std::string Screen::ResetPosition(bool clear) const {
492 std::stringstream ss;
493 if (clear) {
494 ss << "\r"; // MOVE_LEFT;
495 ss << "\x1b[2K"; // CLEAR_SCREEN;
496 for (int y = 1; y < dimy_; ++y) {
497 ss << "\x1B[1A"; // MOVE_UP;
498 ss << "\x1B[2K"; // CLEAR_LINE;
499 }
500 } else {
501 ss << "\r"; // MOVE_LEFT;
502 for (int y = 1; y < dimy_; ++y) {
503 ss << "\x1B[1A"; // MOVE_UP;
504 }
505 }
506 return ss.str();
507}
508
509/// @brief Clear all the pixel from the screen.
511 for (auto& line : pixels_) {
512 for (auto& cell : line) {
513 cell = Pixel();
514 }
515 }
516 cursor_.x = dimx_ - 1;
517 cursor_.y = dimy_ - 1;
518}
519
520// clang-format off
522 // Merge box characters togethers.
523 for (int y = 0; y < dimy_; ++y) {
524 for (int x = 0; x < dimx_; ++x) {
525 // Box drawing character uses exactly 3 byte.
526 Pixel& cur = pixels_[y][x];
527 if (!ShouldAttemptAutoMerge(cur)) {
528 continue;
529 }
530
531 if (x > 0) {
532 Pixel& left = pixels_[y][x-1];
533 if (ShouldAttemptAutoMerge(left)) {
534 UpgradeLeftRight(left.character, cur.character);
535 }
536 }
537 if (y > 0) {
538 Pixel& top = pixels_[y-1][x];
539 if (ShouldAttemptAutoMerge(top)) {
540 UpgradeTopDown(top.character, cur.character);
541 }
542 }
543 }
544 }
545}
546
547// clang-format on
548
549} // namespace ftxui
550
551// Copyright 2020 Arthur Sonzogni. All rights reserved.
552// Use of this source code is governed by the MIT license that can be found in
553// the LICENSE file.
A rectangular grid of Pixel.
Definition screen.hpp:57
void ApplyShader()
Definition screen.cpp:521
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Definition screen.cpp:405
Pixel & PixelAt(int x, int y)
Access a Pixel at a given position.
Definition screen.cpp:468
std::string & at(int x, int y)
Access a character a given position.
Definition screen.cpp:461
Screen(int dimx, int dimy)
Definition screen.cpp:409
std::string ToString()
Definition screen.cpp:428
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:491
void Print()
Definition screen.cpp:454
Cursor cursor_
Definition screen.hpp:108
void Clear()
Clear all the pixel from the screen.
Definition screen.cpp:510
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:371
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