Allow color to be used when stdout is redirected with -O

This commit is contained in:
ars-ka0s 2023-01-18 16:54:34 -06:00
parent 91446cb787
commit 74d6cc6bc2
2 changed files with 38 additions and 27 deletions

View File

@ -277,11 +277,16 @@ int main (int argc, char *argv[])
* Default will be no colors if stdout is not a terminal (i.e. piped into
* something else such as "tee") but command line can override this.
*/
for (j=1; j<argc; j++) {
if (strcmp(argv[j], "-O") == 0) {
O_opt = 1;
}
}
#if __WIN32__
t_opt = _isatty(_fileno(stdout)) > 0;
t_opt = _isatty(_fileno(O_opt ? stderr : stdout)) > 0;
#else
t_opt = isatty(fileno(stdout));
t_opt = isatty(fileno(O_opt ? stderr : stdout));
#endif
/* 1 = normal, 0 = no text colors. */
/* 2, 3, ... alternate escape sequences for different terminals. */
@ -292,8 +297,6 @@ int main (int argc, char *argv[])
if (strcmp(argv[j], "-t") == 0) {
t_opt = atoi (argv[j+1]);
//dw_printf ("DEBUG: text color option = %d.\n", t_opt);
} else if (strcmp(argv[j], "-O") == 0) {
O_opt = 1;
}
}

View File

@ -177,7 +177,6 @@ void text_color_init (int enable_color, int redirect_output)
{
if (redirect_output != 0) {
g_dw_printf_dest = stderr;
enable_color = 0;
} else {
g_dw_printf_dest = stdout;
}
@ -195,7 +194,12 @@ void text_color_init (int enable_color, int redirect_output)
COORD coord;
DWORD nwritten;
h = GetStdHandle(STD_OUTPUT_HANDLE);
if (redirect_output != 0) {
h = GetStdHandle(STD_ERROR_HANDLE);
} else {
h = GetStdHandle(STD_OUTPUT_HANDLE);
}
if (h != NULL && h != INVALID_HANDLE_VALUE) {
GetConsoleScreenBufferInfo (h, &csbi);
@ -215,17 +219,17 @@ void text_color_init (int enable_color, int redirect_output)
int t;
for (t = 0; t <= MAX_T; t++) {
text_color_init (t, redirect_output);
printf ("-t %d", t);
if (t) printf (" [white background] ");
printf ("\n");
printf ("%sBlack ", t_black[t]);
printf ("%sRed ", t_red[t]);
printf ("%sGreen ", t_green[t]);
printf ("%sDark-Green ", t_dark_green[t]);
printf ("%sYellow ", t_yellow[t]);
printf ("%sBlue ", t_blue[t]);
printf ("%sMagenta ", t_magenta[t]);
printf ("%sCyan \n", t_cyan[t]);
fprintf (g_dw_printf_dest,"-t %d", t);
if (t) fprintf (g_dw_printf_dest, " [white background] ");
fprintf (g_dw_printf_dest,"\n");
fprintf (g_dw_printf_dest,"%sBlack ", t_black[t]);
fprintf (g_dw_printf_dest,"%sRed ", t_red[t]);
fprintf (g_dw_printf_dest,"%sGreen ", t_green[t]);
fprintf (g_dw_printf_dest,"%sDark-Green ", t_dark_green[t]);
fprintf (g_dw_printf_dest,"%sYellow ", t_yellow[t]);
fprintf (g_dw_printf_dest,"%sBlue ", t_blue[t]);
fprintf (g_dw_printf_dest, "%sMagenta ", t_magenta[t]);
fprintf (g_dw_printf_dest, "%sCyan \n", t_cyan[t]);
}
exit (EXIT_SUCCESS);
}
@ -238,9 +242,9 @@ void text_color_init (int enable_color, int redirect_output)
if (t < 0) t = 0;
if (t > MAX_T) t = MAX_T;
printf ("%s", t_background_white[t]);
printf ("%s", clear_eos);
printf ("%s", t_black[t]);
fprintf (g_dw_printf_dest, "%s", t_background_white[t]);
fprintf (g_dw_printf_dest, "%s", clear_eos);
fprintf (g_dw_printf_dest, "%s", t_black[t]);
}
#endif
}
@ -291,7 +295,11 @@ void text_color_set ( enum dw_color_e c )
break;
}
h = GetStdHandle(STD_OUTPUT_HANDLE);
if (dw_printf_redirected()) {
h = GetStdHandle(STD_ERROR_HANDLE);
} else {
h = GetStdHandle(STD_OUTPUT_HANDLE);
}
if (h != NULL && h != INVALID_HANDLE_VALUE) {
SetConsoleTextAttribute (h, attr);
@ -316,30 +324,30 @@ void text_color_set ( enum dw_color_e c )
default:
case DW_COLOR_INFO:
printf ("%s", t_black[t]);
fprintf (g_dw_printf_dest, "%s", t_black[t]);
break;
case DW_COLOR_ERROR:
printf ("%s", t_red[t]);
fprintf (g_dw_printf_dest, "%s", t_red[t]);
break;
case DW_COLOR_REC:
// Bright green is very difficult to read against a while background.
// Let's use dark green instead. release 1.6.
//printf ("%s", t_green[t]);
printf ("%s", t_dark_green[t]);
fprintf (g_dw_printf_dest, "%s", t_dark_green[t]);
break;
case DW_COLOR_DECODED:
printf ("%s", t_blue[t]);
fprintf (g_dw_printf_dest, "%s", t_blue[t]);
break;
case DW_COLOR_XMIT:
printf ("%s", t_magenta[t]);
fprintf (g_dw_printf_dest, "%s", t_magenta[t]);
break;
case DW_COLOR_DEBUG:
printf ("%s", t_dark_green[t]);
fprintf (g_dw_printf_dest, "%s", t_dark_green[t]);
break;
}
}