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 if (!GetConsoleMode(stdout_handle, &out_mode)) {
68 const int enable_virtual_terminal_processing = 0x0004;
69 const int disable_newline_auto_return = 0x0008;
70 out_mode |= enable_virtual_terminal_processing;
71 out_mode |= disable_newline_auto_return;
73 SetConsoleMode(stdout_handle, out_mode);
78void UpdateCellStyle(
const Screen* screen,
85 ss += screen->Hyperlink(next.hyperlink);
90 if (
FTXUI_UNLIKELY((next.bold ^ prev.bold) | (next.dim ^ prev.dim))) {
92 if ((prev.bold && !next.bold) || (prev.dim && !next.dim)) {
105 next.underlined_double != prev.underlined_double)) {
106 ss += (next.underlined ?
"\x1B[4m"
107 : next.underlined_double ?
"\x1B[21m"
113 ss += (next.blink ?
"\x1B[5m"
119 ss += (next.inverted ?
"\x1B[7m"
125 ss += (next.italic ?
"\x1B[3m"
131 ss += (next.strikethrough ?
"\x1B[9m"
135 if (
FTXUI_UNLIKELY(next.foreground_color != prev.foreground_color ||
136 next.background_color != prev.background_color)) {
138 next.foreground_color.PrintTo(ss,
false);
141 next.background_color.PrintTo(ss,
true);
154 bool operator<(
const TileEncoding& other)
const {
155 if (
left < other.left) {
return true; }
156 if (
left > other.left) {
return false; }
157 if (
top < other.top) {
return true; }
158 if (
top > other.top) {
return false; }
159 if (
right < other.right) {
return true; }
160 if (
right > other.right) {
return false; }
161 if (
down < other.down) {
return true; }
162 if (
down > other.down) {
return false; }
163 if (
round < other.round) {
return true; }
164 if (
round > other.round) {
return false; }
171const std::map<std::string, TileEncoding> tile_encoding = {
172 {
"─", {1, 0, 1, 0, 0}},
173 {
"━", {2, 0, 2, 0, 0}},
174 {
"╍", {2, 0, 2, 0, 0}},
176 {
"│", {0, 1, 0, 1, 0}},
177 {
"┃", {0, 2, 0, 2, 0}},
178 {
"╏", {0, 2, 0, 2, 0}},
180 {
"┌", {0, 0, 1, 1, 0}},
181 {
"┍", {0, 0, 2, 1, 0}},
182 {
"┎", {0, 0, 1, 2, 0}},
183 {
"┏", {0, 0, 2, 2, 0}},
185 {
"┐", {1, 0, 0, 1, 0}},
186 {
"┑", {2, 0, 0, 1, 0}},
187 {
"┒", {1, 0, 0, 2, 0}},
188 {
"┓", {2, 0, 0, 2, 0}},
190 {
"└", {0, 1, 1, 0, 0}},
191 {
"┕", {0, 1, 2, 0, 0}},
192 {
"┖", {0, 2, 1, 0, 0}},
193 {
"┗", {0, 2, 2, 0, 0}},
195 {
"┘", {1, 1, 0, 0, 0}},
196 {
"┙", {2, 1, 0, 0, 0}},
197 {
"┚", {1, 2, 0, 0, 0}},
198 {
"┛", {2, 2, 0, 0, 0}},
200 {
"├", {0, 1, 1, 1, 0}},
201 {
"┝", {0, 1, 2, 1, 0}},
202 {
"┞", {0, 2, 1, 1, 0}},
203 {
"┟", {0, 1, 1, 2, 0}},
204 {
"┠", {0, 2, 1, 2, 0}},
205 {
"┡", {0, 2, 2, 1, 0}},
206 {
"┢", {0, 1, 2, 2, 0}},
207 {
"┣", {0, 2, 2, 2, 0}},
209 {
"┤", {1, 1, 0, 1, 0}},
210 {
"┥", {2, 1, 0, 1, 0}},
211 {
"┦", {1, 2, 0, 1, 0}},
212 {
"┧", {1, 1, 0, 2, 0}},
213 {
"┨", {1, 2, 0, 2, 0}},
214 {
"┩", {2, 2, 0, 1, 0}},
215 {
"┪", {2, 1, 0, 2, 0}},
216 {
"┫", {2, 2, 0, 2, 0}},
218 {
"┬", {1, 0, 1, 1, 0}},
219 {
"┭", {2, 0, 1, 1, 0}},
220 {
"┮", {1, 0, 2, 1, 0}},
221 {
"┯", {2, 0, 2, 1, 0}},
222 {
"┰", {1, 0, 1, 2, 0}},
223 {
"┱", {2, 0, 1, 2, 0}},
224 {
"┲", {1, 0, 2, 2, 0}},
225 {
"┳", {2, 0, 2, 2, 0}},
227 {
"┴", {1, 1, 1, 0, 0}},
228 {
"┵", {2, 1, 1, 0, 0}},
229 {
"┶", {1, 1, 2, 0, 0}},
230 {
"┷", {2, 1, 2, 0, 0}},
231 {
"┸", {1, 2, 1, 0, 0}},
232 {
"┹", {2, 2, 1, 0, 0}},
233 {
"┺", {1, 2, 2, 0, 0}},
234 {
"┻", {2, 2, 2, 0, 0}},
236 {
"┼", {1, 1, 1, 1, 0}},
237 {
"┽", {2, 1, 1, 1, 0}},
238 {
"┾", {1, 1, 2, 1, 0}},
239 {
"┿", {2, 1, 2, 1, 0}},
240 {
"╀", {1, 2, 1, 1, 0}},
241 {
"╁", {1, 1, 1, 2, 0}},
242 {
"╂", {1, 2, 1, 2, 0}},
243 {
"╃", {2, 2, 1, 1, 0}},
244 {
"╄", {1, 2, 2, 1, 0}},
245 {
"╅", {2, 1, 1, 2, 0}},
246 {
"╆", {1, 1, 2, 2, 0}},
247 {
"╇", {2, 2, 2, 1, 0}},
248 {
"╈", {2, 1, 2, 2, 0}},
249 {
"╉", {2, 2, 1, 2, 0}},
250 {
"╊", {1, 2, 2, 2, 0}},
251 {
"╋", {2, 2, 2, 2, 0}},
253 {
"═", {3, 0, 3, 0, 0}},
254 {
"║", {0, 3, 0, 3, 0}},
256 {
"╒", {0, 0, 3, 1, 0}},
257 {
"╓", {0, 0, 1, 3, 0}},
258 {
"╔", {0, 0, 3, 3, 0}},
260 {
"╕", {3, 0, 0, 1, 0}},
261 {
"╖", {1, 0, 0, 3, 0}},
262 {
"╗", {3, 0, 0, 3, 0}},
264 {
"╘", {0, 1, 3, 0, 0}},
265 {
"╙", {0, 3, 1, 0, 0}},
266 {
"╚", {0, 3, 3, 0, 0}},
268 {
"╛", {3, 1, 0, 0, 0}},
269 {
"╜", {1, 3, 0, 0, 0}},
270 {
"╝", {3, 3, 0, 0, 0}},
272 {
"╞", {0, 1, 3, 1, 0}},
273 {
"╟", {0, 3, 1, 3, 0}},
274 {
"╠", {0, 3, 3, 3, 0}},
276 {
"╡", {3, 1, 0, 1, 0}},
277 {
"╢", {1, 3, 0, 3, 0}},
278 {
"╣", {3, 3, 0, 3, 0}},
280 {
"╤", {3, 0, 3, 1, 0}},
281 {
"╥", {1, 0, 1, 3, 0}},
282 {
"╦", {3, 0, 3, 3, 0}},
284 {
"╧", {3, 1, 3, 0, 0}},
285 {
"╨", {1, 3, 1, 0, 0}},
286 {
"╩", {3, 3, 3, 0, 0}},
288 {
"╪", {3, 1, 3, 1, 0}},
289 {
"╫", {1, 3, 1, 3, 0}},
290 {
"╬", {3, 3, 3, 3, 0}},
292 {
"╭", {0, 0, 1, 1, 1}},
293 {
"╮", {1, 0, 0, 1, 1}},
294 {
"╯", {1, 1, 0, 0, 1}},
295 {
"╰", {0, 1, 1, 0, 1}},
297 {
"╴", {1, 0, 0, 0, 0}},
298 {
"╵", {0, 1, 0, 0, 0}},
299 {
"╶", {0, 0, 1, 0, 0}},
300 {
"╷", {0, 0, 0, 1, 0}},
302 {
"╸", {2, 0, 0, 0, 0}},
303 {
"╹", {0, 2, 0, 0, 0}},
304 {
"╺", {0, 0, 2, 0, 0}},
305 {
"╻", {0, 0, 0, 2, 0}},
307 {
"╼", {1, 0, 2, 0, 0}},
308 {
"╽", {0, 1, 0, 2, 0}},
309 {
"╾", {2, 0, 1, 0, 0}},
310 {
"╿", {0, 2, 0, 1, 0}},
314template <
class A,
class B>
315std::map<B, A> InvertMap(
const std::map<A, B>& input) {
316 std::map<B, A> output;
317 for (
const auto& it : input) {
318 output[it.second] = it.first;
323const std::map<TileEncoding, std::string> tile_encoding_inverse =
324 InvertMap(tile_encoding);
326void UpgradeLeftRight(std::string&
left, std::string&
right) {
327 const auto it_left = tile_encoding.find(
left);
328 if (it_left == tile_encoding.end()) {
331 const auto it_right = tile_encoding.find(
right);
332 if (it_right == tile_encoding.end()) {
336 if (it_left->second.right == 0 && it_right->second.left != 0) {
337 TileEncoding encoding_left = it_left->second;
338 encoding_left.right = it_right->second.left;
339 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
340 if (it_left_upgrade != tile_encoding_inverse.end()) {
341 left = it_left_upgrade->second;
345 if (it_right->second.left == 0 && it_left->second.right != 0) {
346 TileEncoding encoding_right = it_right->second;
347 encoding_right.left = it_left->second.right;
348 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
349 if (it_right_upgrade != tile_encoding_inverse.end()) {
350 right = it_right_upgrade->second;
355void UpgradeTopDown(std::string&
top, std::string&
down) {
356 const auto it_top = tile_encoding.find(
top);
357 if (it_top == tile_encoding.end()) {
360 const auto it_down = tile_encoding.find(
down);
361 if (it_down == tile_encoding.end()) {
365 if (it_top->second.down == 0 && it_down->second.top != 0) {
366 TileEncoding encoding_top = it_top->second;
367 encoding_top.down = it_down->second.top;
368 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
369 if (it_top_down != tile_encoding_inverse.end()) {
370 top = it_top_down->second;
374 if (it_down->second.top == 0 && it_top->second.down != 0) {
375 TileEncoding encoding_down = it_down->second;
376 encoding_down.top = it_top->second.down;
377 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
378 if (it_down_top != tile_encoding_inverse.end()) {
379 down = it_down_top->second;
384bool ShouldAttemptAutoMerge(Cell& cell) {
385 return cell.automerge && cell.character.size() == 3;
393Dimensions Dimension::Fixed(
int v) {
400Dimensions Dimension::Full() {
406Screen Screen::Create(Dimensions width, Dimensions height) {
407 return {width.dimx, height.dimy};
412Screen Screen::Create(Dimensions dimension) {
413 return {dimension.dimx, dimension.dimy};
416Screen::Screen(
int dimx,
int dimy) : Surface{dimx, dimy} {
423 SetConsoleOutputCP(CP_UTF8);
424 SetConsoleCP(CP_UTF8);
425 WindowsEmulateVT100Terminal();
433std::string Screen::ToString()
const {
436 ss.reserve(
static_cast<size_t>(dimx_) *
static_cast<size_t>(dimy_) * 30);
444void Screen::ToString(std::string& ss)
const {
445 const Cell default_cell;
446 const Cell* previous_cell_ref = &default_cell;
448 for (
int y = 0;
y < dimy_; ++
y) {
451 UpdateCellStyle(
this, ss, *previous_cell_ref, default_cell);
452 previous_cell_ref = &default_cell;
457 bool previous_fullwidth =
false;
459 const Cell* line_start = &FastCellAt(0, y);
460 const Cell* line_end = line_start + dimx_;
461 for (
const Cell* it = line_start; it != line_end; ++it) {
462 const auto& cell = *it;
463 if (!previous_fullwidth) {
464 UpdateCellStyle(
this, ss, *previous_cell_ref, cell);
465 previous_cell_ref = &cell;
466 if (cell.character.empty()) {
469 ss += cell.character;
472 if (cell.character.size() <= 1) {
473 previous_fullwidth =
false;
475 previous_fullwidth = (string_width(cell.character) == 2);
482 UpdateCellStyle(
this, ss, *previous_cell_ref, default_cell);
486void Screen::Print()
const {
487 std::cout << ToString() <<
'\0' << std::flush;
509std::string Screen::ResetPosition(
bool clear)
const {
511 ss.reserve(
static_cast<size_t>(dimy_) * 12);
512 ResetPosition(ss, clear);
520void Screen::ResetPosition(std::string& ss,
bool clear)
const {
527 for (
int y = 1;
y < dimy_; ++
y) {
537 ss +=
"\x1B[" + std::to_string(dimy_ - 1) +
"A";
543void Screen::Clear() {
546 cursor_.x = dimx_ - 1;
547 cursor_.y = dimy_ - 1;
555void Screen::ApplyShader() {
557 for (
int y = 0;
y < dimy_; ++
y) {
558 for (
int x = 0; x < dimx_; ++x) {
560 Cell& cur = FastCellAt(x, y);
561 if (!ShouldAttemptAutoMerge(cur)) {
566 Cell&
left = FastCellAt(x - 1, y);
567 if (ShouldAttemptAutoMerge(
left)) {
568 UpgradeLeftRight(
left.character, cur.character);
572 Cell&
top = FastCellAt(x, y - 1);
573 if (ShouldAttemptAutoMerge(
top)) {
574 UpgradeTopDown(
top.character, cur.character);
582std::uint8_t Screen::RegisterHyperlink(std::string_view link) {
583 for (std::size_t i = 0; i < hyperlinks_.size(); ++i) {
584 if (hyperlinks_[i] == link) {
588 if (hyperlinks_.size() == std::numeric_limits<std::uint8_t>::max()) {
591 hyperlinks_.emplace_back(link);
592 return hyperlinks_.size() - 1;
595const std::string& Screen::Hyperlink(std::uint8_t
id)
const {
596 if (
id >= hyperlinks_.size()) {
597 return hyperlinks_[0];
599 return hyperlinks_[id];
604const Screen::SelectionStyle& Screen::GetSelectionStyle()
const {
605 return selection_style_;
610void Screen::SetSelectionStyle(SelectionStyle decorator) {
611 selection_style_ = std::move(decorator);
614void Screen::Reserved1() {}
615void Screen::Reserved2() {}
616void Screen::Reserved3() {}
617void Screen::Reserved4() {}
618void Screen::Reserved5() {}
619void Screen::Reserved6() {}
620void Screen::Reserved7() {}
621void Screen::Reserved8() {}
Dimensions Size()
Get the terminal size.
The FTXUI ftxui:: namespace.
#define FTXUI_UNLIKELY(x)