diff options
author | Ron Yorston <rmy@pobox.com> | 2021-02-17 09:26:33 +0000 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2021-02-17 09:26:33 +0000 |
commit | d2804fc6020aad3f9d0ed914d8c2aefa37ffa6d8 (patch) | |
tree | b83b9f61e99828101664267f339a30f377df2a7c | |
parent | 1e9328224c22baafc4c8f27082eb5e9a83b0b092 (diff) | |
download | busybox-w32-d2804fc6020aad3f9d0ed914d8c2aefa37ffa6d8.tar.gz busybox-w32-d2804fc6020aad3f9d0ed914d8c2aefa37ffa6d8.tar.bz2 busybox-w32-d2804fc6020aad3f9d0ed914d8c2aefa37ffa6d8.zip |
winansi: code shrink and improvements
Commit b0b7ab792 (winansi: code shrink) noted that combining reverse
vidoe escape sequences (e.g. ESC[7;27m) didn't work properly.
Make further changes to improve the situation:
- revert to keeping the current attributes in a static variable;
- when reverse video is enabled switch the intensity as well as the
colour components;
- move the code from set_console_attr() into its only caller,
process_escape();
- use 0xffff instead of 0 as a flag to indicate the attributes
haven't been initialised.
Saves 44 bytes and seems to work better.
-rw-r--r-- | win32/winansi.c | 56 |
1 files changed, 15 insertions, 41 deletions
diff --git a/win32/winansi.c b/win32/winansi.c index 45737bf2d..971702f18 100644 --- a/win32/winansi.c +++ b/win32/winansi.c | |||
@@ -25,7 +25,8 @@ | |||
25 | #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) | 25 | #define FOREGROUND_ALL (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) |
26 | #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE) | 26 | #define BACKGROUND_ALL (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE) |
27 | 27 | ||
28 | static WORD plain_attr = 0; | 28 | static WORD plain_attr = 0xffff; |
29 | static WORD current_attr; | ||
29 | 30 | ||
30 | static HANDLE get_console(void) | 31 | static HANDLE get_console(void) |
31 | { | 32 | { |
@@ -44,8 +45,8 @@ static WORD get_console_attr(void) | |||
44 | 45 | ||
45 | static int is_console(int fd) | 46 | static int is_console(int fd) |
46 | { | 47 | { |
47 | if (!plain_attr) | 48 | if (plain_attr == 0xffff) |
48 | plain_attr = get_console_attr(); | 49 | current_attr = plain_attr = get_console_attr(); |
49 | return isatty(fd) && get_console() != INVALID_HANDLE_VALUE; | 50 | return isatty(fd) && get_console() != INVALID_HANDLE_VALUE; |
50 | } | 51 | } |
51 | 52 | ||
@@ -162,33 +163,6 @@ static void use_alt_buffer(int flag) | |||
162 | _open_osfhandle((intptr_t)console, O_RDWR|O_BINARY); | 163 | _open_osfhandle((intptr_t)console, O_RDWR|O_BINARY); |
163 | } | 164 | } |
164 | 165 | ||
165 | static void set_console_attr(WORD attributes, int invert) | ||
166 | { | ||
167 | HANDLE console = get_console(); | ||
168 | if (invert) { | ||
169 | WORD save = attributes; | ||
170 | attributes &= ~FOREGROUND_ALL; | ||
171 | attributes &= ~BACKGROUND_ALL; | ||
172 | |||
173 | /* This could probably use a bitmask | ||
174 | instead of a series of ifs */ | ||
175 | if (save & FOREGROUND_RED) | ||
176 | attributes |= BACKGROUND_RED; | ||
177 | if (save & FOREGROUND_GREEN) | ||
178 | attributes |= BACKGROUND_GREEN; | ||
179 | if (save & FOREGROUND_BLUE) | ||
180 | attributes |= BACKGROUND_BLUE; | ||
181 | |||
182 | if (save & BACKGROUND_RED) | ||
183 | attributes |= FOREGROUND_RED; | ||
184 | if (save & BACKGROUND_GREEN) | ||
185 | attributes |= FOREGROUND_GREEN; | ||
186 | if (save & BACKGROUND_BLUE) | ||
187 | attributes |= FOREGROUND_BLUE; | ||
188 | } | ||
189 | SetConsoleTextAttribute(console, attributes); | ||
190 | } | ||
191 | |||
192 | static void clear_buffer(DWORD len, COORD pos) | 166 | static void clear_buffer(DWORD len, COORD pos) |
193 | { | 167 | { |
194 | HANDLE console = get_console(); | 168 | HANDLE console = get_console(); |
@@ -481,9 +455,8 @@ static char *process_escape(char *pos) | |||
481 | char *str, *func; | 455 | char *str, *func; |
482 | char *bel; | 456 | char *bel; |
483 | size_t len; | 457 | size_t len; |
484 | WORD t, attr = get_console_attr(); | 458 | WORD t, attr = current_attr; |
485 | int invert = FALSE; | 459 | static int reverse = 0; |
486 | static int inverse = 0; | ||
487 | 460 | ||
488 | switch (pos[1]) { | 461 | switch (pos[1]) { |
489 | case '[': | 462 | case '[': |
@@ -513,7 +486,7 @@ static char *process_escape(char *pos) | |||
513 | switch (val) { | 486 | switch (val) { |
514 | case 0: /* reset */ | 487 | case 0: /* reset */ |
515 | attr = plain_attr; | 488 | attr = plain_attr; |
516 | inverse = 0; | 489 | reverse = 0; |
517 | break; | 490 | break; |
518 | case 1: /* bold */ | 491 | case 1: /* bold */ |
519 | attr |= FOREGROUND_INTENSITY; | 492 | attr |= FOREGROUND_INTENSITY; |
@@ -543,13 +516,11 @@ static char *process_escape(char *pos) | |||
543 | case 25: /* no blink */ | 516 | case 25: /* no blink */ |
544 | attr &= ~BACKGROUND_INTENSITY; | 517 | attr &= ~BACKGROUND_INTENSITY; |
545 | break; | 518 | break; |
546 | case 7: /* inverse on */ | 519 | case 7: /* reverse video on */ |
547 | invert = !inverse; | 520 | reverse = 1; |
548 | inverse = 1; | ||
549 | break; | 521 | break; |
550 | case 27: /* inverse off */ | 522 | case 27: /* reverse video off */ |
551 | invert = inverse; | 523 | reverse = 0; |
552 | inverse = 0; | ||
553 | break; | 524 | break; |
554 | case 8: /* conceal */ | 525 | case 8: /* conceal */ |
555 | case 9: /* strike through */ | 526 | case 9: /* strike through */ |
@@ -612,7 +583,10 @@ static char *process_escape(char *pos) | |||
612 | str++; | 583 | str++; |
613 | } while (*(str-1) == ';'); | 584 | } while (*(str-1) == ';'); |
614 | 585 | ||
615 | set_console_attr(attr, invert); | 586 | current_attr = attr; |
587 | if (reverse) | ||
588 | attr = ((attr >> 4) & 0xf) | ((attr << 4) & 0xf0); | ||
589 | SetConsoleTextAttribute(get_console(), attr); | ||
616 | break; | 590 | break; |
617 | case 'A': /* up */ | 591 | case 'A': /* up */ |
618 | move_cursor_row(-strtol(str, (char **)&str, 10)); | 592 | move_cursor_row(-strtol(str, (char **)&str, 10)); |