10 #define WIN32_LEAN_AND_MEAN
20 static const char BOLD_SET[] =
"\x1B[1m";
21 static const char BOLD_RESET[] =
"\x1B[22m";
23 static const char DIM_SET[] =
"\x1B[2m";
24 static const char DIM_RESET[] =
"\x1B[22m";
26 static const char UNDERLINED_SET[] =
"\x1B[4m";
27 static const char UNDERLINED_RESET[] =
"\x1B[24m";
29 static const char BLINK_SET[] =
"\x1B[5m";
30 static const char BLINK_RESET[] =
"\x1B[25m";
32 static const char INVERTED_SET[] =
"\x1B[7m";
33 static const char INVERTED_RESET[] =
"\x1B[27m";
35 static const char MOVE_LEFT[] =
"\r";
36 static const char MOVE_UP[] =
"\x1B[1A";
37 static const char CLEAR_LINE[] =
"\x1B[2K";
42 void WindowsEmulateVT100Terminal() {
43 static bool done =
false;
49 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
52 GetConsoleMode(stdout_handle, &out_mode);
55 const int enable_virtual_terminal_processing = 0x0004;
56 const int disable_newline_auto_return = 0x0008;
57 out_mode |= enable_virtual_terminal_processing;
58 out_mode |= disable_newline_auto_return;
60 SetConsoleMode(stdout_handle, out_mode);
64 void UpdatePixelStyle(std::stringstream& ss,
67 if (next.bold != previous.bold)
68 ss << (next.bold ? BOLD_SET : BOLD_RESET);
70 if (next.dim != previous.dim)
71 ss << (next.dim ? DIM_SET : DIM_RESET);
73 if (next.underlined != previous.underlined)
74 ss << (next.underlined ? UNDERLINED_SET : UNDERLINED_RESET);
76 if (next.blink != previous.blink)
77 ss << (next.blink ? BLINK_SET : BLINK_RESET);
79 if (next.inverted != previous.inverted)
80 ss << (next.inverted ? INVERTED_SET : INVERTED_RESET);
82 if (next.foreground_color != previous.foreground_color ||
83 next.background_color != previous.background_color) {
84 ss <<
"\x1B[" + next.foreground_color.Print(
false) +
"m";
85 ss <<
"\x1B[" + next.background_color.Print(
true) +
"m";
120 : stencil{0,
dimx - 1, 0,
dimy - 1},
123 pixels_(dimy, std::vector<Pixel>(dimx)) {
130 SetConsoleOutputCP(CP_UTF8);
131 SetConsoleCP(CP_UTF8);
132 WindowsEmulateVT100Terminal();
139 std::stringstream ss;
141 Pixel previous_pixel;
144 for (
int y = 0; y <
dimy_; ++y) {
146 UpdatePixelStyle(ss, previous_pixel, final_pixel);
149 bool previous_fullwidth =
false;
150 for (
const auto& pixel :
pixels_[y]) {
151 if (!previous_fullwidth) {
152 UpdatePixelStyle(ss, previous_pixel, pixel);
153 ss << pixel.character;
155 previous_fullwidth = (
string_width(pixel.character) == 2);
159 UpdatePixelStyle(ss, previous_pixel, final_pixel);
165 std::cout <<
ToString() <<
'\0' << std::flush;
202 std::stringstream ss;
204 ss << MOVE_LEFT << CLEAR_LINE;
205 for (
int y = 1; y <
dimy_; ++y) {
206 ss << MOVE_UP << CLEAR_LINE;
210 for (
int y = 1; y <
dimy_; ++y) {
228 for (
int y = 1; y <
dimy_; ++y) {
229 for (
int x = 1; x <
dimx_; ++x) {
231 std::string& cur =
pixels_[y][x].character;
232 if (cur.size() != 3u)
236 std::string& left =
pixels_[y][x-1].character;
237 if (left.size() == 3u) {
238 if (cur ==
"│" && left ==
"─") cur =
"┤";
239 if (cur ==
"├" && left ==
"─") cur =
"┼";
240 if (cur ==
"─" && left ==
"│") left =
"├";
241 if (cur ==
"─" && left ==
"┤") left =
"┼";
245 std::string& top =
pixels_[y-1][x].character;
246 if (top.size() == 3u) {
247 if (cur ==
"─" && top ==
"│") cur =
"┴";
248 if (cur ==
"┬" && top ==
"│") cur =
"┼";
249 if (cur ==
"│" && top ==
"─") top =
"┬";
250 if (cur ==
"│" && top ==
"┴") top =
"┼";