12#define WIN32_LEAN_AND_MEAN
22static const char BOLD_SET[] =
"\x1B[1m";
23static const char BOLD_RESET[] =
"\x1B[22m";
25static const char DIM_SET[] =
"\x1B[2m";
26static const char DIM_RESET[] =
"\x1B[22m";
28static const char UNDERLINED_SET[] =
"\x1B[4m";
29static const char UNDERLINED_RESET[] =
"\x1B[24m";
31static const char BLINK_SET[] =
"\x1B[5m";
32static const char BLINK_RESET[] =
"\x1B[25m";
34static const char INVERTED_SET[] =
"\x1B[7m";
35static const char INVERTED_RESET[] =
"\x1B[27m";
37static const char MOVE_LEFT[] =
"\r";
38static const char MOVE_UP[] =
"\x1B[1A";
39static const char CLEAR_LINE[] =
"\x1B[2K";
44void WindowsEmulateVT100Terminal() {
45 static bool done =
false;
51 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
54 GetConsoleMode(stdout_handle, &out_mode);
57 const int enable_virtual_terminal_processing = 0x0004;
58 const int disable_newline_auto_return = 0x0008;
59 out_mode |= enable_virtual_terminal_processing;
60 out_mode |= disable_newline_auto_return;
62 SetConsoleMode(stdout_handle, out_mode);
66void UpdatePixelStyle(std::stringstream& ss,
69 if (next.bold != previous.bold)
70 ss << (next.bold ? BOLD_SET : BOLD_RESET);
72 if (next.dim != previous.dim)
73 ss << (next.dim ? DIM_SET : DIM_RESET);
75 if (next.underlined != previous.underlined)
76 ss << (next.underlined ? UNDERLINED_SET : UNDERLINED_RESET);
78 if (next.blink != previous.blink)
79 ss << (next.blink ? BLINK_SET : BLINK_RESET);
81 if (next.inverted != previous.inverted)
82 ss << (next.inverted ? INVERTED_SET : INVERTED_RESET);
84 if (next.foreground_color != previous.foreground_color ||
85 next.background_color != previous.background_color) {
86 ss <<
"\x1B[" + next.foreground_color.Print(
false) +
"m";
87 ss <<
"\x1B[" + next.background_color.Print(
true) +
"m";
94 unsigned int left : 2;
96 unsigned int right : 2;
97 unsigned int down : 2;
98 unsigned int round : 1;
101 bool operator<(
const TileEncoding& other)
const {
102 if (left < other.left)
return true;
103 if (left > other.left)
return false;
104 if (top < other.top)
return true;
105 if (top > other.top)
return false;
106 if (right < other.right)
return true;
107 if (right > other.right)
return false;
108 if (down < other.down)
return true;
109 if (down > other.down)
return false;
110 if (round < other.round)
return true;
111 if (round > other.round)
return false;
118const std::map<std::string, TileEncoding> tile_encoding = {
119 {
"─", {1, 0, 1, 0, 0}},
120 {
"━", {2, 0, 2, 0, 0}},
122 {
"│", {0, 1, 0, 1, 0}},
123 {
"┃", {0, 2, 0, 2, 0}},
125 {
"┌", {0, 0, 1, 1, 0}},
126 {
"┍", {0, 0, 2, 1, 0}},
127 {
"┎", {0, 0, 1, 2, 0}},
128 {
"┏", {0, 0, 2, 2, 0}},
130 {
"┐", {1, 0, 0, 1, 0}},
131 {
"┑", {2, 0, 0, 1, 0}},
132 {
"┒", {1, 0, 0, 2, 0}},
133 {
"┓", {2, 0, 0, 2, 0}},
135 {
"└", {0, 1, 1, 0, 0}},
136 {
"┕", {0, 1, 2, 0, 0}},
137 {
"┖", {0, 2, 1, 0, 0}},
138 {
"┗", {0, 2, 2, 0, 0}},
140 {
"┘", {1, 1, 0, 0, 0}},
141 {
"┙", {2, 1, 0, 0, 0}},
142 {
"┚", {1, 2, 0, 0, 0}},
143 {
"┛", {2, 2, 0, 0, 0}},
145 {
"├", {0, 1, 1, 1, 0}},
146 {
"┝", {0, 1, 2, 1, 0}},
147 {
"┞", {0, 2, 1, 1, 0}},
148 {
"┟", {0, 1, 1, 2, 0}},
149 {
"┠", {0, 2, 1, 2, 0}},
150 {
"┡", {0, 2, 2, 1, 0}},
151 {
"┢", {0, 1, 2, 2, 0}},
152 {
"┣", {0, 2, 2, 2, 0}},
154 {
"┤", {1, 1, 0, 1, 0}},
155 {
"┥", {2, 1, 0, 1, 0}},
156 {
"┦", {1, 2, 0, 1, 0}},
157 {
"┧", {1, 1, 0, 2, 0}},
158 {
"┨", {1, 2, 0, 2, 0}},
159 {
"┩", {2, 2, 0, 1, 0}},
160 {
"┪", {2, 1, 0, 2, 0}},
161 {
"┫", {2, 2, 0, 2, 0}},
163 {
"┬", {1, 0, 1, 1, 0}},
164 {
"┭", {2, 0, 1, 1, 0}},
165 {
"┮", {1, 0, 2, 1, 0}},
166 {
"┯", {2, 0, 2, 1, 0}},
167 {
"┰", {1, 0, 1, 2, 0}},
168 {
"┱", {2, 0, 1, 2, 0}},
169 {
"┲", {1, 0, 2, 2, 0}},
170 {
"┳", {2, 0, 2, 2, 0}},
172 {
"┴", {1, 1, 1, 0, 0}},
173 {
"┵", {2, 1, 1, 0, 0}},
174 {
"┶", {1, 1, 2, 0, 0}},
175 {
"┷", {2, 1, 2, 0, 0}},
176 {
"┸", {1, 2, 1, 0, 0}},
177 {
"┹", {2, 2, 1, 0, 0}},
178 {
"┺", {1, 2, 2, 0, 0}},
179 {
"┻", {2, 2, 2, 0, 0}},
181 {
"┼", {1, 1, 1, 1, 0}},
182 {
"┽", {2, 1, 1, 1, 0}},
183 {
"┾", {1, 1, 2, 1, 0}},
184 {
"┿", {2, 1, 2, 1, 0}},
185 {
"╀", {1, 2, 1, 1, 0}},
186 {
"╁", {1, 1, 1, 2, 0}},
187 {
"╂", {1, 2, 1, 2, 0}},
188 {
"╃", {2, 2, 1, 1, 0}},
189 {
"╄", {1, 2, 2, 1, 0}},
190 {
"╅", {2, 1, 1, 2, 0}},
191 {
"╆", {1, 1, 2, 2, 0}},
192 {
"╇", {2, 2, 2, 1, 0}},
193 {
"╈", {2, 1, 2, 2, 0}},
194 {
"╉", {2, 2, 1, 2, 0}},
195 {
"╊", {1, 2, 2, 2, 0}},
196 {
"╋", {2, 2, 2, 2, 0}},
198 {
"═", {3, 0, 3, 0, 0}},
199 {
"║", {0, 3, 0, 3, 0}},
201 {
"╒", {0, 0, 3, 1, 0}},
202 {
"╓", {0, 0, 1, 3, 0}},
203 {
"╔", {0, 0, 3, 3, 0}},
205 {
"╕", {3, 0, 0, 1, 0}},
206 {
"╖", {1, 0, 0, 3, 0}},
207 {
"╗", {3, 0, 0, 3, 0}},
209 {
"╘", {0, 1, 3, 0, 0}},
210 {
"╙", {0, 3, 1, 0, 0}},
211 {
"╚", {0, 3, 3, 0, 0}},
213 {
"╛", {3, 1, 0, 0, 0}},
214 {
"╜", {1, 3, 0, 0, 0}},
215 {
"╝", {3, 3, 0, 0, 0}},
217 {
"╞", {0, 1, 3, 1, 0}},
218 {
"╟", {0, 3, 1, 3, 0}},
219 {
"╠", {0, 3, 3, 3, 0}},
221 {
"╡", {3, 1, 0, 1, 0}},
222 {
"╢", {1, 3, 0, 3, 0}},
223 {
"╣", {3, 3, 0, 3, 0}},
225 {
"╤", {3, 0, 3, 1, 0}},
226 {
"╥", {1, 0, 1, 3, 0}},
227 {
"╦", {3, 0, 3, 3, 0}},
229 {
"╧", {3, 1, 3, 0, 0}},
230 {
"╨", {1, 3, 1, 0, 0}},
231 {
"╩", {3, 3, 3, 0, 0}},
233 {
"╪", {3, 1, 3, 1, 0}},
234 {
"╫", {1, 3, 1, 3, 0}},
235 {
"╬", {3, 3, 3, 3, 0}},
237 {
"╭", {0, 0, 1, 1, 1}},
238 {
"╮", {1, 0, 0, 1, 1}},
239 {
"╯", {1, 1, 0, 0, 1}},
240 {
"╰", {0, 1, 1, 0, 1}},
242 {
"╴", {1, 0, 0, 0, 0}},
243 {
"╵", {0, 1, 0, 0, 0}},
244 {
"╶", {0, 0, 1, 0, 0}},
245 {
"╷", {0, 0, 0, 1, 0}},
247 {
"╸", {2, 0, 0, 0, 0}},
248 {
"╹", {0, 2, 0, 0, 0}},
249 {
"╺", {0, 0, 2, 0, 0}},
250 {
"╻", {0, 0, 0, 2, 0}},
252 {
"╼", {1, 0, 2, 0, 0}},
253 {
"╽", {0, 1, 0, 2, 0}},
254 {
"╾", {2, 0, 1, 0, 0}},
255 {
"╿", {0, 2, 0, 1, 0}},
259template <
class A,
class B>
260const std::map<B, A> InvertMap(
const std::map<A, B> input) {
261 std::map<B, A> output;
262 for (
const auto& it : input)
263 output[it.second] = it.first;
267const std::map<TileEncoding, std::string> tile_encoding_inverse =
268 InvertMap(tile_encoding);
270void UpgradeLeftRight(std::string& left, std::string& right) {
271 const auto it_left = tile_encoding.find(left);
272 if (it_left == tile_encoding.end())
274 const auto it_right = tile_encoding.find(right);
275 if (it_right == tile_encoding.end())
278 if (it_left->second.right == 0 && it_right->second.left != 0) {
279 TileEncoding encoding_left = it_left->second;
280 encoding_left.right = it_right->second.left;
281 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
282 if (it_left_upgrade != tile_encoding_inverse.end())
283 left = it_left_upgrade->second;
286 if (it_right->second.left == 0 && it_left->second.right != 0) {
287 TileEncoding encoding_right = it_right->second;
288 encoding_right.left = it_left->second.right;
289 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
290 if (it_right_upgrade != tile_encoding_inverse.end())
291 right = it_right_upgrade->second;
295void UpgradeTopDown(std::string& top, std::string& down) {
296 const auto it_top = tile_encoding.find(top);
297 if (it_top == tile_encoding.end())
299 const auto it_down = tile_encoding.find(down);
300 if (it_down == tile_encoding.end())
303 if (it_top->second.down == 0 && it_down->second.top != 0) {
304 TileEncoding encoding_top = it_top->second;
305 encoding_top.down = it_down->second.top;
306 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
307 if (it_top_down != tile_encoding_inverse.end())
308 top = it_top_down->second;
311 if (it_down->second.top == 0 && it_top->second.down != 0) {
312 TileEncoding encoding_down = it_down->second;
313 encoding_down.top = it_top->second.down;
314 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
315 if (it_down_top != tile_encoding_inverse.end())
316 down = it_down_top->second;
349 : stencil{0, dimx - 1, 0, dimy - 1},
352 pixels_(dimy, std::vector<
Pixel>(dimx)) {
359 SetConsoleOutputCP(CP_UTF8);
360 SetConsoleCP(CP_UTF8);
361 WindowsEmulateVT100Terminal();
368 std::stringstream ss;
370 Pixel previous_pixel;
373 for (
int y = 0; y <
dimy_; ++y) {
375 UpdatePixelStyle(ss, previous_pixel, final_pixel);
378 bool previous_fullwidth =
false;
379 for (
const auto& pixel :
pixels_[y]) {
380 if (!previous_fullwidth) {
381 UpdatePixelStyle(ss, previous_pixel, pixel);
382 ss << pixel.character;
384 previous_fullwidth = (
string_width(pixel.character) == 2);
388 UpdatePixelStyle(ss, previous_pixel, final_pixel);
394 std::cout <<
ToString() <<
'\0' << std::flush;
431 std::stringstream ss;
433 ss << MOVE_LEFT << CLEAR_LINE;
434 for (
int y = 1; y <
dimy_; ++y) {
435 ss << MOVE_UP << CLEAR_LINE;
439 for (
int y = 1; y <
dimy_; ++y) {
457 for (
int y = 1; y <
dimy_; ++y) {
458 for (
int x = 1; x <
dimx_; ++x) {
460 std::string& cur =
pixels_[y][x].character;
461 if (cur.size() != 3u)
465 std::string& left =
pixels_[y][x-1].character;
466 if (left.size() == 3u)
467 UpgradeLeftRight(left, cur);
470 std::string& top =
pixels_[y-1][x].character;
471 if (top.size() == 3u)
472 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.