13#define WIN32_LEAN_AND_MEAN
24Pixel& dev_null_pixel() {
30void WindowsEmulateVT100Terminal() {
31 static bool done =
false;
37 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
40 GetConsoleMode(stdout_handle, &out_mode);
43 const int enable_virtual_terminal_processing = 0x0004;
44 const int disable_newline_auto_return = 0x0008;
45 out_mode |= enable_virtual_terminal_processing;
46 out_mode |= disable_newline_auto_return;
48 SetConsoleMode(stdout_handle, out_mode);
52void UpdatePixelStyle(std::stringstream& ss,
55 if (next == previous) {
59 if (next.bold && !previous.bold) {
63 if (!next.bold && previous.bold) {
67 if (next.dim && !previous.dim) {
71 if (!next.dim && previous.dim) {
75 if (next.underlined && !previous.underlined) {
79 if (!next.underlined && previous.underlined) {
83 if (next.blink && !previous.blink) {
87 if (!next.blink && previous.blink) {
91 if (next.inverted && !previous.inverted) {
95 if (!next.inverted && previous.inverted) {
99 if (next.foreground_color != previous.foreground_color ||
100 next.background_color != previous.background_color) {
101 ss <<
"\x1B[" + next.foreground_color.Print(
false) +
"m";
102 ss <<
"\x1B[" + next.background_color.Print(
true) +
"m";
116 bool operator<(
const TileEncoding& other)
const {
117 if (left < other.left) {
return true; }
118 if (left > other.left) {
return false; }
119 if (top < other.top) {
return true; }
120 if (top > other.top) {
return false; }
121 if (right < other.right) {
return true; }
122 if (right > other.right) {
return false; }
123 if (down < other.down) {
return true; }
124 if (down > other.down) {
return false; }
125 if (round < other.round) {
return true; }
126 if (round > other.round) {
return false; }
133const std::map<std::string, TileEncoding> tile_encoding = {
134 {
"─", {1, 0, 1, 0, 0}},
135 {
"━", {2, 0, 2, 0, 0}},
137 {
"│", {0, 1, 0, 1, 0}},
138 {
"┃", {0, 2, 0, 2, 0}},
140 {
"┌", {0, 0, 1, 1, 0}},
141 {
"┍", {0, 0, 2, 1, 0}},
142 {
"┎", {0, 0, 1, 2, 0}},
143 {
"┏", {0, 0, 2, 2, 0}},
145 {
"┐", {1, 0, 0, 1, 0}},
146 {
"┑", {2, 0, 0, 1, 0}},
147 {
"┒", {1, 0, 0, 2, 0}},
148 {
"┓", {2, 0, 0, 2, 0}},
150 {
"└", {0, 1, 1, 0, 0}},
151 {
"┕", {0, 1, 2, 0, 0}},
152 {
"┖", {0, 2, 1, 0, 0}},
153 {
"┗", {0, 2, 2, 0, 0}},
155 {
"┘", {1, 1, 0, 0, 0}},
156 {
"┙", {2, 1, 0, 0, 0}},
157 {
"┚", {1, 2, 0, 0, 0}},
158 {
"┛", {2, 2, 0, 0, 0}},
160 {
"├", {0, 1, 1, 1, 0}},
161 {
"┝", {0, 1, 2, 1, 0}},
162 {
"┞", {0, 2, 1, 1, 0}},
163 {
"┟", {0, 1, 1, 2, 0}},
164 {
"┠", {0, 2, 1, 2, 0}},
165 {
"┡", {0, 2, 2, 1, 0}},
166 {
"┢", {0, 1, 2, 2, 0}},
167 {
"┣", {0, 2, 2, 2, 0}},
169 {
"┤", {1, 1, 0, 1, 0}},
170 {
"┥", {2, 1, 0, 1, 0}},
171 {
"┦", {1, 2, 0, 1, 0}},
172 {
"┧", {1, 1, 0, 2, 0}},
173 {
"┨", {1, 2, 0, 2, 0}},
174 {
"┩", {2, 2, 0, 1, 0}},
175 {
"┪", {2, 1, 0, 2, 0}},
176 {
"┫", {2, 2, 0, 2, 0}},
178 {
"┬", {1, 0, 1, 1, 0}},
179 {
"┭", {2, 0, 1, 1, 0}},
180 {
"┮", {1, 0, 2, 1, 0}},
181 {
"┯", {2, 0, 2, 1, 0}},
182 {
"┰", {1, 0, 1, 2, 0}},
183 {
"┱", {2, 0, 1, 2, 0}},
184 {
"┲", {1, 0, 2, 2, 0}},
185 {
"┳", {2, 0, 2, 2, 0}},
187 {
"┴", {1, 1, 1, 0, 0}},
188 {
"┵", {2, 1, 1, 0, 0}},
189 {
"┶", {1, 1, 2, 0, 0}},
190 {
"┷", {2, 1, 2, 0, 0}},
191 {
"┸", {1, 2, 1, 0, 0}},
192 {
"┹", {2, 2, 1, 0, 0}},
193 {
"┺", {1, 2, 2, 0, 0}},
194 {
"┻", {2, 2, 2, 0, 0}},
196 {
"┼", {1, 1, 1, 1, 0}},
197 {
"┽", {2, 1, 1, 1, 0}},
198 {
"┾", {1, 1, 2, 1, 0}},
199 {
"┿", {2, 1, 2, 1, 0}},
200 {
"╀", {1, 2, 1, 1, 0}},
201 {
"╁", {1, 1, 1, 2, 0}},
202 {
"╂", {1, 2, 1, 2, 0}},
203 {
"╃", {2, 2, 1, 1, 0}},
204 {
"╄", {1, 2, 2, 1, 0}},
205 {
"╅", {2, 1, 1, 2, 0}},
206 {
"╆", {1, 1, 2, 2, 0}},
207 {
"╇", {2, 2, 2, 1, 0}},
208 {
"╈", {2, 1, 2, 2, 0}},
209 {
"╉", {2, 2, 1, 2, 0}},
210 {
"╊", {1, 2, 2, 2, 0}},
211 {
"╋", {2, 2, 2, 2, 0}},
213 {
"═", {3, 0, 3, 0, 0}},
214 {
"║", {0, 3, 0, 3, 0}},
216 {
"╒", {0, 0, 3, 1, 0}},
217 {
"╓", {0, 0, 1, 3, 0}},
218 {
"╔", {0, 0, 3, 3, 0}},
220 {
"╕", {3, 0, 0, 1, 0}},
221 {
"╖", {1, 0, 0, 3, 0}},
222 {
"╗", {3, 0, 0, 3, 0}},
224 {
"╘", {0, 1, 3, 0, 0}},
225 {
"╙", {0, 3, 1, 0, 0}},
226 {
"╚", {0, 3, 3, 0, 0}},
228 {
"╛", {3, 1, 0, 0, 0}},
229 {
"╜", {1, 3, 0, 0, 0}},
230 {
"╝", {3, 3, 0, 0, 0}},
232 {
"╞", {0, 1, 3, 1, 0}},
233 {
"╟", {0, 3, 1, 3, 0}},
234 {
"╠", {0, 3, 3, 3, 0}},
236 {
"╡", {3, 1, 0, 1, 0}},
237 {
"╢", {1, 3, 0, 3, 0}},
238 {
"╣", {3, 3, 0, 3, 0}},
240 {
"╤", {3, 0, 3, 1, 0}},
241 {
"╥", {1, 0, 1, 3, 0}},
242 {
"╦", {3, 0, 3, 3, 0}},
244 {
"╧", {3, 1, 3, 0, 0}},
245 {
"╨", {1, 3, 1, 0, 0}},
246 {
"╩", {3, 3, 3, 0, 0}},
248 {
"╪", {3, 1, 3, 1, 0}},
249 {
"╫", {1, 3, 1, 3, 0}},
250 {
"╬", {3, 3, 3, 3, 0}},
252 {
"╭", {0, 0, 1, 1, 1}},
253 {
"╮", {1, 0, 0, 1, 1}},
254 {
"╯", {1, 1, 0, 0, 1}},
255 {
"╰", {0, 1, 1, 0, 1}},
257 {
"╴", {1, 0, 0, 0, 0}},
258 {
"╵", {0, 1, 0, 0, 0}},
259 {
"╶", {0, 0, 1, 0, 0}},
260 {
"╷", {0, 0, 0, 1, 0}},
262 {
"╸", {2, 0, 0, 0, 0}},
263 {
"╹", {0, 2, 0, 0, 0}},
264 {
"╺", {0, 0, 2, 0, 0}},
265 {
"╻", {0, 0, 0, 2, 0}},
267 {
"╼", {1, 0, 2, 0, 0}},
268 {
"╽", {0, 1, 0, 2, 0}},
269 {
"╾", {2, 0, 1, 0, 0}},
270 {
"╿", {0, 2, 0, 1, 0}},
274template <
class A,
class B>
275std::map<B, A> InvertMap(
const std::map<A, B> input) {
276 std::map<B, A> output;
277 for (
const auto& it : input) {
278 output[it.second] = it.first;
283const std::map<TileEncoding, std::string> tile_encoding_inverse =
284 InvertMap(tile_encoding);
286void UpgradeLeftRight(std::string& left, std::string& right) {
287 const auto it_left = tile_encoding.find(left);
288 if (it_left == tile_encoding.end()) {
291 const auto it_right = tile_encoding.find(right);
292 if (it_right == tile_encoding.end()) {
296 if (it_left->second.right == 0 && it_right->second.left != 0) {
297 TileEncoding encoding_left = it_left->second;
298 encoding_left.right = it_right->second.left;
299 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
300 if (it_left_upgrade != tile_encoding_inverse.end()) {
301 left = it_left_upgrade->second;
305 if (it_right->second.left == 0 && it_left->second.right != 0) {
306 TileEncoding encoding_right = it_right->second;
307 encoding_right.left = it_left->second.right;
308 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
309 if (it_right_upgrade != tile_encoding_inverse.end()) {
310 right = it_right_upgrade->second;
315void UpgradeTopDown(std::string& top, std::string& down) {
316 const auto it_top = tile_encoding.find(top);
317 if (it_top == tile_encoding.end()) {
320 const auto it_down = tile_encoding.find(down);
321 if (it_down == tile_encoding.end()) {
325 if (it_top->second.down == 0 && it_down->second.top != 0) {
326 TileEncoding encoding_top = it_top->second;
327 encoding_top.down = it_down->second.top;
328 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
329 if (it_top_down != tile_encoding_inverse.end()) {
330 top = it_top_down->second;
334 if (it_down->second.top == 0 && it_top->second.down != 0) {
335 TileEncoding encoding_down = it_down->second;
336 encoding_down.top = it_top->second.down;
337 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
338 if (it_down_top != tile_encoding_inverse.end()) {
339 down = it_down_top->second;
344bool ShouldAttemptAutoMerge(Pixel& pixel) {
345 return pixel.automerge && pixel.character.size() == 3;
389 : stencil{0, dimx - 1, 0, dimy - 1},
392 pixels_(dimy, std::vector<
Pixel>(dimx)) {
399 SetConsoleOutputCP(CP_UTF8);
400 SetConsoleCP(CP_UTF8);
401 WindowsEmulateVT100Terminal();
408 std::stringstream ss;
410 Pixel previous_pixel;
413 for (
int y = 0; y <
dimy_; ++y) {
415 UpdatePixelStyle(ss, previous_pixel, final_pixel);
418 bool previous_fullwidth =
false;
419 for (
const auto& pixel :
pixels_[y]) {
420 if (!previous_fullwidth) {
421 UpdatePixelStyle(ss, previous_pixel, pixel);
422 ss << pixel.character;
424 previous_fullwidth = (
string_width(pixel.character) == 2);
428 UpdatePixelStyle(ss, previous_pixel, final_pixel);
434 std::cout <<
ToString() <<
'\0' << std::flush;
471 std::stringstream ss;
475 for (
int y = 1; y <
dimy_; ++y) {
481 for (
int y = 1; y <
dimy_; ++y) {
499 for (
int y = 1; y <
dimy_; ++y) {
500 for (
int x = 1; x <
dimx_; ++x) {
503 if (!ShouldAttemptAutoMerge(cur)) {
510 if (ShouldAttemptAutoMerge(left)) {
513 if (ShouldAttemptAutoMerge(top)) {
A rectangular grid of Pixel.
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)
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::vector< Pixel > > pixels_
int string_width(const std::string &)
bool Contain(int x, int y) const
A unicode character and its associated style.
bool operator==(const Pixel &other) const