20#define WIN32_LEAN_AND_MEAN
28#if !defined(FTXUI_UNLIKELY)
29#if defined(COMPILER_GCC) || defined(__clang__)
30#define FTXUI_UNLIKELY(x) __builtin_expect(!!(x), 0)
32#define FTXUI_UNLIKELY(x) (x)
36#if !defined(FTXUI_LIKELY)
37#if defined(COMPILER_GCC) || defined(__clang__)
38#define FTXUI_LIKELY(x) __builtin_expect(!!(x), 1)
40#define FTXUI_LIKELY(x) (x)
49void WindowsEmulateVT100Terminal() {
50 static bool done =
false;
57 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
60 GetConsoleMode(stdout_handle, &out_mode);
63 const int enable_virtual_terminal_processing = 0x0004;
64 const int disable_newline_auto_return = 0x0008;
65 out_mode |= enable_virtual_terminal_processing;
66 out_mode |= disable_newline_auto_return;
68 SetConsoleMode(stdout_handle, out_mode);
73void UpdateCellStyle(
const Screen* screen,
80 ss += screen->Hyperlink(next.hyperlink);
85 if (
FTXUI_UNLIKELY((next.bold ^ prev.bold) | (next.dim ^ prev.dim))) {
87 if ((prev.bold && !next.bold) || (prev.dim && !next.dim)) {
100 next.underlined_double != prev.underlined_double)) {
101 ss += (next.underlined ?
"\x1B[4m"
102 : next.underlined_double ?
"\x1B[21m"
108 ss += (next.blink ?
"\x1B[5m"
114 ss += (next.inverted ?
"\x1B[7m"
120 ss += (next.italic ?
"\x1B[3m"
126 ss += (next.strikethrough ?
"\x1B[9m"
130 if (
FTXUI_UNLIKELY(next.foreground_color != prev.foreground_color ||
131 next.background_color != prev.background_color)) {
133 next.foreground_color.PrintTo(ss,
false);
136 next.background_color.PrintTo(ss,
true);
149 bool operator<(
const TileEncoding& other)
const {
150 if (
left < other.left) {
return true; }
151 if (
left > other.left) {
return false; }
152 if (
top < other.top) {
return true; }
153 if (
top > other.top) {
return false; }
154 if (
right < other.right) {
return true; }
155 if (
right > other.right) {
return false; }
156 if (
down < other.down) {
return true; }
157 if (
down > other.down) {
return false; }
158 if (
round < other.round) {
return true; }
159 if (
round > other.round) {
return false; }
166const std::map<std::string, TileEncoding> tile_encoding = {
167 {
"─", {1, 0, 1, 0, 0}},
168 {
"━", {2, 0, 2, 0, 0}},
169 {
"╍", {2, 0, 2, 0, 0}},
171 {
"│", {0, 1, 0, 1, 0}},
172 {
"┃", {0, 2, 0, 2, 0}},
173 {
"╏", {0, 2, 0, 2, 0}},
175 {
"┌", {0, 0, 1, 1, 0}},
176 {
"┍", {0, 0, 2, 1, 0}},
177 {
"┎", {0, 0, 1, 2, 0}},
178 {
"┏", {0, 0, 2, 2, 0}},
180 {
"┐", {1, 0, 0, 1, 0}},
181 {
"┑", {2, 0, 0, 1, 0}},
182 {
"┒", {1, 0, 0, 2, 0}},
183 {
"┓", {2, 0, 0, 2, 0}},
185 {
"└", {0, 1, 1, 0, 0}},
186 {
"┕", {0, 1, 2, 0, 0}},
187 {
"┖", {0, 2, 1, 0, 0}},
188 {
"┗", {0, 2, 2, 0, 0}},
190 {
"┘", {1, 1, 0, 0, 0}},
191 {
"┙", {2, 1, 0, 0, 0}},
192 {
"┚", {1, 2, 0, 0, 0}},
193 {
"┛", {2, 2, 0, 0, 0}},
195 {
"├", {0, 1, 1, 1, 0}},
196 {
"┝", {0, 1, 2, 1, 0}},
197 {
"┞", {0, 2, 1, 1, 0}},
198 {
"┟", {0, 1, 1, 2, 0}},
199 {
"┠", {0, 2, 1, 2, 0}},
200 {
"┡", {0, 2, 2, 1, 0}},
201 {
"┢", {0, 1, 2, 2, 0}},
202 {
"┣", {0, 2, 2, 2, 0}},
204 {
"┤", {1, 1, 0, 1, 0}},
205 {
"┥", {2, 1, 0, 1, 0}},
206 {
"┦", {1, 2, 0, 1, 0}},
207 {
"┧", {1, 1, 0, 2, 0}},
208 {
"┨", {1, 2, 0, 2, 0}},
209 {
"┩", {2, 2, 0, 1, 0}},
210 {
"┪", {2, 1, 0, 2, 0}},
211 {
"┫", {2, 2, 0, 2, 0}},
213 {
"┬", {1, 0, 1, 1, 0}},
214 {
"┭", {2, 0, 1, 1, 0}},
215 {
"┮", {1, 0, 2, 1, 0}},
216 {
"┯", {2, 0, 2, 1, 0}},
217 {
"┰", {1, 0, 1, 2, 0}},
218 {
"┱", {2, 0, 1, 2, 0}},
219 {
"┲", {1, 0, 2, 2, 0}},
220 {
"┳", {2, 0, 2, 2, 0}},
222 {
"┴", {1, 1, 1, 0, 0}},
223 {
"┵", {2, 1, 1, 0, 0}},
224 {
"┶", {1, 1, 2, 0, 0}},
225 {
"┷", {2, 1, 2, 0, 0}},
226 {
"┸", {1, 2, 1, 0, 0}},
227 {
"┹", {2, 2, 1, 0, 0}},
228 {
"┺", {1, 2, 2, 0, 0}},
229 {
"┻", {2, 2, 2, 0, 0}},
231 {
"┼", {1, 1, 1, 1, 0}},
232 {
"┽", {2, 1, 1, 1, 0}},
233 {
"┾", {1, 1, 2, 1, 0}},
234 {
"┿", {2, 1, 2, 1, 0}},
235 {
"╀", {1, 2, 1, 1, 0}},
236 {
"╁", {1, 1, 1, 2, 0}},
237 {
"╂", {1, 2, 1, 2, 0}},
238 {
"╃", {2, 2, 1, 1, 0}},
239 {
"╄", {1, 2, 2, 1, 0}},
240 {
"╅", {2, 1, 1, 2, 0}},
241 {
"╆", {1, 1, 2, 2, 0}},
242 {
"╇", {2, 2, 2, 1, 0}},
243 {
"╈", {2, 1, 2, 2, 0}},
244 {
"╉", {2, 2, 1, 2, 0}},
245 {
"╊", {1, 2, 2, 2, 0}},
246 {
"╋", {2, 2, 2, 2, 0}},
248 {
"═", {3, 0, 3, 0, 0}},
249 {
"║", {0, 3, 0, 3, 0}},
251 {
"╒", {0, 0, 3, 1, 0}},
252 {
"╓", {0, 0, 1, 3, 0}},
253 {
"╔", {0, 0, 3, 3, 0}},
255 {
"╕", {3, 0, 0, 1, 0}},
256 {
"╖", {1, 0, 0, 3, 0}},
257 {
"╗", {3, 0, 0, 3, 0}},
259 {
"╘", {0, 1, 3, 0, 0}},
260 {
"╙", {0, 3, 1, 0, 0}},
261 {
"╚", {0, 3, 3, 0, 0}},
263 {
"╛", {3, 1, 0, 0, 0}},
264 {
"╜", {1, 3, 0, 0, 0}},
265 {
"╝", {3, 3, 0, 0, 0}},
267 {
"╞", {0, 1, 3, 1, 0}},
268 {
"╟", {0, 3, 1, 3, 0}},
269 {
"╠", {0, 3, 3, 3, 0}},
271 {
"╡", {3, 1, 0, 1, 0}},
272 {
"╢", {1, 3, 0, 3, 0}},
273 {
"╣", {3, 3, 0, 3, 0}},
275 {
"╤", {3, 0, 3, 1, 0}},
276 {
"╥", {1, 0, 1, 3, 0}},
277 {
"╦", {3, 0, 3, 3, 0}},
279 {
"╧", {3, 1, 3, 0, 0}},
280 {
"╨", {1, 3, 1, 0, 0}},
281 {
"╩", {3, 3, 3, 0, 0}},
283 {
"╪", {3, 1, 3, 1, 0}},
284 {
"╫", {1, 3, 1, 3, 0}},
285 {
"╬", {3, 3, 3, 3, 0}},
287 {
"╭", {0, 0, 1, 1, 1}},
288 {
"╮", {1, 0, 0, 1, 1}},
289 {
"╯", {1, 1, 0, 0, 1}},
290 {
"╰", {0, 1, 1, 0, 1}},
292 {
"╴", {1, 0, 0, 0, 0}},
293 {
"╵", {0, 1, 0, 0, 0}},
294 {
"╶", {0, 0, 1, 0, 0}},
295 {
"╷", {0, 0, 0, 1, 0}},
297 {
"╸", {2, 0, 0, 0, 0}},
298 {
"╹", {0, 2, 0, 0, 0}},
299 {
"╺", {0, 0, 2, 0, 0}},
300 {
"╻", {0, 0, 0, 2, 0}},
302 {
"╼", {1, 0, 2, 0, 0}},
303 {
"╽", {0, 1, 0, 2, 0}},
304 {
"╾", {2, 0, 1, 0, 0}},
305 {
"╿", {0, 2, 0, 1, 0}},
309template <
class A,
class B>
310std::map<B, A> InvertMap(
const std::map<A, B>& input) {
311 std::map<B, A> output;
312 for (
const auto& it : input) {
313 output[it.second] = it.first;
318const std::map<TileEncoding, std::string> tile_encoding_inverse =
319 InvertMap(tile_encoding);
321void UpgradeLeftRight(std::string&
left, std::string&
right) {
322 const auto it_left = tile_encoding.find(
left);
323 if (it_left == tile_encoding.end()) {
326 const auto it_right = tile_encoding.find(
right);
327 if (it_right == tile_encoding.end()) {
331 if (it_left->second.right == 0 && it_right->second.left != 0) {
332 TileEncoding encoding_left = it_left->second;
333 encoding_left.right = it_right->second.left;
334 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
335 if (it_left_upgrade != tile_encoding_inverse.end()) {
336 left = it_left_upgrade->second;
340 if (it_right->second.left == 0 && it_left->second.right != 0) {
341 TileEncoding encoding_right = it_right->second;
342 encoding_right.left = it_left->second.right;
343 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
344 if (it_right_upgrade != tile_encoding_inverse.end()) {
345 right = it_right_upgrade->second;
350void UpgradeTopDown(std::string&
top, std::string&
down) {
351 const auto it_top = tile_encoding.find(
top);
352 if (it_top == tile_encoding.end()) {
355 const auto it_down = tile_encoding.find(
down);
356 if (it_down == tile_encoding.end()) {
360 if (it_top->second.down == 0 && it_down->second.top != 0) {
361 TileEncoding encoding_top = it_top->second;
362 encoding_top.down = it_down->second.top;
363 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
364 if (it_top_down != tile_encoding_inverse.end()) {
365 top = it_top_down->second;
369 if (it_down->second.top == 0 && it_top->second.down != 0) {
370 TileEncoding encoding_down = it_down->second;
371 encoding_down.top = it_top->second.down;
372 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
373 if (it_down_top != tile_encoding_inverse.end()) {
374 down = it_down_top->second;
379bool ShouldAttemptAutoMerge(Cell& cell) {
380 return cell.automerge && cell.character.size() == 3;
388Dimensions Dimension::Fixed(
int v) {
395Dimensions Dimension::Full() {
401Screen Screen::Create(Dimensions width, Dimensions height) {
402 return {width.dimx, height.dimy};
407Screen Screen::Create(Dimensions dimension) {
408 return {dimension.dimx, dimension.dimy};
411Screen::Screen(
int dimx,
int dimy) : Surface{dimx, dimy} {
418 SetConsoleOutputCP(CP_UTF8);
419 SetConsoleCP(CP_UTF8);
420 WindowsEmulateVT100Terminal();
428std::string Screen::ToString()
const {
431 ss.reserve(
static_cast<size_t>(dimx_) *
static_cast<size_t>(dimy_) * 30);
439void Screen::ToString(std::string& ss)
const {
440 const Cell default_cell;
441 const Cell* previous_cell_ref = &default_cell;
443 for (
int y = 0;
y < dimy_; ++
y) {
446 UpdateCellStyle(
this, ss, *previous_cell_ref, default_cell);
447 previous_cell_ref = &default_cell;
452 bool previous_fullwidth =
false;
454 const Cell* line_start = &FastCellAt(0, y);
455 const Cell* line_end = line_start + dimx_;
456 for (
const Cell* it = line_start; it != line_end; ++it) {
457 const auto& cell = *it;
458 if (!previous_fullwidth) {
459 UpdateCellStyle(
this, ss, *previous_cell_ref, cell);
460 previous_cell_ref = &cell;
461 if (cell.character.empty()) {
464 ss += cell.character;
467 if (cell.character.size() <= 1) {
468 previous_fullwidth =
false;
470 previous_fullwidth = (string_width(cell.character) == 2);
477 UpdateCellStyle(
this, ss, *previous_cell_ref, default_cell);
481void Screen::Print()
const {
482 std::cout << ToString() <<
'\0' << std::flush;
504std::string Screen::ResetPosition(
bool clear)
const {
506 ss.reserve(
static_cast<size_t>(dimy_) * 12);
507 ResetPosition(ss, clear);
515void Screen::ResetPosition(std::string& ss,
bool clear)
const {
519 for (
int y = 1;
y < dimy_; ++
y) {
525 for (
int y = 1;
y < dimy_; ++
y) {
532void Screen::Clear() {
535 cursor_.x = dimx_ - 1;
536 cursor_.y = dimy_ - 1;
544void Screen::ApplyShader() {
546 for (
int y = 0;
y < dimy_; ++
y) {
547 for (
int x = 0; x < dimx_; ++x) {
549 Cell& cur = FastCellAt(x, y);
550 if (!ShouldAttemptAutoMerge(cur)) {
555 Cell&
left = FastCellAt(x - 1, y);
556 if (ShouldAttemptAutoMerge(
left)) {
557 UpgradeLeftRight(
left.character, cur.character);
561 Cell&
top = FastCellAt(x, y - 1);
562 if (ShouldAttemptAutoMerge(
top)) {
563 UpgradeTopDown(
top.character, cur.character);
571std::uint8_t Screen::RegisterHyperlink(std::string_view link) {
572 for (std::size_t i = 0; i < hyperlinks_.size(); ++i) {
573 if (hyperlinks_[i] == link) {
577 if (hyperlinks_.size() == std::numeric_limits<std::uint8_t>::max()) {
580 hyperlinks_.emplace_back(link);
581 return hyperlinks_.size() - 1;
584const std::string& Screen::Hyperlink(std::uint8_t
id)
const {
585 if (
id >= hyperlinks_.size()) {
586 return hyperlinks_[0];
588 return hyperlinks_[id];
593const Screen::SelectionStyle& Screen::GetSelectionStyle()
const {
594 return selection_style_;
599void Screen::SetSelectionStyle(SelectionStyle decorator) {
600 selection_style_ = std::move(decorator);
Dimensions Size()
Get the terminal size.
The FTXUI ftxui:: namespace.
#define FTXUI_UNLIKELY(x)