13#define WIN32_LEAN_AND_MEAN
23static const char BOLD_SET[] =
"\x1B[1m";
24static const char BOLD_RESET[] =
"\x1B[22m";
26static const char DIM_SET[] =
"\x1B[2m";
27static const char DIM_RESET[] =
"\x1B[22m";
29static const char UNDERLINED_SET[] =
"\x1B[4m";
30static const char UNDERLINED_RESET[] =
"\x1B[24m";
32static const char BLINK_SET[] =
"\x1B[5m";
33static const char BLINK_RESET[] =
"\x1B[25m";
35static const char INVERTED_SET[] =
"\x1B[7m";
36static const char INVERTED_RESET[] =
"\x1B[27m";
38static const char MOVE_LEFT[] =
"\r";
39static const char MOVE_UP[] =
"\x1B[1A";
40static const char CLEAR_LINE[] =
"\x1B[2K";
45void WindowsEmulateVT100Terminal() {
46 static bool done =
false;
52 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
55 GetConsoleMode(stdout_handle, &out_mode);
58 const int enable_virtual_terminal_processing = 0x0004;
59 const int disable_newline_auto_return = 0x0008;
60 out_mode |= enable_virtual_terminal_processing;
61 out_mode |= disable_newline_auto_return;
63 SetConsoleMode(stdout_handle, out_mode);
67void UpdatePixelStyle(std::stringstream& ss,
70 if (next.bold != previous.bold)
71 ss << (next.bold ? BOLD_SET : BOLD_RESET);
73 if (next.dim != previous.dim)
74 ss << (next.dim ? DIM_SET : DIM_RESET);
76 if (next.underlined != previous.underlined)
77 ss << (next.underlined ? UNDERLINED_SET : UNDERLINED_RESET);
79 if (next.blink != previous.blink)
80 ss << (next.blink ? BLINK_SET : BLINK_RESET);
82 if (next.inverted != previous.inverted)
83 ss << (next.inverted ? INVERTED_SET : INVERTED_RESET);
85 if (next.foreground_color != previous.foreground_color ||
86 next.background_color != previous.background_color) {
87 ss <<
"\x1B[" + next.foreground_color.Print(
false) +
"m";
88 ss <<
"\x1B[" + next.background_color.Print(
true) +
"m";
95 unsigned int left : 2;
97 unsigned int right : 2;
98 unsigned int down : 2;
99 unsigned int round : 1;
102 bool operator<(
const TileEncoding& other)
const {
103 if (left < other.left)
return true;
104 if (left > other.left)
return false;
105 if (top < other.top)
return true;
106 if (top > other.top)
return false;
107 if (right < other.right)
return true;
108 if (right > other.right)
return false;
109 if (down < other.down)
return true;
110 if (down > other.down)
return false;
111 if (round < other.round)
return true;
112 if (round > other.round)
return false;
119const std::map<std::string, TileEncoding> tile_encoding = {
120 {
"─", {1, 0, 1, 0, 0}},
121 {
"━", {2, 0, 2, 0, 0}},
123 {
"│", {0, 1, 0, 1, 0}},
124 {
"┃", {0, 2, 0, 2, 0}},
126 {
"┌", {0, 0, 1, 1, 0}},
127 {
"┍", {0, 0, 2, 1, 0}},
128 {
"┎", {0, 0, 1, 2, 0}},
129 {
"┏", {0, 0, 2, 2, 0}},
131 {
"┐", {1, 0, 0, 1, 0}},
132 {
"┑", {2, 0, 0, 1, 0}},
133 {
"┒", {1, 0, 0, 2, 0}},
134 {
"┓", {2, 0, 0, 2, 0}},
136 {
"└", {0, 1, 1, 0, 0}},
137 {
"┕", {0, 1, 2, 0, 0}},
138 {
"┖", {0, 2, 1, 0, 0}},
139 {
"┗", {0, 2, 2, 0, 0}},
141 {
"┘", {1, 1, 0, 0, 0}},
142 {
"┙", {2, 1, 0, 0, 0}},
143 {
"┚", {1, 2, 0, 0, 0}},
144 {
"┛", {2, 2, 0, 0, 0}},
146 {
"├", {0, 1, 1, 1, 0}},
147 {
"┝", {0, 1, 2, 1, 0}},
148 {
"┞", {0, 2, 1, 1, 0}},
149 {
"┟", {0, 1, 1, 2, 0}},
150 {
"┠", {0, 2, 1, 2, 0}},
151 {
"┡", {0, 2, 2, 1, 0}},
152 {
"┢", {0, 1, 2, 2, 0}},
153 {
"┣", {0, 2, 2, 2, 0}},
155 {
"┤", {1, 1, 0, 1, 0}},
156 {
"┥", {2, 1, 0, 1, 0}},
157 {
"┦", {1, 2, 0, 1, 0}},
158 {
"┧", {1, 1, 0, 2, 0}},
159 {
"┨", {1, 2, 0, 2, 0}},
160 {
"┩", {2, 2, 0, 1, 0}},
161 {
"┪", {2, 1, 0, 2, 0}},
162 {
"┫", {2, 2, 0, 2, 0}},
164 {
"┬", {1, 0, 1, 1, 0}},
165 {
"┭", {2, 0, 1, 1, 0}},
166 {
"┮", {1, 0, 2, 1, 0}},
167 {
"┯", {2, 0, 2, 1, 0}},
168 {
"┰", {1, 0, 1, 2, 0}},
169 {
"┱", {2, 0, 1, 2, 0}},
170 {
"┲", {1, 0, 2, 2, 0}},
171 {
"┳", {2, 0, 2, 2, 0}},
173 {
"┴", {1, 1, 1, 0, 0}},
174 {
"┵", {2, 1, 1, 0, 0}},
175 {
"┶", {1, 1, 2, 0, 0}},
176 {
"┷", {2, 1, 2, 0, 0}},
177 {
"┸", {1, 2, 1, 0, 0}},
178 {
"┹", {2, 2, 1, 0, 0}},
179 {
"┺", {1, 2, 2, 0, 0}},
180 {
"┻", {2, 2, 2, 0, 0}},
182 {
"┼", {1, 1, 1, 1, 0}},
183 {
"┽", {2, 1, 1, 1, 0}},
184 {
"┾", {1, 1, 2, 1, 0}},
185 {
"┿", {2, 1, 2, 1, 0}},
186 {
"╀", {1, 2, 1, 1, 0}},
187 {
"╁", {1, 1, 1, 2, 0}},
188 {
"╂", {1, 2, 1, 2, 0}},
189 {
"╃", {2, 2, 1, 1, 0}},
190 {
"╄", {1, 2, 2, 1, 0}},
191 {
"╅", {2, 1, 1, 2, 0}},
192 {
"╆", {1, 1, 2, 2, 0}},
193 {
"╇", {2, 2, 2, 1, 0}},
194 {
"╈", {2, 1, 2, 2, 0}},
195 {
"╉", {2, 2, 1, 2, 0}},
196 {
"╊", {1, 2, 2, 2, 0}},
197 {
"╋", {2, 2, 2, 2, 0}},
199 {
"═", {3, 0, 3, 0, 0}},
200 {
"║", {0, 3, 0, 3, 0}},
202 {
"╒", {0, 0, 3, 1, 0}},
203 {
"╓", {0, 0, 1, 3, 0}},
204 {
"╔", {0, 0, 3, 3, 0}},
206 {
"╕", {3, 0, 0, 1, 0}},
207 {
"╖", {1, 0, 0, 3, 0}},
208 {
"╗", {3, 0, 0, 3, 0}},
210 {
"╘", {0, 1, 3, 0, 0}},
211 {
"╙", {0, 3, 1, 0, 0}},
212 {
"╚", {0, 3, 3, 0, 0}},
214 {
"╛", {3, 1, 0, 0, 0}},
215 {
"╜", {1, 3, 0, 0, 0}},
216 {
"╝", {3, 3, 0, 0, 0}},
218 {
"╞", {0, 1, 3, 1, 0}},
219 {
"╟", {0, 3, 1, 3, 0}},
220 {
"╠", {0, 3, 3, 3, 0}},
222 {
"╡", {3, 1, 0, 1, 0}},
223 {
"╢", {1, 3, 0, 3, 0}},
224 {
"╣", {3, 3, 0, 3, 0}},
226 {
"╤", {3, 0, 3, 1, 0}},
227 {
"╥", {1, 0, 1, 3, 0}},
228 {
"╦", {3, 0, 3, 3, 0}},
230 {
"╧", {3, 1, 3, 0, 0}},
231 {
"╨", {1, 3, 1, 0, 0}},
232 {
"╩", {3, 3, 3, 0, 0}},
234 {
"╪", {3, 1, 3, 1, 0}},
235 {
"╫", {1, 3, 1, 3, 0}},
236 {
"╬", {3, 3, 3, 3, 0}},
238 {
"╭", {0, 0, 1, 1, 1}},
239 {
"╮", {1, 0, 0, 1, 1}},
240 {
"╯", {1, 1, 0, 0, 1}},
241 {
"╰", {0, 1, 1, 0, 1}},
243 {
"╴", {1, 0, 0, 0, 0}},
244 {
"╵", {0, 1, 0, 0, 0}},
245 {
"╶", {0, 0, 1, 0, 0}},
246 {
"╷", {0, 0, 0, 1, 0}},
248 {
"╸", {2, 0, 0, 0, 0}},
249 {
"╹", {0, 2, 0, 0, 0}},
250 {
"╺", {0, 0, 2, 0, 0}},
251 {
"╻", {0, 0, 0, 2, 0}},
253 {
"╼", {1, 0, 2, 0, 0}},
254 {
"╽", {0, 1, 0, 2, 0}},
255 {
"╾", {2, 0, 1, 0, 0}},
256 {
"╿", {0, 2, 0, 1, 0}},
260template <
class A,
class B>
261const std::map<B, A> InvertMap(
const std::map<A, B> input) {
262 std::map<B, A> output;
263 for (
const auto& it : input)
264 output[it.second] = it.first;
268const std::map<TileEncoding, std::string> tile_encoding_inverse =
269 InvertMap(tile_encoding);
271void UpgradeLeftRight(std::string& left, std::string& right) {
272 const auto it_left = tile_encoding.find(left);
273 if (it_left == tile_encoding.end())
275 const auto it_right = tile_encoding.find(right);
276 if (it_right == tile_encoding.end())
279 if (it_left->second.right == 0 && it_right->second.left != 0) {
280 TileEncoding encoding_left = it_left->second;
281 encoding_left.right = it_right->second.left;
282 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
283 if (it_left_upgrade != tile_encoding_inverse.end())
284 left = it_left_upgrade->second;
287 if (it_right->second.left == 0 && it_left->second.right != 0) {
288 TileEncoding encoding_right = it_right->second;
289 encoding_right.left = it_left->second.right;
290 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
291 if (it_right_upgrade != tile_encoding_inverse.end())
292 right = it_right_upgrade->second;
296void UpgradeTopDown(std::string& top, std::string& down) {
297 const auto it_top = tile_encoding.find(top);
298 if (it_top == tile_encoding.end())
300 const auto it_down = tile_encoding.find(down);
301 if (it_down == tile_encoding.end())
304 if (it_top->second.down == 0 && it_down->second.top != 0) {
305 TileEncoding encoding_top = it_top->second;
306 encoding_top.down = it_down->second.top;
307 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
308 if (it_top_down != tile_encoding_inverse.end())
309 top = it_top_down->second;
312 if (it_down->second.top == 0 && it_top->second.down != 0) {
313 TileEncoding encoding_down = it_down->second;
314 encoding_down.top = it_top->second.down;
315 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
316 if (it_down_top != tile_encoding_inverse.end())
317 down = it_down_top->second;
350 : stencil{0, dimx - 1, 0, dimy - 1},
353 pixels_(dimy, std::vector<
Pixel>(dimx)) {
360 SetConsoleOutputCP(CP_UTF8);
361 SetConsoleCP(CP_UTF8);
362 WindowsEmulateVT100Terminal();
369 std::stringstream ss;
371 Pixel previous_pixel;
374 for (
int y = 0; y <
dimy_; ++y) {
376 UpdatePixelStyle(ss, previous_pixel, final_pixel);
379 bool previous_fullwidth =
false;
380 for (
const auto& pixel :
pixels_[y]) {
381 if (!previous_fullwidth) {
382 UpdatePixelStyle(ss, previous_pixel, pixel);
383 ss << pixel.character;
385 previous_fullwidth = (
string_width(pixel.character) == 2);
389 UpdatePixelStyle(ss, previous_pixel, final_pixel);
395 std::cout <<
ToString() <<
'\0' << std::flush;
432 std::stringstream ss;
434 ss << MOVE_LEFT << CLEAR_LINE;
435 for (
int y = 1; y <
dimy_; ++y) {
436 ss << MOVE_UP << CLEAR_LINE;
440 for (
int y = 1; y <
dimy_; ++y) {
458 for (
int y = 1; y <
dimy_; ++y) {
459 for (
int x = 1; x <
dimx_; ++x) {
461 std::string& cur =
pixels_[y][x].character;
462 if (cur.size() != 3u)
466 std::string& left =
pixels_[y][x-1].character;
467 if (left.size() == 3u)
468 UpgradeLeftRight(left, cur);
471 std::string& top =
pixels_[y-1][x].character;
472 if (top.size() == 3u)
473 UpgradeTopDown(top, cur);
A rectangular grid of Pixel.
std::string ResetPosition(bool clear=false)
Return a string to be printed in order to reset the cursor position to the beginning of the screen.
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Pixel & PixelAt(int x, int y)
Access a Pixel at a given position.
std::string & at(int x, int y)
Access a character a given position.
Screen(int dimx, int dimy)
void Clear()
Clear all the pixel from the screen.
std::vector< std::vector< Pixel > > pixels_
int string_width(const std::string &)
bool Contain(int x, int y)
A unicode character and its associated style.