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);
53void UpdatePixelStyle(std::stringstream& ss,
56 if (next == previous) {
60 if ((!next.bold && previous.bold) ||
61 (!next.dim && previous.dim)) {
66 previous.bold =
false;
70 if ((!next.underlined && previous.underlined) ||
71 (!next.underlined_double && previous.underlined_double)) {
76 previous.underlined =
false;
77 previous.underlined_double =
false;
80 if (next.bold && !previous.bold) {
84 if (next.dim && !previous.dim) {
88 if (next.underlined && !previous.underlined) {
92 if (next.blink && !previous.blink) {
96 if (!next.blink && previous.blink) {
100 if (next.inverted && !previous.inverted) {
104 if (!next.inverted && previous.inverted) {
108 if (next.strikethrough && !previous.strikethrough) {
112 if (!next.strikethrough && previous.strikethrough) {
116 if (next.underlined_double && !previous.underlined_double) {
120 if (next.foreground_color != previous.foreground_color ||
121 next.background_color != previous.background_color) {
122 ss <<
"\x1B[" + next.foreground_color.Print(
false) +
"m";
123 ss <<
"\x1B[" + next.background_color.Print(
true) +
"m";
137 bool operator<(
const TileEncoding& other)
const {
138 if (left < other.left) {
return true; }
139 if (left > other.left) {
return false; }
140 if (top < other.top) {
return true; }
141 if (top > other.top) {
return false; }
142 if (right < other.right) {
return true; }
143 if (right > other.right) {
return false; }
144 if (down < other.down) {
return true; }
145 if (down > other.down) {
return false; }
146 if (round < other.round) {
return true; }
147 if (round > other.round) {
return false; }
154const std::map<std::string, TileEncoding> tile_encoding = {
155 {
"─", {1, 0, 1, 0, 0}},
156 {
"━", {2, 0, 2, 0, 0}},
158 {
"│", {0, 1, 0, 1, 0}},
159 {
"┃", {0, 2, 0, 2, 0}},
161 {
"┌", {0, 0, 1, 1, 0}},
162 {
"┍", {0, 0, 2, 1, 0}},
163 {
"┎", {0, 0, 1, 2, 0}},
164 {
"┏", {0, 0, 2, 2, 0}},
166 {
"┐", {1, 0, 0, 1, 0}},
167 {
"┑", {2, 0, 0, 1, 0}},
168 {
"┒", {1, 0, 0, 2, 0}},
169 {
"┓", {2, 0, 0, 2, 0}},
171 {
"└", {0, 1, 1, 0, 0}},
172 {
"┕", {0, 1, 2, 0, 0}},
173 {
"┖", {0, 2, 1, 0, 0}},
174 {
"┗", {0, 2, 2, 0, 0}},
176 {
"┘", {1, 1, 0, 0, 0}},
177 {
"┙", {2, 1, 0, 0, 0}},
178 {
"┚", {1, 2, 0, 0, 0}},
179 {
"┛", {2, 2, 0, 0, 0}},
181 {
"├", {0, 1, 1, 1, 0}},
182 {
"┝", {0, 1, 2, 1, 0}},
183 {
"┞", {0, 2, 1, 1, 0}},
184 {
"┟", {0, 1, 1, 2, 0}},
185 {
"┠", {0, 2, 1, 2, 0}},
186 {
"┡", {0, 2, 2, 1, 0}},
187 {
"┢", {0, 1, 2, 2, 0}},
188 {
"┣", {0, 2, 2, 2, 0}},
190 {
"┤", {1, 1, 0, 1, 0}},
191 {
"┥", {2, 1, 0, 1, 0}},
192 {
"┦", {1, 2, 0, 1, 0}},
193 {
"┧", {1, 1, 0, 2, 0}},
194 {
"┨", {1, 2, 0, 2, 0}},
195 {
"┩", {2, 2, 0, 1, 0}},
196 {
"┪", {2, 1, 0, 2, 0}},
197 {
"┫", {2, 2, 0, 2, 0}},
199 {
"┬", {1, 0, 1, 1, 0}},
200 {
"┭", {2, 0, 1, 1, 0}},
201 {
"┮", {1, 0, 2, 1, 0}},
202 {
"┯", {2, 0, 2, 1, 0}},
203 {
"┰", {1, 0, 1, 2, 0}},
204 {
"┱", {2, 0, 1, 2, 0}},
205 {
"┲", {1, 0, 2, 2, 0}},
206 {
"┳", {2, 0, 2, 2, 0}},
208 {
"┴", {1, 1, 1, 0, 0}},
209 {
"┵", {2, 1, 1, 0, 0}},
210 {
"┶", {1, 1, 2, 0, 0}},
211 {
"┷", {2, 1, 2, 0, 0}},
212 {
"┸", {1, 2, 1, 0, 0}},
213 {
"┹", {2, 2, 1, 0, 0}},
214 {
"┺", {1, 2, 2, 0, 0}},
215 {
"┻", {2, 2, 2, 0, 0}},
217 {
"┼", {1, 1, 1, 1, 0}},
218 {
"┽", {2, 1, 1, 1, 0}},
219 {
"┾", {1, 1, 2, 1, 0}},
220 {
"┿", {2, 1, 2, 1, 0}},
221 {
"╀", {1, 2, 1, 1, 0}},
222 {
"╁", {1, 1, 1, 2, 0}},
223 {
"╂", {1, 2, 1, 2, 0}},
224 {
"╃", {2, 2, 1, 1, 0}},
225 {
"╄", {1, 2, 2, 1, 0}},
226 {
"╅", {2, 1, 1, 2, 0}},
227 {
"╆", {1, 1, 2, 2, 0}},
228 {
"╇", {2, 2, 2, 1, 0}},
229 {
"╈", {2, 1, 2, 2, 0}},
230 {
"╉", {2, 2, 1, 2, 0}},
231 {
"╊", {1, 2, 2, 2, 0}},
232 {
"╋", {2, 2, 2, 2, 0}},
234 {
"═", {3, 0, 3, 0, 0}},
235 {
"║", {0, 3, 0, 3, 0}},
237 {
"╒", {0, 0, 3, 1, 0}},
238 {
"╓", {0, 0, 1, 3, 0}},
239 {
"╔", {0, 0, 3, 3, 0}},
241 {
"╕", {3, 0, 0, 1, 0}},
242 {
"╖", {1, 0, 0, 3, 0}},
243 {
"╗", {3, 0, 0, 3, 0}},
245 {
"╘", {0, 1, 3, 0, 0}},
246 {
"╙", {0, 3, 1, 0, 0}},
247 {
"╚", {0, 3, 3, 0, 0}},
249 {
"╛", {3, 1, 0, 0, 0}},
250 {
"╜", {1, 3, 0, 0, 0}},
251 {
"╝", {3, 3, 0, 0, 0}},
253 {
"╞", {0, 1, 3, 1, 0}},
254 {
"╟", {0, 3, 1, 3, 0}},
255 {
"╠", {0, 3, 3, 3, 0}},
257 {
"╡", {3, 1, 0, 1, 0}},
258 {
"╢", {1, 3, 0, 3, 0}},
259 {
"╣", {3, 3, 0, 3, 0}},
261 {
"╤", {3, 0, 3, 1, 0}},
262 {
"╥", {1, 0, 1, 3, 0}},
263 {
"╦", {3, 0, 3, 3, 0}},
265 {
"╧", {3, 1, 3, 0, 0}},
266 {
"╨", {1, 3, 1, 0, 0}},
267 {
"╩", {3, 3, 3, 0, 0}},
269 {
"╪", {3, 1, 3, 1, 0}},
270 {
"╫", {1, 3, 1, 3, 0}},
271 {
"╬", {3, 3, 3, 3, 0}},
273 {
"╭", {0, 0, 1, 1, 1}},
274 {
"╮", {1, 0, 0, 1, 1}},
275 {
"╯", {1, 1, 0, 0, 1}},
276 {
"╰", {0, 1, 1, 0, 1}},
278 {
"╴", {1, 0, 0, 0, 0}},
279 {
"╵", {0, 1, 0, 0, 0}},
280 {
"╶", {0, 0, 1, 0, 0}},
281 {
"╷", {0, 0, 0, 1, 0}},
283 {
"╸", {2, 0, 0, 0, 0}},
284 {
"╹", {0, 2, 0, 0, 0}},
285 {
"╺", {0, 0, 2, 0, 0}},
286 {
"╻", {0, 0, 0, 2, 0}},
288 {
"╼", {1, 0, 2, 0, 0}},
289 {
"╽", {0, 1, 0, 2, 0}},
290 {
"╾", {2, 0, 1, 0, 0}},
291 {
"╿", {0, 2, 0, 1, 0}},
295template <
class A,
class B>
296std::map<B, A> InvertMap(
const std::map<A, B> input) {
297 std::map<B, A> output;
298 for (
const auto& it : input) {
299 output[it.second] = it.first;
304const std::map<TileEncoding, std::string> tile_encoding_inverse =
305 InvertMap(tile_encoding);
307void UpgradeLeftRight(std::string& left, std::string& right) {
308 const auto it_left = tile_encoding.find(left);
309 if (it_left == tile_encoding.end()) {
312 const auto it_right = tile_encoding.find(right);
313 if (it_right == tile_encoding.end()) {
317 if (it_left->second.right == 0 && it_right->second.left != 0) {
318 TileEncoding encoding_left = it_left->second;
319 encoding_left.right = it_right->second.left;
320 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
321 if (it_left_upgrade != tile_encoding_inverse.end()) {
322 left = it_left_upgrade->second;
326 if (it_right->second.left == 0 && it_left->second.right != 0) {
327 TileEncoding encoding_right = it_right->second;
328 encoding_right.left = it_left->second.right;
329 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
330 if (it_right_upgrade != tile_encoding_inverse.end()) {
331 right = it_right_upgrade->second;
336void UpgradeTopDown(std::string& top, std::string& down) {
337 const auto it_top = tile_encoding.find(top);
338 if (it_top == tile_encoding.end()) {
341 const auto it_down = tile_encoding.find(down);
342 if (it_down == tile_encoding.end()) {
346 if (it_top->second.down == 0 && it_down->second.top != 0) {
347 TileEncoding encoding_top = it_top->second;
348 encoding_top.down = it_down->second.top;
349 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
350 if (it_top_down != tile_encoding_inverse.end()) {
351 top = it_top_down->second;
355 if (it_down->second.top == 0 && it_top->second.down != 0) {
356 TileEncoding encoding_down = it_down->second;
357 encoding_down.top = it_top->second.down;
358 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
359 if (it_down_top != tile_encoding_inverse.end()) {
360 down = it_down_top->second;
365bool ShouldAttemptAutoMerge(Pixel& pixel) {
366 return pixel.automerge && pixel.character.size() == 3;
406 return {dimension.
dimx, dimension.
dimy};
410 : stencil{0, dimx - 1, 0, dimy - 1},
413 pixels_(dimy, std::vector<
Pixel>(dimx)) {
420 SetConsoleOutputCP(CP_UTF8);
421 SetConsoleCP(CP_UTF8);
422 WindowsEmulateVT100Terminal();
429 std::stringstream ss;
431 Pixel previous_pixel;
432 const Pixel final_pixel;
434 for (
int y = 0; y <
dimy_; ++y) {
436 UpdatePixelStyle(ss, previous_pixel, final_pixel);
439 bool previous_fullwidth =
false;
440 for (
const auto& pixel :
pixels_[y]) {
441 if (!previous_fullwidth) {
442 UpdatePixelStyle(ss, previous_pixel, pixel);
443 ss << pixel.character;
445 previous_fullwidth = (
string_width(pixel.character) == 2);
449 UpdatePixelStyle(ss, previous_pixel, final_pixel);
455 std::cout <<
ToString() <<
'\0' << std::flush;
492 std::stringstream ss;
496 for (
int y = 1; y <
dimy_; ++y) {
502 for (
int y = 1; y <
dimy_; ++y) {
512 for (
auto& cell : line) {
523 for (
int y = 0; y <
dimy_; ++y) {
524 for (
int x = 0; x <
dimx_; ++x) {
527 if (!ShouldAttemptAutoMerge(cur)) {
533 if (ShouldAttemptAutoMerge(left)) {
539 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