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 * 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. * 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__ #if __WIN32__
t_opt = _isatty(_fileno(stdout)) > 0; t_opt = _isatty(_fileno(O_opt ? stderr : stdout)) > 0;
#else #else
t_opt = isatty(fileno(stdout)); t_opt = isatty(fileno(O_opt ? stderr : stdout));
#endif #endif
/* 1 = normal, 0 = no text colors. */ /* 1 = normal, 0 = no text colors. */
/* 2, 3, ... alternate escape sequences for different terminals. */ /* 2, 3, ... alternate escape sequences for different terminals. */
@ -292,8 +297,6 @@ int main (int argc, char *argv[])
if (strcmp(argv[j], "-t") == 0) { if (strcmp(argv[j], "-t") == 0) {
t_opt = atoi (argv[j+1]); t_opt = atoi (argv[j+1]);
//dw_printf ("DEBUG: text color option = %d.\n", t_opt); //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) { if (redirect_output != 0) {
g_dw_printf_dest = stderr; g_dw_printf_dest = stderr;
enable_color = 0;
} else { } else {
g_dw_printf_dest = stdout; g_dw_printf_dest = stdout;
} }
@ -195,7 +194,12 @@ void text_color_init (int enable_color, int redirect_output)
COORD coord; COORD coord;
DWORD nwritten; DWORD nwritten;
if (redirect_output != 0) {
h = GetStdHandle(STD_ERROR_HANDLE);
} else {
h = GetStdHandle(STD_OUTPUT_HANDLE); h = GetStdHandle(STD_OUTPUT_HANDLE);
}
if (h != NULL && h != INVALID_HANDLE_VALUE) { if (h != NULL && h != INVALID_HANDLE_VALUE) {
GetConsoleScreenBufferInfo (h, &csbi); GetConsoleScreenBufferInfo (h, &csbi);
@ -215,17 +219,17 @@ void text_color_init (int enable_color, int redirect_output)
int t; int t;
for (t = 0; t <= MAX_T; t++) { for (t = 0; t <= MAX_T; t++) {
text_color_init (t, redirect_output); text_color_init (t, redirect_output);
printf ("-t %d", t); fprintf (g_dw_printf_dest,"-t %d", t);
if (t) printf (" [white background] "); if (t) fprintf (g_dw_printf_dest, " [white background] ");
printf ("\n"); fprintf (g_dw_printf_dest,"\n");
printf ("%sBlack ", t_black[t]); fprintf (g_dw_printf_dest,"%sBlack ", t_black[t]);
printf ("%sRed ", t_red[t]); fprintf (g_dw_printf_dest,"%sRed ", t_red[t]);
printf ("%sGreen ", t_green[t]); fprintf (g_dw_printf_dest,"%sGreen ", t_green[t]);
printf ("%sDark-Green ", t_dark_green[t]); fprintf (g_dw_printf_dest,"%sDark-Green ", t_dark_green[t]);
printf ("%sYellow ", t_yellow[t]); fprintf (g_dw_printf_dest,"%sYellow ", t_yellow[t]);
printf ("%sBlue ", t_blue[t]); fprintf (g_dw_printf_dest,"%sBlue ", t_blue[t]);
printf ("%sMagenta ", t_magenta[t]); fprintf (g_dw_printf_dest, "%sMagenta ", t_magenta[t]);
printf ("%sCyan \n", t_cyan[t]); fprintf (g_dw_printf_dest, "%sCyan \n", t_cyan[t]);
} }
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
} }
@ -238,9 +242,9 @@ void text_color_init (int enable_color, int redirect_output)
if (t < 0) t = 0; if (t < 0) t = 0;
if (t > MAX_T) t = MAX_T; if (t > MAX_T) t = MAX_T;
printf ("%s", t_background_white[t]); fprintf (g_dw_printf_dest, "%s", t_background_white[t]);
printf ("%s", clear_eos); fprintf (g_dw_printf_dest, "%s", clear_eos);
printf ("%s", t_black[t]); fprintf (g_dw_printf_dest, "%s", t_black[t]);
} }
#endif #endif
} }
@ -291,7 +295,11 @@ void text_color_set ( enum dw_color_e c )
break; break;
} }
if (dw_printf_redirected()) {
h = GetStdHandle(STD_ERROR_HANDLE);
} else {
h = GetStdHandle(STD_OUTPUT_HANDLE); h = GetStdHandle(STD_OUTPUT_HANDLE);
}
if (h != NULL && h != INVALID_HANDLE_VALUE) { if (h != NULL && h != INVALID_HANDLE_VALUE) {
SetConsoleTextAttribute (h, attr); SetConsoleTextAttribute (h, attr);
@ -316,30 +324,30 @@ void text_color_set ( enum dw_color_e c )
default: default:
case DW_COLOR_INFO: case DW_COLOR_INFO:
printf ("%s", t_black[t]); fprintf (g_dw_printf_dest, "%s", t_black[t]);
break; break;
case DW_COLOR_ERROR: case DW_COLOR_ERROR:
printf ("%s", t_red[t]); fprintf (g_dw_printf_dest, "%s", t_red[t]);
break; break;
case DW_COLOR_REC: case DW_COLOR_REC:
// Bright green is very difficult to read against a while background. // Bright green is very difficult to read against a while background.
// Let's use dark green instead. release 1.6. // Let's use dark green instead. release 1.6.
//printf ("%s", t_green[t]); //printf ("%s", t_green[t]);
printf ("%s", t_dark_green[t]); fprintf (g_dw_printf_dest, "%s", t_dark_green[t]);
break; break;
case DW_COLOR_DECODED: case DW_COLOR_DECODED:
printf ("%s", t_blue[t]); fprintf (g_dw_printf_dest, "%s", t_blue[t]);
break; break;
case DW_COLOR_XMIT: case DW_COLOR_XMIT:
printf ("%s", t_magenta[t]); fprintf (g_dw_printf_dest, "%s", t_magenta[t]);
break; break;
case DW_COLOR_DEBUG: case DW_COLOR_DEBUG:
printf ("%s", t_dark_green[t]); fprintf (g_dw_printf_dest, "%s", t_dark_green[t]);
break; break;
} }
} }