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;
408 return {dimension.
dimx, dimension.
dimy};
418 SetConsoleOutputCP(CP_UTF8);
419 SetConsoleCP(CP_UTF8);
420 WindowsEmulateVT100Terminal();
431 ss.reserve(
static_cast<size_t>(
dimx_) *
static_cast<size_t>(
dimy_) * 30);
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;
453 for (
const auto& cell :
cells_[y]) {
454 if (!previous_fullwidth) {
455 UpdateCellStyle(
this, ss, *previous_cell_ref, cell);
456 previous_cell_ref = &cell;
457 if (cell.character.empty()) {
460 ss += cell.character;
463 if (cell.character.size() <= 1) {
464 previous_fullwidth =
false;
466 previous_fullwidth = (
string_width(cell.character) == 2);
472 UpdateCellStyle(
this, ss, *previous_cell_ref, default_cell);
477 std::cout <<
ToString() <<
'\0' << std::flush;
501 ss.reserve(
static_cast<size_t>(
dimy_) * 12);
514 for (
int y = 1; y <
dimy_; ++y) {
520 for (
int y = 1; y <
dimy_; ++y) {
541 for (
int y = 0; y <
dimy_; ++y) {
542 for (
int x = 0; x <
dimx_; ++x) {
545 if (!ShouldAttemptAutoMerge(cur)) {
551 if (ShouldAttemptAutoMerge(
left)) {
557 if (ShouldAttemptAutoMerge(
top)) {
567 for (std::size_t i = 0; i <
hyperlinks_.size(); ++i) {
572 if (
hyperlinks_.size() == std::numeric_limits<std::uint8_t>::max()) {
const SelectionStyle & GetSelectionStyle() const
Return the current selection style.
const std::string & Hyperlink(uint8_t id) const
std::string ToString() const
uint8_t RegisterHyperlink(std::string_view link)
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
std::function< void(Cell &)> SelectionStyle
Screen(int dimx, int dimy)
std::string ResetPosition(bool clear=false) const
Return a string to be printed in order to reset the cursor position to the beginning of the screen.
void Clear()
Clear all the cells from the screen.
std::vector< std::vector< Cell > > cells_
SelectionStyle selection_style_
void SetSelectionStyle(SelectionStyle decorator)
Set the current selection style.
std::vector< std::string > hyperlinks_
A rectangular grid of Cell.
A rectangular grid of Cell.
Dimensions Size()
Get the terminal size.
A Unicode character and its associated style.
Dimensions is a structure that represents the size of the terminal.
The FTXUI ftxui:: namespace.
int string_width(std::string_view)
#define FTXUI_UNLIKELY(x)