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