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