18#define WIN32_LEAN_AND_MEAN
26#if !defined(FTXUI_UNLIKELY)
27#if defined(COMPILER_GCC) || defined(__clang__)
28#define FTXUI_UNLIKELY(x) __builtin_expect(!!(x), 0)
30#define FTXUI_UNLIKELY(x) (x)
34#if !defined(FTXUI_LIKELY)
35#if defined(COMPILER_GCC) || defined(__clang__)
36#define FTXUI_LIKELY(x) __builtin_expect(!!(x), 1)
38#define FTXUI_LIKELY(x) (x)
47void WindowsEmulateVT100Terminal() {
48 static bool done =
false;
55 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
58 GetConsoleMode(stdout_handle, &out_mode);
61 const int enable_virtual_terminal_processing = 0x0004;
62 const int disable_newline_auto_return = 0x0008;
63 out_mode |= enable_virtual_terminal_processing;
64 out_mode |= disable_newline_auto_return;
66 SetConsoleMode(stdout_handle, out_mode);
71void UpdateCellStyle(
const Screen* screen,
78 ss += screen->Hyperlink(next.hyperlink);
83 if (
FTXUI_UNLIKELY((next.bold ^ prev.bold) | (next.dim ^ prev.dim))) {
85 if ((prev.bold && !next.bold) || (prev.dim && !next.dim)) {
98 next.underlined_double != prev.underlined_double)) {
99 ss += (next.underlined ?
"\x1B[4m"
100 : next.underlined_double ?
"\x1B[21m"
106 ss += (next.blink ?
"\x1B[5m"
112 ss += (next.inverted ?
"\x1B[7m"
118 ss += (next.italic ?
"\x1B[3m"
124 ss += (next.strikethrough ?
"\x1B[9m"
128 if (
FTXUI_UNLIKELY(next.foreground_color != prev.foreground_color ||
129 next.background_color != prev.background_color)) {
131 next.foreground_color.PrintTo(ss,
false);
134 next.background_color.PrintTo(ss,
true);
147 bool operator<(
const TileEncoding& other)
const {
148 if (
left < other.left) {
return true; }
149 if (
left > other.left) {
return false; }
150 if (
top < other.top) {
return true; }
151 if (
top > other.top) {
return false; }
152 if (
right < other.right) {
return true; }
153 if (
right > other.right) {
return false; }
154 if (
down < other.down) {
return true; }
155 if (
down > other.down) {
return false; }
156 if (
round < other.round) {
return true; }
157 if (
round > other.round) {
return false; }
164const std::map<std::string, TileEncoding> tile_encoding = {
165 {
"─", {1, 0, 1, 0, 0}},
166 {
"━", {2, 0, 2, 0, 0}},
167 {
"╍", {2, 0, 2, 0, 0}},
169 {
"│", {0, 1, 0, 1, 0}},
170 {
"┃", {0, 2, 0, 2, 0}},
171 {
"╏", {0, 2, 0, 2, 0}},
173 {
"┌", {0, 0, 1, 1, 0}},
174 {
"┍", {0, 0, 2, 1, 0}},
175 {
"┎", {0, 0, 1, 2, 0}},
176 {
"┏", {0, 0, 2, 2, 0}},
178 {
"┐", {1, 0, 0, 1, 0}},
179 {
"┑", {2, 0, 0, 1, 0}},
180 {
"┒", {1, 0, 0, 2, 0}},
181 {
"┓", {2, 0, 0, 2, 0}},
183 {
"└", {0, 1, 1, 0, 0}},
184 {
"┕", {0, 1, 2, 0, 0}},
185 {
"┖", {0, 2, 1, 0, 0}},
186 {
"┗", {0, 2, 2, 0, 0}},
188 {
"┘", {1, 1, 0, 0, 0}},
189 {
"┙", {2, 1, 0, 0, 0}},
190 {
"┚", {1, 2, 0, 0, 0}},
191 {
"┛", {2, 2, 0, 0, 0}},
193 {
"├", {0, 1, 1, 1, 0}},
194 {
"┝", {0, 1, 2, 1, 0}},
195 {
"┞", {0, 2, 1, 1, 0}},
196 {
"┟", {0, 1, 1, 2, 0}},
197 {
"┠", {0, 2, 1, 2, 0}},
198 {
"┡", {0, 2, 2, 1, 0}},
199 {
"┢", {0, 1, 2, 2, 0}},
200 {
"┣", {0, 2, 2, 2, 0}},
202 {
"┤", {1, 1, 0, 1, 0}},
203 {
"┥", {2, 1, 0, 1, 0}},
204 {
"┦", {1, 2, 0, 1, 0}},
205 {
"┧", {1, 1, 0, 2, 0}},
206 {
"┨", {1, 2, 0, 2, 0}},
207 {
"┩", {2, 2, 0, 1, 0}},
208 {
"┪", {2, 1, 0, 2, 0}},
209 {
"┫", {2, 2, 0, 2, 0}},
211 {
"┬", {1, 0, 1, 1, 0}},
212 {
"┭", {2, 0, 1, 1, 0}},
213 {
"┮", {1, 0, 2, 1, 0}},
214 {
"┯", {2, 0, 2, 1, 0}},
215 {
"┰", {1, 0, 1, 2, 0}},
216 {
"┱", {2, 0, 1, 2, 0}},
217 {
"┲", {1, 0, 2, 2, 0}},
218 {
"┳", {2, 0, 2, 2, 0}},
220 {
"┴", {1, 1, 1, 0, 0}},
221 {
"┵", {2, 1, 1, 0, 0}},
222 {
"┶", {1, 1, 2, 0, 0}},
223 {
"┷", {2, 1, 2, 0, 0}},
224 {
"┸", {1, 2, 1, 0, 0}},
225 {
"┹", {2, 2, 1, 0, 0}},
226 {
"┺", {1, 2, 2, 0, 0}},
227 {
"┻", {2, 2, 2, 0, 0}},
229 {
"┼", {1, 1, 1, 1, 0}},
230 {
"┽", {2, 1, 1, 1, 0}},
231 {
"┾", {1, 1, 2, 1, 0}},
232 {
"┿", {2, 1, 2, 1, 0}},
233 {
"╀", {1, 2, 1, 1, 0}},
234 {
"╁", {1, 1, 1, 2, 0}},
235 {
"╂", {1, 2, 1, 2, 0}},
236 {
"╃", {2, 2, 1, 1, 0}},
237 {
"╄", {1, 2, 2, 1, 0}},
238 {
"╅", {2, 1, 1, 2, 0}},
239 {
"╆", {1, 1, 2, 2, 0}},
240 {
"╇", {2, 2, 2, 1, 0}},
241 {
"╈", {2, 1, 2, 2, 0}},
242 {
"╉", {2, 2, 1, 2, 0}},
243 {
"╊", {1, 2, 2, 2, 0}},
244 {
"╋", {2, 2, 2, 2, 0}},
246 {
"═", {3, 0, 3, 0, 0}},
247 {
"║", {0, 3, 0, 3, 0}},
249 {
"╒", {0, 0, 3, 1, 0}},
250 {
"╓", {0, 0, 1, 3, 0}},
251 {
"╔", {0, 0, 3, 3, 0}},
253 {
"╕", {3, 0, 0, 1, 0}},
254 {
"╖", {1, 0, 0, 3, 0}},
255 {
"╗", {3, 0, 0, 3, 0}},
257 {
"╘", {0, 1, 3, 0, 0}},
258 {
"╙", {0, 3, 1, 0, 0}},
259 {
"╚", {0, 3, 3, 0, 0}},
261 {
"╛", {3, 1, 0, 0, 0}},
262 {
"╜", {1, 3, 0, 0, 0}},
263 {
"╝", {3, 3, 0, 0, 0}},
265 {
"╞", {0, 1, 3, 1, 0}},
266 {
"╟", {0, 3, 1, 3, 0}},
267 {
"╠", {0, 3, 3, 3, 0}},
269 {
"╡", {3, 1, 0, 1, 0}},
270 {
"╢", {1, 3, 0, 3, 0}},
271 {
"╣", {3, 3, 0, 3, 0}},
273 {
"╤", {3, 0, 3, 1, 0}},
274 {
"╥", {1, 0, 1, 3, 0}},
275 {
"╦", {3, 0, 3, 3, 0}},
277 {
"╧", {3, 1, 3, 0, 0}},
278 {
"╨", {1, 3, 1, 0, 0}},
279 {
"╩", {3, 3, 3, 0, 0}},
281 {
"╪", {3, 1, 3, 1, 0}},
282 {
"╫", {1, 3, 1, 3, 0}},
283 {
"╬", {3, 3, 3, 3, 0}},
285 {
"╭", {0, 0, 1, 1, 1}},
286 {
"╮", {1, 0, 0, 1, 1}},
287 {
"╯", {1, 1, 0, 0, 1}},
288 {
"╰", {0, 1, 1, 0, 1}},
290 {
"╴", {1, 0, 0, 0, 0}},
291 {
"╵", {0, 1, 0, 0, 0}},
292 {
"╶", {0, 0, 1, 0, 0}},
293 {
"╷", {0, 0, 0, 1, 0}},
295 {
"╸", {2, 0, 0, 0, 0}},
296 {
"╹", {0, 2, 0, 0, 0}},
297 {
"╺", {0, 0, 2, 0, 0}},
298 {
"╻", {0, 0, 0, 2, 0}},
300 {
"╼", {1, 0, 2, 0, 0}},
301 {
"╽", {0, 1, 0, 2, 0}},
302 {
"╾", {2, 0, 1, 0, 0}},
303 {
"╿", {0, 2, 0, 1, 0}},
307template <
class A,
class B>
308std::map<B, A> InvertMap(
const std::map<A, B> input) {
309 std::map<B, A> output;
310 for (
const auto& it : input) {
311 output[it.second] = it.first;
316const std::map<TileEncoding, std::string> tile_encoding_inverse =
317 InvertMap(tile_encoding);
319void UpgradeLeftRight(std::string&
left, std::string&
right) {
320 const auto it_left = tile_encoding.find(
left);
321 if (it_left == tile_encoding.end()) {
324 const auto it_right = tile_encoding.find(
right);
325 if (it_right == tile_encoding.end()) {
329 if (it_left->second.right == 0 && it_right->second.left != 0) {
330 TileEncoding encoding_left = it_left->second;
331 encoding_left.right = it_right->second.left;
332 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
333 if (it_left_upgrade != tile_encoding_inverse.end()) {
334 left = it_left_upgrade->second;
338 if (it_right->second.left == 0 && it_left->second.right != 0) {
339 TileEncoding encoding_right = it_right->second;
340 encoding_right.left = it_left->second.right;
341 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
342 if (it_right_upgrade != tile_encoding_inverse.end()) {
343 right = it_right_upgrade->second;
348void UpgradeTopDown(std::string&
top, std::string&
down) {
349 const auto it_top = tile_encoding.find(
top);
350 if (it_top == tile_encoding.end()) {
353 const auto it_down = tile_encoding.find(
down);
354 if (it_down == tile_encoding.end()) {
358 if (it_top->second.down == 0 && it_down->second.top != 0) {
359 TileEncoding encoding_top = it_top->second;
360 encoding_top.down = it_down->second.top;
361 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
362 if (it_top_down != tile_encoding_inverse.end()) {
363 top = it_top_down->second;
367 if (it_down->second.top == 0 && it_top->second.down != 0) {
368 TileEncoding encoding_down = it_down->second;
369 encoding_down.top = it_top->second.down;
370 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
371 if (it_down_top != tile_encoding_inverse.end()) {
372 down = it_down_top->second;
377bool ShouldAttemptAutoMerge(Cell& cell) {
378 return cell.automerge && cell.character.size() == 3;
406 return {dimension.
dimx, dimension.
dimy};
416 SetConsoleOutputCP(CP_UTF8);
417 SetConsoleCP(CP_UTF8);
418 WindowsEmulateVT100Terminal();
429 ss.reserve(
static_cast<size_t>(
dimx_) *
static_cast<size_t>(
dimy_) * 30);
438 const Cell default_cell;
439 const Cell* previous_cell_ref = &default_cell;
441 for (
int y = 0; y <
dimy_; ++y) {
444 UpdateCellStyle(
this, ss, *previous_cell_ref, default_cell);
445 previous_cell_ref = &default_cell;
450 bool previous_fullwidth =
false;
451 for (
const auto& cell :
cells_[y]) {
452 if (!previous_fullwidth) {
453 UpdateCellStyle(
this, ss, *previous_cell_ref, cell);
454 previous_cell_ref = &cell;
455 if (cell.character.empty()) {
458 ss += cell.character;
461 if (cell.character.size() <= 1) {
462 previous_fullwidth =
false;
464 previous_fullwidth = (
string_width(cell.character) == 2);
470 UpdateCellStyle(
this, ss, *previous_cell_ref, default_cell);
475 std::cout <<
ToString() <<
'\0' << std::flush;
499 ss.reserve(
static_cast<size_t>(
dimy_) * 12);
512 for (
int y = 1; y <
dimy_; ++y) {
518 for (
int y = 1; y <
dimy_; ++y) {
539 for (
int y = 0; y <
dimy_; ++y) {
540 for (
int x = 0; x <
dimx_; ++x) {
543 if (!ShouldAttemptAutoMerge(cur)) {
549 if (ShouldAttemptAutoMerge(
left)) {
555 if (ShouldAttemptAutoMerge(
top)) {
565 for (std::size_t i = 0; i <
hyperlinks_.size(); ++i) {
570 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)