21#define WIN32_LEAN_AND_MEAN
29#if !defined(FTXUI_UNLIKELY)
30#if defined(COMPILER_GCC) || defined(__clang__)
31#define FTXUI_UNLIKELY(x) __builtin_expect(!!(x), 0)
33#define FTXUI_UNLIKELY(x) (x)
37#if !defined(FTXUI_LIKELY)
38#if defined(COMPILER_GCC) || defined(__clang__)
39#define FTXUI_LIKELY(x) __builtin_expect(!!(x), 1)
41#define FTXUI_LIKELY(x) (x)
50void WindowsEmulateVT100Terminal() {
51 static bool done =
false;
58 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
61 if (!GetConsoleMode(stdout_handle, &out_mode)) {
69 const int enable_virtual_terminal_processing = 0x0004;
70 const int disable_newline_auto_return = 0x0008;
71 out_mode |= enable_virtual_terminal_processing;
72 out_mode |= disable_newline_auto_return;
74 SetConsoleMode(stdout_handle, out_mode);
90void UpdateCellStyle(
const Screen* screen,
93 ColorSequence& last_colors,
99 ss += screen->Hyperlink(next.hyperlink);
104 if (
FTXUI_UNLIKELY((next.bold ^ prev.bold) | (next.dim ^ prev.dim))) {
106 if ((prev.bold && !next.bold) || (prev.dim && !next.dim)) {
119 next.underlined_double != prev.underlined_double)) {
120 ss += (next.underlined ?
"\x1B[4m"
121 : next.underlined_double ?
"\x1B[21m"
127 ss += (next.blink ?
"\x1B[5m"
133 ss += (next.inverted ?
"\x1B[7m"
139 ss += (next.italic ?
"\x1B[3m"
145 ss += (next.strikethrough ?
"\x1B[9m"
149 if (
FTXUI_UNLIKELY(next.foreground_color != prev.foreground_color ||
150 next.background_color != prev.background_color)) {
151 const size_t begin = ss.size();
153 next.foreground_color.PrintTo(ss,
false, color_support);
156 next.background_color.PrintTo(ss,
true, color_support);
165 const size_t size = ss.size() -
begin;
166 if (size == last_colors.size &&
167 ss.compare(
begin, size, ss, last_colors.begin, size) == 0) {
170 last_colors.begin =
begin;
171 last_colors.size =
size;
185 bool operator<(
const TileEncoding& other)
const {
186 if (
left < other.left) {
return true; }
187 if (
left > other.left) {
return false; }
188 if (
top < other.top) {
return true; }
189 if (
top > other.top) {
return false; }
190 if (
right < other.right) {
return true; }
191 if (
right > other.right) {
return false; }
192 if (
down < other.down) {
return true; }
193 if (
down > other.down) {
return false; }
194 if (
round < other.round) {
return true; }
195 if (
round > other.round) {
return false; }
202const std::map<std::string, TileEncoding> tile_encoding = {
203 {
"─", {1, 0, 1, 0, 0}},
204 {
"━", {2, 0, 2, 0, 0}},
205 {
"╍", {2, 0, 2, 0, 0}},
207 {
"│", {0, 1, 0, 1, 0}},
208 {
"┃", {0, 2, 0, 2, 0}},
209 {
"╏", {0, 2, 0, 2, 0}},
211 {
"┌", {0, 0, 1, 1, 0}},
212 {
"┍", {0, 0, 2, 1, 0}},
213 {
"┎", {0, 0, 1, 2, 0}},
214 {
"┏", {0, 0, 2, 2, 0}},
216 {
"┐", {1, 0, 0, 1, 0}},
217 {
"┑", {2, 0, 0, 1, 0}},
218 {
"┒", {1, 0, 0, 2, 0}},
219 {
"┓", {2, 0, 0, 2, 0}},
221 {
"└", {0, 1, 1, 0, 0}},
222 {
"┕", {0, 1, 2, 0, 0}},
223 {
"┖", {0, 2, 1, 0, 0}},
224 {
"┗", {0, 2, 2, 0, 0}},
226 {
"┘", {1, 1, 0, 0, 0}},
227 {
"┙", {2, 1, 0, 0, 0}},
228 {
"┚", {1, 2, 0, 0, 0}},
229 {
"┛", {2, 2, 0, 0, 0}},
231 {
"├", {0, 1, 1, 1, 0}},
232 {
"┝", {0, 1, 2, 1, 0}},
233 {
"┞", {0, 2, 1, 1, 0}},
234 {
"┟", {0, 1, 1, 2, 0}},
235 {
"┠", {0, 2, 1, 2, 0}},
236 {
"┡", {0, 2, 2, 1, 0}},
237 {
"┢", {0, 1, 2, 2, 0}},
238 {
"┣", {0, 2, 2, 2, 0}},
240 {
"┤", {1, 1, 0, 1, 0}},
241 {
"┥", {2, 1, 0, 1, 0}},
242 {
"┦", {1, 2, 0, 1, 0}},
243 {
"┧", {1, 1, 0, 2, 0}},
244 {
"┨", {1, 2, 0, 2, 0}},
245 {
"┩", {2, 2, 0, 1, 0}},
246 {
"┪", {2, 1, 0, 2, 0}},
247 {
"┫", {2, 2, 0, 2, 0}},
249 {
"┬", {1, 0, 1, 1, 0}},
250 {
"┭", {2, 0, 1, 1, 0}},
251 {
"┮", {1, 0, 2, 1, 0}},
252 {
"┯", {2, 0, 2, 1, 0}},
253 {
"┰", {1, 0, 1, 2, 0}},
254 {
"┱", {2, 0, 1, 2, 0}},
255 {
"┲", {1, 0, 2, 2, 0}},
256 {
"┳", {2, 0, 2, 2, 0}},
258 {
"┴", {1, 1, 1, 0, 0}},
259 {
"┵", {2, 1, 1, 0, 0}},
260 {
"┶", {1, 1, 2, 0, 0}},
261 {
"┷", {2, 1, 2, 0, 0}},
262 {
"┸", {1, 2, 1, 0, 0}},
263 {
"┹", {2, 2, 1, 0, 0}},
264 {
"┺", {1, 2, 2, 0, 0}},
265 {
"┻", {2, 2, 2, 0, 0}},
267 {
"┼", {1, 1, 1, 1, 0}},
268 {
"┽", {2, 1, 1, 1, 0}},
269 {
"┾", {1, 1, 2, 1, 0}},
270 {
"┿", {2, 1, 2, 1, 0}},
271 {
"╀", {1, 2, 1, 1, 0}},
272 {
"╁", {1, 1, 1, 2, 0}},
273 {
"╂", {1, 2, 1, 2, 0}},
274 {
"╃", {2, 2, 1, 1, 0}},
275 {
"╄", {1, 2, 2, 1, 0}},
276 {
"╅", {2, 1, 1, 2, 0}},
277 {
"╆", {1, 1, 2, 2, 0}},
278 {
"╇", {2, 2, 2, 1, 0}},
279 {
"╈", {2, 1, 2, 2, 0}},
280 {
"╉", {2, 2, 1, 2, 0}},
281 {
"╊", {1, 2, 2, 2, 0}},
282 {
"╋", {2, 2, 2, 2, 0}},
284 {
"═", {3, 0, 3, 0, 0}},
285 {
"║", {0, 3, 0, 3, 0}},
287 {
"╒", {0, 0, 3, 1, 0}},
288 {
"╓", {0, 0, 1, 3, 0}},
289 {
"╔", {0, 0, 3, 3, 0}},
291 {
"╕", {3, 0, 0, 1, 0}},
292 {
"╖", {1, 0, 0, 3, 0}},
293 {
"╗", {3, 0, 0, 3, 0}},
295 {
"╘", {0, 1, 3, 0, 0}},
296 {
"╙", {0, 3, 1, 0, 0}},
297 {
"╚", {0, 3, 3, 0, 0}},
299 {
"╛", {3, 1, 0, 0, 0}},
300 {
"╜", {1, 3, 0, 0, 0}},
301 {
"╝", {3, 3, 0, 0, 0}},
303 {
"╞", {0, 1, 3, 1, 0}},
304 {
"╟", {0, 3, 1, 3, 0}},
305 {
"╠", {0, 3, 3, 3, 0}},
307 {
"╡", {3, 1, 0, 1, 0}},
308 {
"╢", {1, 3, 0, 3, 0}},
309 {
"╣", {3, 3, 0, 3, 0}},
311 {
"╤", {3, 0, 3, 1, 0}},
312 {
"╥", {1, 0, 1, 3, 0}},
313 {
"╦", {3, 0, 3, 3, 0}},
315 {
"╧", {3, 1, 3, 0, 0}},
316 {
"╨", {1, 3, 1, 0, 0}},
317 {
"╩", {3, 3, 3, 0, 0}},
319 {
"╪", {3, 1, 3, 1, 0}},
320 {
"╫", {1, 3, 1, 3, 0}},
321 {
"╬", {3, 3, 3, 3, 0}},
323 {
"╭", {0, 0, 1, 1, 1}},
324 {
"╮", {1, 0, 0, 1, 1}},
325 {
"╯", {1, 1, 0, 0, 1}},
326 {
"╰", {0, 1, 1, 0, 1}},
328 {
"╴", {1, 0, 0, 0, 0}},
329 {
"╵", {0, 1, 0, 0, 0}},
330 {
"╶", {0, 0, 1, 0, 0}},
331 {
"╷", {0, 0, 0, 1, 0}},
333 {
"╸", {2, 0, 0, 0, 0}},
334 {
"╹", {0, 2, 0, 0, 0}},
335 {
"╺", {0, 0, 2, 0, 0}},
336 {
"╻", {0, 0, 0, 2, 0}},
338 {
"╼", {1, 0, 2, 0, 0}},
339 {
"╽", {0, 1, 0, 2, 0}},
340 {
"╾", {2, 0, 1, 0, 0}},
341 {
"╿", {0, 2, 0, 1, 0}},
345template <
class A,
class B>
346std::map<B, A> InvertMap(
const std::map<A, B>& input) {
347 std::map<B, A> output;
348 for (
const auto& it : input) {
349 output[it.second] = it.first;
354const std::map<TileEncoding, std::string> tile_encoding_inverse =
355 InvertMap(tile_encoding);
357void UpgradeLeftRight(std::string&
left, std::string&
right) {
358 const auto it_left = tile_encoding.find(
left);
359 if (it_left == tile_encoding.end()) {
362 const auto it_right = tile_encoding.find(
right);
363 if (it_right == tile_encoding.end()) {
367 if (it_left->second.right == 0 && it_right->second.left != 0) {
368 TileEncoding encoding_left = it_left->second;
369 encoding_left.right = it_right->second.left;
370 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
371 if (it_left_upgrade != tile_encoding_inverse.end()) {
372 left = it_left_upgrade->second;
376 if (it_right->second.left == 0 && it_left->second.right != 0) {
377 TileEncoding encoding_right = it_right->second;
378 encoding_right.left = it_left->second.right;
379 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
380 if (it_right_upgrade != tile_encoding_inverse.end()) {
381 right = it_right_upgrade->second;
386void UpgradeTopDown(std::string&
top, std::string&
down) {
387 const auto it_top = tile_encoding.find(
top);
388 if (it_top == tile_encoding.end()) {
391 const auto it_down = tile_encoding.find(
down);
392 if (it_down == tile_encoding.end()) {
396 if (it_top->second.down == 0 && it_down->second.top != 0) {
397 TileEncoding encoding_top = it_top->second;
398 encoding_top.down = it_down->second.top;
399 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
400 if (it_top_down != tile_encoding_inverse.end()) {
401 top = it_top_down->second;
405 if (it_down->second.top == 0 && it_top->second.down != 0) {
406 TileEncoding encoding_down = it_down->second;
407 encoding_down.top = it_top->second.down;
408 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
409 if (it_down_top != tile_encoding_inverse.end()) {
410 down = it_down_top->second;
415bool ShouldAttemptAutoMerge(Cell& cell) {
416 return cell.automerge && cell.character.size() == 3;
420bool SameCell(
const Screen& screen_a,
422 const Screen& screen_b,
424 return a.character == b.character &&
425 a.foreground_color == b.foreground_color &&
426 a.background_color == b.background_color &&
427 a.blink == b.blink &&
430 a.italic == b.italic &&
431 a.inverted == b.inverted &&
432 a.underlined == b.underlined &&
433 a.underlined_double == b.underlined_double &&
434 a.strikethrough == b.strikethrough &&
435 screen_a.Hyperlink(a.hyperlink) == screen_b.Hyperlink(b.hyperlink);
440void HiddenCells(
const Cell* row,
int dimx, std::vector<bool>& hidden) {
441 hidden.assign(
static_cast<size_t>(dimx),
false);
442 bool previous_fullwidth =
false;
443 for (
int x = 0; x < dimx; ++x) {
444 hidden[x] = previous_fullwidth;
445 const std::string& character = row[x].character;
447 !hidden[x] && character.size() > 1 &&
string_width(character) == 2;
456Dimensions Dimension::Fixed(
int v) {
463Dimensions Dimension::Full() {
469Screen Screen::Create(Dimensions width, Dimensions height) {
470 return {width.dimx, height.dimy};
475Screen Screen::Create(Dimensions dimension) {
476 return {dimension.dimx, dimension.dimy};
479Screen::Screen(
int dimx,
int dimy) : Surface{dimx, dimy} {
486 SetConsoleOutputCP(CP_UTF8);
487 SetConsoleCP(CP_UTF8);
488 WindowsEmulateVT100Terminal();
496std::string Screen::ToString()
const {
499 ss.reserve(
static_cast<size_t>(dimx_) *
static_cast<size_t>(dimy_) * 30);
507void Screen::ToString(std::string& ss)
const {
508 const Cell default_cell;
509 const Cell* previous_cell_ref = &default_cell;
510 const Terminal::Color color_support = Terminal::ColorSupport();
511 ColorSequence last_colors;
513 for (
int y = 0;
y < dimy_; ++
y) {
516 UpdateCellStyle(
this, ss, color_support, last_colors, *previous_cell_ref,
518 previous_cell_ref = &default_cell;
523 bool previous_fullwidth =
false;
525 const Cell* line_start = &FastCellAt(0, y);
526 const Cell* line_end = line_start + dimx_;
527 for (
const Cell* it = line_start; it != line_end; ++it) {
528 const auto& cell = *it;
529 if (!previous_fullwidth) {
530 UpdateCellStyle(
this, ss, color_support, last_colors,
531 *previous_cell_ref, cell);
532 previous_cell_ref = &cell;
533 if (cell.character.empty()) {
536 ss += cell.character;
540 previous_fullwidth = !previous_fullwidth && cell.character.size() > 1 &&
541 string_width(cell.character) == 2;
547 UpdateCellStyle(
this, ss, color_support, last_colors, *previous_cell_ref,
556void Screen::ToString(std::string& ss,
const Screen& previous)
const {
557 if (previous.dimx_ != dimx_ || previous.dimy_ != dimy_ || dimx_ == 0 ||
563 const Cell default_cell;
564 const Terminal::Color color_support = Terminal::ColorSupport();
565 ColorSequence last_colors;
566 std::vector<bool> hidden;
567 std::vector<bool> previous_hidden;
570 for (
int y = 0;
y < dimy_; ++
y) {
571 const Cell* row = &FastCellAt(0, y);
572 const Cell* previous_row = &previous.FastCellAt(0, y);
573 auto same = [&](
int x) {
574 return SameCell(*
this, row[x], previous, previous_row[x]);
578 while (first < dimx_ && same(first)) {
581 int last = dimx_ - 1;
582 if (y != dimy_ - 1) {
583 if (first == dimx_) {
589 }
else if (first == dimx_) {
596 HiddenCells(row, dimx_, hidden);
597 HiddenCells(previous_row, dimx_, previous_hidden);
598 while (first > 0 && (hidden[first] || previous_hidden[first])) {
601 while (last + 1 < dimx_ &&
602 (hidden[last + 1] || previous_hidden[last + 1])) {
608 ss +=
"\x1B[" + std::to_string(y - cursor_y) +
"B";
613 ss +=
"\x1B[" + std::to_string(first) +
"C";
616 const Cell* previous_cell_ref = &default_cell;
617 for (
int x = first; x <= last; ++x) {
621 const Cell& cell = row[x];
622 UpdateCellStyle(
this, ss, color_support, last_colors, *previous_cell_ref,
624 previous_cell_ref = &cell;
625 if (cell.character.empty()) {
628 ss += cell.character;
631 UpdateCellStyle(
this, ss, color_support, last_colors, *previous_cell_ref,
637void Screen::Print()
const {
638 std::cout << ToString() <<
'\0' << std::flush;
660std::string Screen::ResetPosition(
bool clear)
const {
662 ss.reserve(
static_cast<size_t>(dimy_) * 12);
663 ResetPosition(ss, clear);
671void Screen::ResetPosition(std::string& ss,
bool clear)
const {
678 for (
int y = 1;
y < dimy_; ++
y) {
688 ss +=
"\x1B[" + std::to_string(dimy_ - 1) +
"A";
694void Screen::Clear() {
697 cursor_.x = dimx_ - 1;
698 cursor_.y = dimy_ - 1;
706void Screen::ApplyShader() {
708 for (
int y = 0;
y < dimy_; ++
y) {
709 for (
int x = 0; x < dimx_; ++x) {
711 Cell& cur = FastCellAt(x, y);
712 if (!ShouldAttemptAutoMerge(cur)) {
717 Cell&
left = FastCellAt(x - 1, y);
718 if (ShouldAttemptAutoMerge(
left)) {
719 UpgradeLeftRight(
left.character, cur.character);
723 Cell&
top = FastCellAt(x, y - 1);
724 if (ShouldAttemptAutoMerge(
top)) {
725 UpgradeTopDown(
top.character, cur.character);
733std::uint8_t Screen::RegisterHyperlink(std::string_view link) {
734 for (std::size_t i = 0; i < hyperlinks_.size(); ++i) {
735 if (hyperlinks_[i] == link) {
739 if (hyperlinks_.size() == std::numeric_limits<std::uint8_t>::max()) {
742 hyperlinks_.emplace_back(link);
743 return hyperlinks_.size() - 1;
746const std::string& Screen::Hyperlink(std::uint8_t
id)
const {
747 if (
id >= hyperlinks_.size()) {
748 return hyperlinks_[0];
750 return hyperlinks_[id];
755const Screen::SelectionStyle& Screen::GetSelectionStyle()
const {
756 return selection_style_;
761void Screen::SetSelectionStyle(SelectionStyle decorator) {
762 selection_style_ = std::move(decorator);
765void Screen::Reserved1() {}
766void Screen::Reserved2() {}
767void Screen::Reserved3() {}
768void Screen::Reserved4() {}
769void Screen::Reserved5() {}
770void Screen::Reserved6() {}
771void Screen::Reserved7() {}
772void Screen::Reserved8() {}
Color
Color is an enumeration that represents the color support of the terminal.
Dimensions Size()
Get the terminal size.
The FTXUI ftxui:: namespace.
int string_width(std::string_view input)
#define FTXUI_UNLIKELY(x)