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