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