From 2157d2a7f99e5fce7438a3c804e1c4c119746717 Mon Sep 17 00:00:00 2001 From: ars-ka0s <26339355+ars-ka0s@users.noreply.github.com> Date: Wed, 18 Jan 2023 18:57:40 -0600 Subject: [PATCH] Add text color options with black background. --- src/textcolor.c | 62 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/textcolor.c b/src/textcolor.c index dea90f0..7ddf74a 100644 --- a/src/textcolor.c +++ b/src/textcolor.c @@ -89,7 +89,13 @@ #if __WIN32__ +// For Windows platform: +// -t 0 disables color +// -t 1 enables with white background (default) +// -t 2 enables with black background + #define BACKGROUND_WHITE (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY) +#define FOREGROUND_WHITE (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY) #else /* Linux, BSD, Mac OSX */ @@ -133,18 +139,28 @@ // In recent tests, background is always gray, not white like it should be. -#define MAX_T 4 +// Alternative 5: -static const char *t_background_white[MAX_T+1] = { "", "\e[48;2;255;255;255m", "\e[48;2;255;255;255m", "\e[5;47m", "\e[1;47m" }; +// This uses the 8-color foreground colors like alternative 2, but with a reset background color. +// For people that prefer dark backgrounds or use terminals with color theming. -static const char *t_black[MAX_T+1] = { "", "\e[38;2;0;0;0m", "\e[0;30m" "\e[48;2;255;255;255m", "\e[0;30m" "\e[5;47m", "\e[0;30m" "\e[1;47m" }; -static const char *t_red[MAX_T+1] = { "", "\e[38;2;255;0;0m", "\e[1;31m" "\e[48;2;255;255;255m", "\e[1;31m" "\e[5;47m", "\e[1;31m" "\e[1;47m" }; -static const char *t_green[MAX_T+1] = { "", "\e[38;2;0;255;0m", "\e[1;32m" "\e[48;2;255;255;255m", "\e[1;32m" "\e[5;47m", "\e[1;32m" "\e[1;47m" }; -static const char *t_dark_green[MAX_T+1]= { "", "\e[38;2;0;192;0m", "\e[0;32m" "\e[48;2;255;255;255m", "\e[0;32m" "\e[5;47m", "\e[0;32m" "\e[1;47m" }; -static const char *t_yellow[MAX_T+1] = { "", "\e[38;2;255;255;0m", "\e[1;33m" "\e[48;2;255;255;255m", "\e[1;33m" "\e[5;47m", "\e[1;33m" "\e[1;47m" }; -static const char *t_blue[MAX_T+1] = { "", "\e[38;2;0;0;255m", "\e[1;34m" "\e[48;2;255;255;255m", "\e[1;34m" "\e[5;47m", "\e[1;34m" "\e[1;47m" }; -static const char *t_magenta[MAX_T+1] = { "", "\e[38;2;255;0;255m", "\e[1;35m" "\e[48;2;255;255;255m", "\e[1;35m" "\e[5;47m", "\e[1;35m" "\e[1;47m" }; -static const char *t_cyan[MAX_T+1] = { "", "\e[38;2;0;255;255m", "\e[0;36m" "\e[48;2;255;255;255m", "\e[0;36m" "\e[5;47m", "\e[0;36m" "\e[1;47m" }; + +// Alternative 6: + +// This uses the RGB color setting like alternative 1, but with a black background. + +#define MAX_T 6 + +static const char *t_background_white[MAX_T+1] = { "", "\e[48;2;255;255;255m", "\e[48;2;255;255;255m", "\e[5;47m", "\e[1;47m", "\e[0;49m" , "\e[48;2;0;0;0m" }; + +static const char *t_black[MAX_T+1] = { "", "\e[38;2;0;0;0m", "\e[0;30m" "\e[48;2;255;255;255m", "\e[0;30m" "\e[5;47m", "\e[0;30m" "\e[1;47m", "\e[0;49m" "\e[0;39m", "\e[38;2;255;255;255m" }; +static const char *t_red[MAX_T+1] = { "", "\e[38;2;255;0;0m", "\e[1;31m" "\e[48;2;255;255;255m", "\e[1;31m" "\e[5;47m", "\e[1;31m" "\e[1;47m", "\e[0;49m" "\e[1;31m", "\e[38;2;255;0;0m"}; +static const char *t_green[MAX_T+1] = { "", "\e[38;2;0;255;0m", "\e[1;32m" "\e[48;2;255;255;255m", "\e[1;32m" "\e[5;47m", "\e[1;32m" "\e[1;47m", "\e[0;49m" "\e[1;32m", "\e[38;2;0;255;0m"}; +static const char *t_dark_green[MAX_T+1]= { "", "\e[38;2;0;192;0m", "\e[0;32m" "\e[48;2;255;255;255m", "\e[0;32m" "\e[5;47m", "\e[0;32m" "\e[1;47m", "\e[0;49m" "\e[0;32m", "\e[38;2;0;192;0m"}; +static const char *t_yellow[MAX_T+1] = { "", "\e[38;2;255;255;0m", "\e[1;33m" "\e[48;2;255;255;255m", "\e[1;33m" "\e[5;47m", "\e[1;33m" "\e[1;47m", "\e[0;49m" "\e[1;33m", "\e[38;2;255;255;0m"}; +static const char *t_blue[MAX_T+1] = { "", "\e[38;2;0;0;255m", "\e[1;34m" "\e[48;2;255;255;255m", "\e[1;34m" "\e[5;47m", "\e[1;34m" "\e[1;47m", "\e[0;49m" "\e[1;34m", "\e[38;2;0;0;255m"}; +static const char *t_magenta[MAX_T+1] = { "", "\e[38;2;255;0;255m", "\e[1;35m" "\e[48;2;255;255;255m", "\e[1;35m" "\e[5;47m", "\e[1;35m" "\e[1;47m", "\e[0;49m" "\e[1;35m", "\e[38;2;255;0;255m"}; +static const char *t_cyan[MAX_T+1] = { "", "\e[38;2;0;255;255m", "\e[0;36m" "\e[48;2;255;255;255m", "\e[0;36m" "\e[5;47m", "\e[0;36m" "\e[1;47m", "\e[0;49m" "\e[0;36m", "\e[38;2;0;255;255m"}; /* Clear from cursor to end of screen. */ @@ -179,6 +195,7 @@ void text_color_init (int enable_color) #if __WIN32__ +g_enable_color = enable_color; if (g_enable_color != 0) { @@ -189,6 +206,10 @@ void text_color_init (int enable_color) COORD coord; DWORD nwritten; + if (g_enable_color > 1) { + attr = 0; + } + h = GetStdHandle(STD_OUTPUT_HANDLE); if (h != NULL && h != INVALID_HANDLE_VALUE) { @@ -210,7 +231,7 @@ void text_color_init (int enable_color) for (t = 0; t <= MAX_T; t++) { text_color_init (t); printf ("-t %d", t); - if (t) printf (" [white background] "); + if (t < 5) printf (" [white background] "); printf ("\n"); printf ("%sBlack ", t_black[t]); printf ("%sRed ", t_red[t]); @@ -254,34 +275,41 @@ void text_color_set ( enum dw_color_e c ) return; } + WORD info_fg = 0; + WORD background = BACKGROUND_WHITE; + if (g_enable_color > 1) { + info_fg = FOREGROUND_WHITE; + background = 0; + } + switch (c) { default: case DW_COLOR_INFO: - attr = BACKGROUND_WHITE; + attr = info_fg | background; break; case DW_COLOR_ERROR: - attr = FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_WHITE; + attr = FOREGROUND_RED | FOREGROUND_INTENSITY | background; break; case DW_COLOR_REC: // Release 1.6. Dark green, same as for debug. // Bright green is too hard to see with white background, // attr = FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_WHITE; - attr = FOREGROUND_GREEN | BACKGROUND_WHITE; + attr = FOREGROUND_GREEN | background; break; case DW_COLOR_DECODED: - attr = FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_WHITE; + attr = FOREGROUND_BLUE | FOREGROUND_INTENSITY | background; break; case DW_COLOR_XMIT: - attr = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_WHITE; + attr = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY | background; break; case DW_COLOR_DEBUG: - attr = FOREGROUND_GREEN | BACKGROUND_WHITE; + attr = FOREGROUND_GREEN | background; break; }