17#define WIN32_LEAN_AND_MEAN
25#if !defined(FTXUI_UNLIKELY)
26#if defined(COMPILER_GCC) || defined(__clang__)
27#define FTXUI_UNLIKELY(x) __builtin_expect(!!(x), 0)
29#define FTXUI_UNLIKELY(x) (x)
33#if !defined(FTXUI_LIKELY)
34#if defined(COMPILER_GCC) || defined(__clang__)
35#define FTXUI_LIKELY(x) __builtin_expect(!!(x), 1)
37#define FTXUI_LIKELY(x) (x)
45Pixel& dev_null_pixel() {
51void WindowsEmulateVT100Terminal() {
52 static bool done =
false;
58 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
61 GetConsoleMode(stdout_handle, &out_mode);
64 const int enable_virtual_terminal_processing = 0x0004;
65 const int disable_newline_auto_return = 0x0008;
66 out_mode |= enable_virtual_terminal_processing;
67 out_mode |= disable_newline_auto_return;
69 SetConsoleMode(stdout_handle, out_mode);
74void UpdatePixelStyle(
const Screen* screen,
75 std::stringstream& ss,
80 ss <<
"\x1B]8;;" << screen->Hyperlink(next.hyperlink) <<
"\x1B\\";
84 if (
FTXUI_UNLIKELY((next.bold ^ prev.bold) | (next.dim ^ prev.dim))) {
86 ss << ((prev.bold && !next.bold) || (prev.dim && !next.dim) ?
"\x1B[22m"
88 ss << (next.bold ?
"\x1B[1m" :
"");
89 ss << (next.dim ?
"\x1B[2m" :
"");
94 next.underlined_double != prev.underlined_double)) {
95 ss << (next.underlined ?
"\x1B[4m"
96 : next.underlined_double ?
"\x1B[21m"
102 ss << (next.blink ?
"\x1B[5m"
108 ss << (next.inverted ?
"\x1B[7m"
114 ss << (next.strikethrough ?
"\x1B[9m"
118 if (
FTXUI_UNLIKELY(next.foreground_color != prev.foreground_color ||
119 next.background_color != prev.background_color)) {
120 ss <<
"\x1B[" + next.foreground_color.Print(
false) +
"m";
121 ss <<
"\x1B[" + next.background_color.Print(
true) +
"m";
133 bool operator<(
const TileEncoding& other)
const {
134 if (left < other.left) {
return true; }
135 if (left > other.left) {
return false; }
136 if (top < other.top) {
return true; }
137 if (top > other.top) {
return false; }
138 if (right < other.right) {
return true; }
139 if (right > other.right) {
return false; }
140 if (down < other.down) {
return true; }
141 if (down > other.down) {
return false; }
142 if (round < other.round) {
return true; }
143 if (round > other.round) {
return false; }
150const std::map<std::string, TileEncoding> tile_encoding = {
151 {
"─", {1, 0, 1, 0, 0}},
152 {
"━", {2, 0, 2, 0, 0}},
153 {
"╍", {2, 0, 2, 0, 0}},
155 {
"│", {0, 1, 0, 1, 0}},
156 {
"┃", {0, 2, 0, 2, 0}},
157 {
"╏", {0, 2, 0, 2, 0}},
159 {
"┌", {0, 0, 1, 1, 0}},
160 {
"┍", {0, 0, 2, 1, 0}},
161 {
"┎", {0, 0, 1, 2, 0}},
162 {
"┏", {0, 0, 2, 2, 0}},
164 {
"┐", {1, 0, 0, 1, 0}},
165 {
"┑", {2, 0, 0, 1, 0}},
166 {
"┒", {1, 0, 0, 2, 0}},
167 {
"┓", {2, 0, 0, 2, 0}},
169 {
"└", {0, 1, 1, 0, 0}},
170 {
"┕", {0, 1, 2, 0, 0}},
171 {
"┖", {0, 2, 1, 0, 0}},
172 {
"┗", {0, 2, 2, 0, 0}},
174 {
"┘", {1, 1, 0, 0, 0}},
175 {
"┙", {2, 1, 0, 0, 0}},
176 {
"┚", {1, 2, 0, 0, 0}},
177 {
"┛", {2, 2, 0, 0, 0}},
179 {
"├", {0, 1, 1, 1, 0}},
180 {
"┝", {0, 1, 2, 1, 0}},
181 {
"┞", {0, 2, 1, 1, 0}},
182 {
"┟", {0, 1, 1, 2, 0}},
183 {
"┠", {0, 2, 1, 2, 0}},
184 {
"┡", {0, 2, 2, 1, 0}},
185 {
"┢", {0, 1, 2, 2, 0}},
186 {
"┣", {0, 2, 2, 2, 0}},
188 {
"┤", {1, 1, 0, 1, 0}},
189 {
"┥", {2, 1, 0, 1, 0}},
190 {
"┦", {1, 2, 0, 1, 0}},
191 {
"┧", {1, 1, 0, 2, 0}},
192 {
"┨", {1, 2, 0, 2, 0}},
193 {
"┩", {2, 2, 0, 1, 0}},
194 {
"┪", {2, 1, 0, 2, 0}},
195 {
"┫", {2, 2, 0, 2, 0}},
197 {
"┬", {1, 0, 1, 1, 0}},
198 {
"┭", {2, 0, 1, 1, 0}},
199 {
"┮", {1, 0, 2, 1, 0}},
200 {
"┯", {2, 0, 2, 1, 0}},
201 {
"┰", {1, 0, 1, 2, 0}},
202 {
"┱", {2, 0, 1, 2, 0}},
203 {
"┲", {1, 0, 2, 2, 0}},
204 {
"┳", {2, 0, 2, 2, 0}},
206 {
"┴", {1, 1, 1, 0, 0}},
207 {
"┵", {2, 1, 1, 0, 0}},
208 {
"┶", {1, 1, 2, 0, 0}},
209 {
"┷", {2, 1, 2, 0, 0}},
210 {
"┸", {1, 2, 1, 0, 0}},
211 {
"┹", {2, 2, 1, 0, 0}},
212 {
"┺", {1, 2, 2, 0, 0}},
213 {
"┻", {2, 2, 2, 0, 0}},
215 {
"┼", {1, 1, 1, 1, 0}},
216 {
"┽", {2, 1, 1, 1, 0}},
217 {
"┾", {1, 1, 2, 1, 0}},
218 {
"┿", {2, 1, 2, 1, 0}},
219 {
"╀", {1, 2, 1, 1, 0}},
220 {
"╁", {1, 1, 1, 2, 0}},
221 {
"╂", {1, 2, 1, 2, 0}},
222 {
"╃", {2, 2, 1, 1, 0}},
223 {
"╄", {1, 2, 2, 1, 0}},
224 {
"╅", {2, 1, 1, 2, 0}},
225 {
"╆", {1, 1, 2, 2, 0}},
226 {
"╇", {2, 2, 2, 1, 0}},
227 {
"╈", {2, 1, 2, 2, 0}},
228 {
"╉", {2, 2, 1, 2, 0}},
229 {
"╊", {1, 2, 2, 2, 0}},
230 {
"╋", {2, 2, 2, 2, 0}},
232 {
"═", {3, 0, 3, 0, 0}},
233 {
"║", {0, 3, 0, 3, 0}},
235 {
"╒", {0, 0, 3, 1, 0}},
236 {
"╓", {0, 0, 1, 3, 0}},
237 {
"╔", {0, 0, 3, 3, 0}},
239 {
"╕", {3, 0, 0, 1, 0}},
240 {
"╖", {1, 0, 0, 3, 0}},
241 {
"╗", {3, 0, 0, 3, 0}},
243 {
"╘", {0, 1, 3, 0, 0}},
244 {
"╙", {0, 3, 1, 0, 0}},
245 {
"╚", {0, 3, 3, 0, 0}},
247 {
"╛", {3, 1, 0, 0, 0}},
248 {
"╜", {1, 3, 0, 0, 0}},
249 {
"╝", {3, 3, 0, 0, 0}},
251 {
"╞", {0, 1, 3, 1, 0}},
252 {
"╟", {0, 3, 1, 3, 0}},
253 {
"╠", {0, 3, 3, 3, 0}},
255 {
"╡", {3, 1, 0, 1, 0}},
256 {
"╢", {1, 3, 0, 3, 0}},
257 {
"╣", {3, 3, 0, 3, 0}},
259 {
"╤", {3, 0, 3, 1, 0}},
260 {
"╥", {1, 0, 1, 3, 0}},
261 {
"╦", {3, 0, 3, 3, 0}},
263 {
"╧", {3, 1, 3, 0, 0}},
264 {
"╨", {1, 3, 1, 0, 0}},
265 {
"╩", {3, 3, 3, 0, 0}},
267 {
"╪", {3, 1, 3, 1, 0}},
268 {
"╫", {1, 3, 1, 3, 0}},
269 {
"╬", {3, 3, 3, 3, 0}},
271 {
"╭", {0, 0, 1, 1, 1}},
272 {
"╮", {1, 0, 0, 1, 1}},
273 {
"╯", {1, 1, 0, 0, 1}},
274 {
"╰", {0, 1, 1, 0, 1}},
276 {
"╴", {1, 0, 0, 0, 0}},
277 {
"╵", {0, 1, 0, 0, 0}},
278 {
"╶", {0, 0, 1, 0, 0}},
279 {
"╷", {0, 0, 0, 1, 0}},
281 {
"╸", {2, 0, 0, 0, 0}},
282 {
"╹", {0, 2, 0, 0, 0}},
283 {
"╺", {0, 0, 2, 0, 0}},
284 {
"╻", {0, 0, 0, 2, 0}},
286 {
"╼", {1, 0, 2, 0, 0}},
287 {
"╽", {0, 1, 0, 2, 0}},
288 {
"╾", {2, 0, 1, 0, 0}},
289 {
"╿", {0, 2, 0, 1, 0}},
293template <
class A,
class B>
294std::map<B, A> InvertMap(
const std::map<A, B> input) {
295 std::map<B, A> output;
296 for (
const auto& it : input) {
297 output[it.second] = it.first;
302const std::map<TileEncoding, std::string> tile_encoding_inverse =
303 InvertMap(tile_encoding);
305void UpgradeLeftRight(std::string& left, std::string& right) {
306 const auto it_left = tile_encoding.find(left);
307 if (it_left == tile_encoding.end()) {
310 const auto it_right = tile_encoding.find(right);
311 if (it_right == tile_encoding.end()) {
315 if (it_left->second.right == 0 && it_right->second.left != 0) {
316 TileEncoding encoding_left = it_left->second;
317 encoding_left.right = it_right->second.left;
318 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
319 if (it_left_upgrade != tile_encoding_inverse.end()) {
320 left = it_left_upgrade->second;
324 if (it_right->second.left == 0 && it_left->second.right != 0) {
325 TileEncoding encoding_right = it_right->second;
326 encoding_right.left = it_left->second.right;
327 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
328 if (it_right_upgrade != tile_encoding_inverse.end()) {
329 right = it_right_upgrade->second;
334void UpgradeTopDown(std::string& top, std::string& down) {
335 const auto it_top = tile_encoding.find(top);
336 if (it_top == tile_encoding.end()) {
339 const auto it_down = tile_encoding.find(down);
340 if (it_down == tile_encoding.end()) {
344 if (it_top->second.down == 0 && it_down->second.top != 0) {
345 TileEncoding encoding_top = it_top->second;
346 encoding_top.down = it_down->second.top;
347 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
348 if (it_top_down != tile_encoding_inverse.end()) {
349 top = it_top_down->second;
353 if (it_down->second.top == 0 && it_top->second.down != 0) {
354 TileEncoding encoding_down = it_down->second;
355 encoding_down.top = it_top->second.down;
356 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
357 if (it_down_top != tile_encoding_inverse.end()) {
358 down = it_down_top->second;
363bool ShouldAttemptAutoMerge(Pixel& pixel) {
364 return pixel.automerge && pixel.character.size() == 3;
392 return {dimension.
dimx, dimension.
dimy};
396 : stencil{0, dimx - 1, 0, dimy - 1},
399 pixels_(dimy, std::vector<
Pixel>(dimx)) {
406 SetConsoleOutputCP(CP_UTF8);
407 SetConsoleCP(CP_UTF8);
408 WindowsEmulateVT100Terminal();
417 std::stringstream ss;
419 const Pixel default_pixel;
420 const Pixel* previous_pixel_ref = &default_pixel;
422 for (
int y = 0; y <
dimy_; ++y) {
425 UpdatePixelStyle(
this, ss, *previous_pixel_ref, default_pixel);
426 previous_pixel_ref = &default_pixel;
431 bool previous_fullwidth =
false;
432 for (
const auto& pixel :
pixels_[y]) {
433 if (!previous_fullwidth) {
434 UpdatePixelStyle(
this, ss, *previous_pixel_ref, pixel);
435 previous_pixel_ref = &pixel;
438 previous_fullwidth = (
string_width(pixel.character) == 2);
443 UpdatePixelStyle(
this, ss, *previous_pixel_ref, default_pixel);
450 std::cout <<
ToString() <<
'\0' << std::flush;
501 std::stringstream ss;
505 for (
int y = 1; y <
dimy_; ++y) {
511 for (
int y = 1; y <
dimy_; ++y) {
521 for (
auto& cell : line) {
536 for (
int y = 0; y <
dimy_; ++y) {
537 for (
int x = 0; x <
dimx_; ++x) {
540 if (!ShouldAttemptAutoMerge(cur)) {
546 if (ShouldAttemptAutoMerge(left)) {
552 if (ShouldAttemptAutoMerge(top)) {
567 if (
hyperlinks_.size() == std::numeric_limits<uint8_t>::max()) {
A rectangular grid of Pixel.
const std::string & Hyperlink(uint8_t id) const
std::string ToString() const
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Pixel & PixelAt(int x, int y)
Access a cell (Pixel) at a given position.
std::string & at(int x, int y)
Access a character in a cell at a given position.
Screen(int dimx, int dimy)
uint8_t RegisterHyperlink(const std::string &link)
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 pixel from the screen.
std::vector< std::string > hyperlinks_
std::vector< std::vector< Pixel > > pixels_
Dimensions Size()
Get the terminal size.
int string_width(const std::string &)
#define FTXUI_UNLIKELY(x)
bool Contain(int x, int y) const
A unicode character and its associated style.