19 #define WIN32_LEAN_AND_MEAN
27 #if !defined(FTXUI_UNLIKELY)
28 #if defined(COMPILER_GCC) || defined(__clang__)
29 #define FTXUI_UNLIKELY(x) __builtin_expect(!!(x), 0)
31 #define FTXUI_UNLIKELY(x) (x)
35 #if !defined(FTXUI_LIKELY)
36 #if defined(COMPILER_GCC) || defined(__clang__)
37 #define FTXUI_LIKELY(x) __builtin_expect(!!(x), 1)
39 #define FTXUI_LIKELY(x) (x)
48 void WindowsEmulateVT100Terminal() {
49 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);
71 void UpdatePixelStyle(
const Screen* screen,
72 std::stringstream& ss,
77 ss <<
"\x1B]8;;" << screen->Hyperlink(next.hyperlink) <<
"\x1B\\";
81 if (
FTXUI_UNLIKELY((next.bold ^ prev.bold) | (next.dim ^ prev.dim))) {
83 ss << ((prev.bold && !next.bold) || (prev.dim && !next.dim) ?
"\x1B[22m"
85 ss << (next.bold ?
"\x1B[1m" :
"");
86 ss << (next.dim ?
"\x1B[2m" :
"");
91 next.underlined_double != prev.underlined_double)) {
92 ss << (next.underlined ?
"\x1B[4m"
93 : next.underlined_double ?
"\x1B[21m"
99 ss << (next.blink ?
"\x1B[5m"
105 ss << (next.inverted ?
"\x1B[7m"
111 ss << (next.strikethrough ?
"\x1B[9m"
115 if (
FTXUI_UNLIKELY(next.foreground_color != prev.foreground_color ||
116 next.background_color != prev.background_color)) {
117 ss <<
"\x1B[" + next.foreground_color.Print(
false) +
"m";
118 ss <<
"\x1B[" + next.background_color.Print(
true) +
"m";
122 struct TileEncoding {
123 std::uint8_t left : 2;
124 std::uint8_t top : 2;
125 std::uint8_t right : 2;
126 std::uint8_t down : 2;
127 std::uint8_t round : 1;
130 bool operator<(
const TileEncoding& other)
const {
131 if (left < other.left) {
return true; }
132 if (left > other.left) {
return false; }
133 if (top < other.top) {
return true; }
134 if (top > other.top) {
return false; }
135 if (right < other.right) {
return true; }
136 if (right > other.right) {
return false; }
137 if (down < other.down) {
return true; }
138 if (down > other.down) {
return false; }
139 if (round < other.round) {
return true; }
140 if (round > other.round) {
return false; }
147 const std::map<std::string, TileEncoding> tile_encoding = {
148 {
"─", {1, 0, 1, 0, 0}},
149 {
"━", {2, 0, 2, 0, 0}},
150 {
"╍", {2, 0, 2, 0, 0}},
152 {
"│", {0, 1, 0, 1, 0}},
153 {
"┃", {0, 2, 0, 2, 0}},
154 {
"╏", {0, 2, 0, 2, 0}},
156 {
"┌", {0, 0, 1, 1, 0}},
157 {
"┍", {0, 0, 2, 1, 0}},
158 {
"┎", {0, 0, 1, 2, 0}},
159 {
"┏", {0, 0, 2, 2, 0}},
161 {
"┐", {1, 0, 0, 1, 0}},
162 {
"┑", {2, 0, 0, 1, 0}},
163 {
"┒", {1, 0, 0, 2, 0}},
164 {
"┓", {2, 0, 0, 2, 0}},
166 {
"└", {0, 1, 1, 0, 0}},
167 {
"┕", {0, 1, 2, 0, 0}},
168 {
"┖", {0, 2, 1, 0, 0}},
169 {
"┗", {0, 2, 2, 0, 0}},
171 {
"┘", {1, 1, 0, 0, 0}},
172 {
"┙", {2, 1, 0, 0, 0}},
173 {
"┚", {1, 2, 0, 0, 0}},
174 {
"┛", {2, 2, 0, 0, 0}},
176 {
"├", {0, 1, 1, 1, 0}},
177 {
"┝", {0, 1, 2, 1, 0}},
178 {
"┞", {0, 2, 1, 1, 0}},
179 {
"┟", {0, 1, 1, 2, 0}},
180 {
"┠", {0, 2, 1, 2, 0}},
181 {
"┡", {0, 2, 2, 1, 0}},
182 {
"┢", {0, 1, 2, 2, 0}},
183 {
"┣", {0, 2, 2, 2, 0}},
185 {
"┤", {1, 1, 0, 1, 0}},
186 {
"┥", {2, 1, 0, 1, 0}},
187 {
"┦", {1, 2, 0, 1, 0}},
188 {
"┧", {1, 1, 0, 2, 0}},
189 {
"┨", {1, 2, 0, 2, 0}},
190 {
"┩", {2, 2, 0, 1, 0}},
191 {
"┪", {2, 1, 0, 2, 0}},
192 {
"┫", {2, 2, 0, 2, 0}},
194 {
"┬", {1, 0, 1, 1, 0}},
195 {
"┭", {2, 0, 1, 1, 0}},
196 {
"┮", {1, 0, 2, 1, 0}},
197 {
"┯", {2, 0, 2, 1, 0}},
198 {
"┰", {1, 0, 1, 2, 0}},
199 {
"┱", {2, 0, 1, 2, 0}},
200 {
"┲", {1, 0, 2, 2, 0}},
201 {
"┳", {2, 0, 2, 2, 0}},
203 {
"┴", {1, 1, 1, 0, 0}},
204 {
"┵", {2, 1, 1, 0, 0}},
205 {
"┶", {1, 1, 2, 0, 0}},
206 {
"┷", {2, 1, 2, 0, 0}},
207 {
"┸", {1, 2, 1, 0, 0}},
208 {
"┹", {2, 2, 1, 0, 0}},
209 {
"┺", {1, 2, 2, 0, 0}},
210 {
"┻", {2, 2, 2, 0, 0}},
212 {
"┼", {1, 1, 1, 1, 0}},
213 {
"┽", {2, 1, 1, 1, 0}},
214 {
"┾", {1, 1, 2, 1, 0}},
215 {
"┿", {2, 1, 2, 1, 0}},
216 {
"╀", {1, 2, 1, 1, 0}},
217 {
"╁", {1, 1, 1, 2, 0}},
218 {
"╂", {1, 2, 1, 2, 0}},
219 {
"╃", {2, 2, 1, 1, 0}},
220 {
"╄", {1, 2, 2, 1, 0}},
221 {
"╅", {2, 1, 1, 2, 0}},
222 {
"╆", {1, 1, 2, 2, 0}},
223 {
"╇", {2, 2, 2, 1, 0}},
224 {
"╈", {2, 1, 2, 2, 0}},
225 {
"╉", {2, 2, 1, 2, 0}},
226 {
"╊", {1, 2, 2, 2, 0}},
227 {
"╋", {2, 2, 2, 2, 0}},
229 {
"═", {3, 0, 3, 0, 0}},
230 {
"║", {0, 3, 0, 3, 0}},
232 {
"╒", {0, 0, 3, 1, 0}},
233 {
"╓", {0, 0, 1, 3, 0}},
234 {
"╔", {0, 0, 3, 3, 0}},
236 {
"╕", {3, 0, 0, 1, 0}},
237 {
"╖", {1, 0, 0, 3, 0}},
238 {
"╗", {3, 0, 0, 3, 0}},
240 {
"╘", {0, 1, 3, 0, 0}},
241 {
"╙", {0, 3, 1, 0, 0}},
242 {
"╚", {0, 3, 3, 0, 0}},
244 {
"╛", {3, 1, 0, 0, 0}},
245 {
"╜", {1, 3, 0, 0, 0}},
246 {
"╝", {3, 3, 0, 0, 0}},
248 {
"╞", {0, 1, 3, 1, 0}},
249 {
"╟", {0, 3, 1, 3, 0}},
250 {
"╠", {0, 3, 3, 3, 0}},
252 {
"╡", {3, 1, 0, 1, 0}},
253 {
"╢", {1, 3, 0, 3, 0}},
254 {
"╣", {3, 3, 0, 3, 0}},
256 {
"╤", {3, 0, 3, 1, 0}},
257 {
"╥", {1, 0, 1, 3, 0}},
258 {
"╦", {3, 0, 3, 3, 0}},
260 {
"╧", {3, 1, 3, 0, 0}},
261 {
"╨", {1, 3, 1, 0, 0}},
262 {
"╩", {3, 3, 3, 0, 0}},
264 {
"╪", {3, 1, 3, 1, 0}},
265 {
"╫", {1, 3, 1, 3, 0}},
266 {
"╬", {3, 3, 3, 3, 0}},
268 {
"╭", {0, 0, 1, 1, 1}},
269 {
"╮", {1, 0, 0, 1, 1}},
270 {
"╯", {1, 1, 0, 0, 1}},
271 {
"╰", {0, 1, 1, 0, 1}},
273 {
"╴", {1, 0, 0, 0, 0}},
274 {
"╵", {0, 1, 0, 0, 0}},
275 {
"╶", {0, 0, 1, 0, 0}},
276 {
"╷", {0, 0, 0, 1, 0}},
278 {
"╸", {2, 0, 0, 0, 0}},
279 {
"╹", {0, 2, 0, 0, 0}},
280 {
"╺", {0, 0, 2, 0, 0}},
281 {
"╻", {0, 0, 0, 2, 0}},
283 {
"╼", {1, 0, 2, 0, 0}},
284 {
"╽", {0, 1, 0, 2, 0}},
285 {
"╾", {2, 0, 1, 0, 0}},
286 {
"╿", {0, 2, 0, 1, 0}},
290 template <
class A,
class B>
291 std::map<B, A> InvertMap(
const std::map<A, B> input) {
292 std::map<B, A> output;
293 for (
const auto& it : input) {
294 output[it.second] = it.first;
299 const std::map<TileEncoding, std::string> tile_encoding_inverse =
300 InvertMap(tile_encoding);
302 void UpgradeLeftRight(std::string& left, std::string& right) {
303 const auto it_left = tile_encoding.find(left);
304 if (it_left == tile_encoding.end()) {
307 const auto it_right = tile_encoding.find(right);
308 if (it_right == tile_encoding.end()) {
312 if (it_left->second.right == 0 && it_right->second.left != 0) {
313 TileEncoding encoding_left = it_left->second;
314 encoding_left.right = it_right->second.left;
315 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
316 if (it_left_upgrade != tile_encoding_inverse.end()) {
317 left = it_left_upgrade->second;
321 if (it_right->second.left == 0 && it_left->second.right != 0) {
322 TileEncoding encoding_right = it_right->second;
323 encoding_right.left = it_left->second.right;
324 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
325 if (it_right_upgrade != tile_encoding_inverse.end()) {
326 right = it_right_upgrade->second;
331 void UpgradeTopDown(std::string& top, std::string& down) {
332 const auto it_top = tile_encoding.find(top);
333 if (it_top == tile_encoding.end()) {
336 const auto it_down = tile_encoding.find(down);
337 if (it_down == tile_encoding.end()) {
341 if (it_top->second.down == 0 && it_down->second.top != 0) {
342 TileEncoding encoding_top = it_top->second;
343 encoding_top.down = it_down->second.top;
344 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
345 if (it_top_down != tile_encoding_inverse.end()) {
346 top = it_top_down->second;
350 if (it_down->second.top == 0 && it_top->second.down != 0) {
351 TileEncoding encoding_down = it_down->second;
352 encoding_down.top = it_top->second.down;
353 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
354 if (it_down_top != tile_encoding_inverse.end()) {
355 down = it_down_top->second;
360 bool ShouldAttemptAutoMerge(Pixel& pixel) {
361 return pixel.automerge && pixel.character.size() == 3;
389 return {dimension.
dimx, dimension.
dimy};
399 SetConsoleOutputCP(CP_UTF8);
400 SetConsoleCP(CP_UTF8);
401 WindowsEmulateVT100Terminal();
410 std::stringstream ss;
412 const Pixel default_pixel;
413 const Pixel* previous_pixel_ref = &default_pixel;
415 for (
int y = 0; y <
dimy_; ++y) {
418 UpdatePixelStyle(
this, ss, *previous_pixel_ref, default_pixel);
419 previous_pixel_ref = &default_pixel;
424 bool previous_fullwidth =
false;
425 for (
const auto& pixel :
pixels_[y]) {
426 if (!previous_fullwidth) {
427 UpdatePixelStyle(
this, ss, *previous_pixel_ref, pixel);
428 previous_pixel_ref = &pixel;
429 if (pixel.character.empty()) {
435 previous_fullwidth = (
string_width(pixel.character) == 2);
440 UpdatePixelStyle(
this, ss, *previous_pixel_ref, default_pixel);
447 std::cout <<
ToString() <<
'\0' << std::flush;
470 std::stringstream ss;
474 for (
int y = 1; y <
dimy_; ++y) {
480 for (
int y = 1; y <
dimy_; ++y) {
502 for (
int y = 0; y <
dimy_; ++y) {
503 for (
int x = 0; x <
dimx_; ++x) {
506 if (!ShouldAttemptAutoMerge(cur)) {
512 if (ShouldAttemptAutoMerge(left)) {
518 if (ShouldAttemptAutoMerge(top)) {
528 for (std::size_t i = 0; i <
hyperlinks_.size(); ++i) {
533 if (
hyperlinks_.size() == std::numeric_limits<std::uint8_t>::max()) {
A rectangular grid of Pixel.
void Clear()
Clear all the pixel from the screen.
std::vector< std::vector< Pixel > > pixels_
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.
uint8_t RegisterHyperlink(const std::string &link)
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 pixel from the screen.
std::vector< std::string > hyperlinks_
Dimensions Size()
Get the terminal size.
int string_width(const std::string &)
#define FTXUI_UNLIKELY(x)
A Unicode character and its associated style.