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;
101 bool operator<(
const TileEncoding& other)
const {
109 return a.output < b.output;
114const std::map<std::string, TileEncoding> tile_encoding = {
115 {
"─", {1, 0, 1, 0, 0}},
116 {
"━", {2, 0, 2, 0, 0}},
118 {
"│", {0, 1, 0, 1, 0}},
119 {
"┃", {0, 2, 0, 2, 0}},
121 {
"┌", {0, 0, 1, 1, 0}},
122 {
"┍", {0, 0, 2, 1, 0}},
123 {
"┎", {0, 0, 1, 2, 0}},
124 {
"┏", {0, 0, 2, 2, 0}},
126 {
"┐", {1, 0, 0, 1, 0}},
127 {
"┑", {2, 0, 0, 1, 0}},
128 {
"┒", {1, 0, 0, 2, 0}},
129 {
"┓", {2, 0, 0, 2, 0}},
131 {
"└", {0, 1, 1, 0, 0}},
132 {
"┕", {0, 1, 2, 0, 0}},
133 {
"┖", {0, 2, 1, 0, 0}},
134 {
"┗", {0, 2, 2, 0, 0}},
136 {
"┘", {1, 1, 0, 0, 0}},
137 {
"┙", {2, 1, 0, 0, 0}},
138 {
"┚", {1, 2, 0, 0, 0}},
139 {
"┛", {2, 2, 0, 0, 0}},
141 {
"├", {0, 1, 1, 1, 0}},
142 {
"┝", {0, 1, 2, 1, 0}},
143 {
"┞", {0, 2, 1, 1, 0}},
144 {
"┟", {0, 1, 1, 2, 0}},
145 {
"┠", {0, 2, 1, 2, 0}},
146 {
"┡", {0, 2, 2, 1, 0}},
147 {
"┢", {0, 1, 2, 2, 0}},
148 {
"┣", {0, 2, 2, 2, 0}},
150 {
"┤", {1, 1, 0, 1, 0}},
151 {
"┥", {2, 1, 0, 1, 0}},
152 {
"┦", {1, 2, 0, 1, 0}},
153 {
"┧", {1, 1, 0, 2, 0}},
154 {
"┨", {1, 2, 0, 2, 0}},
155 {
"┩", {2, 2, 0, 1, 0}},
156 {
"┪", {2, 1, 0, 2, 0}},
157 {
"┫", {2, 2, 0, 2, 0}},
159 {
"┬", {1, 0, 1, 1, 0}},
160 {
"┭", {2, 0, 1, 1, 0}},
161 {
"┮", {1, 0, 2, 1, 0}},
162 {
"┯", {2, 0, 2, 1, 0}},
163 {
"┰", {1, 0, 1, 2, 0}},
164 {
"┱", {2, 0, 1, 2, 0}},
165 {
"┲", {1, 0, 2, 2, 0}},
166 {
"┳", {2, 0, 2, 2, 0}},
168 {
"┴", {1, 1, 1, 0, 0}},
169 {
"┵", {2, 1, 1, 0, 0}},
170 {
"┶", {1, 1, 2, 0, 0}},
171 {
"┷", {2, 1, 2, 0, 0}},
172 {
"┸", {1, 2, 1, 0, 0}},
173 {
"┹", {2, 2, 1, 0, 0}},
174 {
"┺", {1, 2, 2, 0, 0}},
175 {
"┻", {2, 2, 2, 0, 0}},
177 {
"┼", {1, 1, 1, 1, 0}},
178 {
"┽", {2, 1, 1, 1, 0}},
179 {
"┾", {1, 1, 2, 1, 0}},
180 {
"┿", {2, 1, 2, 1, 0}},
181 {
"╀", {1, 2, 1, 1, 0}},
182 {
"╁", {1, 1, 1, 2, 0}},
183 {
"╂", {1, 2, 1, 2, 0}},
184 {
"╃", {2, 2, 1, 1, 0}},
185 {
"╄", {1, 2, 2, 1, 0}},
186 {
"╅", {2, 1, 1, 2, 0}},
187 {
"╆", {1, 1, 2, 2, 0}},
188 {
"╇", {2, 2, 2, 1, 0}},
189 {
"╈", {2, 1, 2, 2, 0}},
190 {
"╉", {2, 2, 1, 2, 0}},
191 {
"╊", {1, 2, 2, 2, 0}},
192 {
"╋", {2, 2, 2, 2, 0}},
194 {
"═", {3, 0, 3, 0, 0}},
195 {
"║", {0, 3, 0, 3, 0}},
197 {
"╒", {0, 0, 3, 1, 0}},
198 {
"╓", {0, 0, 1, 3, 0}},
199 {
"╔", {0, 0, 3, 3, 0}},
201 {
"╕", {3, 0, 0, 1, 0}},
202 {
"╖", {1, 0, 0, 3, 0}},
203 {
"╗", {3, 0, 0, 3, 0}},
205 {
"╘", {0, 1, 3, 0, 0}},
206 {
"╙", {0, 3, 1, 0, 0}},
207 {
"╚", {0, 3, 3, 0, 0}},
209 {
"╛", {3, 1, 0, 0, 0}},
210 {
"╜", {1, 3, 0, 0, 0}},
211 {
"╝", {3, 3, 0, 0, 0}},
213 {
"╞", {0, 1, 3, 1, 0}},
214 {
"╟", {0, 3, 1, 3, 0}},
215 {
"╠", {0, 3, 3, 3, 0}},
217 {
"╡", {3, 1, 0, 1, 0}},
218 {
"╢", {1, 3, 0, 3, 0}},
219 {
"╣", {3, 3, 0, 3, 0}},
221 {
"╤", {3, 0, 3, 1, 0}},
222 {
"╥", {1, 0, 1, 3, 0}},
223 {
"╦", {3, 0, 3, 3, 0}},
225 {
"╧", {3, 1, 3, 0, 0}},
226 {
"╨", {1, 3, 1, 0, 0}},
227 {
"╩", {3, 3, 3, 0, 0}},
229 {
"╪", {3, 1, 3, 1, 0}},
230 {
"╫", {1, 3, 1, 3, 0}},
231 {
"╬", {3, 3, 3, 3, 0}},
233 {
"╭", {0, 0, 1, 1, 1}},
234 {
"╮", {1, 0, 0, 1, 1}},
235 {
"╯", {1, 1, 0, 0, 1}},
236 {
"╰", {0, 1, 1, 0, 1}},
238 {
"╴", {1, 0, 0, 0, 0}},
239 {
"╵", {0, 1, 0, 0, 0}},
240 {
"╶", {0, 0, 1, 0, 0}},
241 {
"╷", {0, 0, 0, 1, 0}},
243 {
"╸", {2, 0, 0, 0, 0}},
244 {
"╹", {0, 2, 0, 0, 0}},
245 {
"╺", {0, 0, 2, 0, 0}},
246 {
"╻", {0, 0, 0, 2, 0}},
248 {
"╼", {1, 0, 2, 0, 0}},
249 {
"╽", {0, 1, 0, 2, 0}},
250 {
"╾", {2, 0, 1, 0, 0}},
251 {
"╿", {0, 2, 0, 1, 0}},
255template <
class A,
class B>
256const std::map<B, A> InvertMap(
const std::map<A, B> input) {
257 std::map<B, A> output;
258 for (
const auto& it : input)
259 output[it.second] = it.first;
263const std::map<TileEncoding, std::string> tile_encoding_inverse =
264 InvertMap(tile_encoding);
266void UpgradeLeftRight(std::string& left, std::string& right) {
267 const auto it_left = tile_encoding.find(left);
268 if (it_left == tile_encoding.end())
270 const auto it_right = tile_encoding.find(right);
271 if (it_right == tile_encoding.end())
274 if (it_left->second.right == 0 && it_right->second.left != 0) {
275 TileEncoding encoding_left = it_left->second;
276 encoding_left.right = it_right->second.left;
277 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
278 if (it_left_upgrade != tile_encoding_inverse.end())
279 left = it_left_upgrade->second;
282 if (it_right->second.left == 0 && it_left->second.right != 0) {
283 TileEncoding encoding_right = it_right->second;
284 encoding_right.left = it_left->second.right;
285 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
286 if (it_right_upgrade != tile_encoding_inverse.end())
287 right = it_right_upgrade->second;
291void UpgradeTopDown(std::string& top, std::string& down) {
292 const auto it_top = tile_encoding.find(top);
293 if (it_top == tile_encoding.end())
295 const auto it_down = tile_encoding.find(down);
296 if (it_down == tile_encoding.end())
299 if (it_top->second.down == 0 && it_down->second.top != 0) {
300 TileEncoding encoding_top = it_top->second;
301 encoding_top.down = it_down->second.top;
302 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
303 if (it_top_down != tile_encoding_inverse.end())
304 top = it_top_down->second;
307 if (it_down->second.top == 0 && it_top->second.down != 0) {
308 TileEncoding encoding_down = it_down->second;
309 encoding_down.top = it_top->second.down;
310 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
311 if (it_down_top != tile_encoding_inverse.end())
312 down = it_down_top->second;
345 : stencil{0, dimx - 1, 0, dimy - 1},
348 pixels_(dimy, std::vector<
Pixel>(dimx)) {
355 SetConsoleOutputCP(CP_UTF8);
356 SetConsoleCP(CP_UTF8);
357 WindowsEmulateVT100Terminal();
364 std::stringstream ss;
366 Pixel previous_pixel;
369 for (
int y = 0; y <
dimy_; ++y) {
371 UpdatePixelStyle(ss, previous_pixel, final_pixel);
374 bool previous_fullwidth =
false;
375 for (
const auto& pixel :
pixels_[y]) {
376 if (!previous_fullwidth) {
377 UpdatePixelStyle(ss, previous_pixel, pixel);
378 ss << pixel.character;
380 previous_fullwidth = (
string_width(pixel.character) == 2);
384 UpdatePixelStyle(ss, previous_pixel, final_pixel);
390 std::cout <<
ToString() <<
'\0' << std::flush;
427 std::stringstream ss;
429 ss << MOVE_LEFT << CLEAR_LINE;
430 for (
int y = 1; y <
dimy_; ++y) {
431 ss << MOVE_UP << CLEAR_LINE;
435 for (
int y = 1; y <
dimy_; ++y) {
453 for (
int y = 1; y <
dimy_; ++y) {
454 for (
int x = 1; x <
dimx_; ++x) {
456 std::string& cur =
pixels_[y][x].character;
457 if (cur.size() != 3u)
461 std::string& left =
pixels_[y][x-1].character;
462 if (left.size() == 3u)
463 UpgradeLeftRight(left, cur);
466 std::string& top =
pixels_[y-1][x].character;
467 if (top.size() == 3u)
468 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.