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