Skip to content

Commit 3dcc520

Browse files
committed
Allow color to be used when stdout is redirected with -O
1 parent 31e6d00 commit 3dcc520

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

Diff for: src/direwolf.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,16 @@ int main (int argc, char *argv[])
276276
* Default will be no colors if stdout is not a terminal (i.e. piped into
277277
* something else such as "tee") but command line can override this.
278278
*/
279+
for (j=1; j<argc; j++) {
280+
if (strcmp(argv[j], "-O") == 0) {
281+
O_opt = 1;
282+
}
283+
}
279284

280285
#if __WIN32__
281-
t_opt = _isatty(_fileno(stdout)) > 0;
286+
t_opt = _isatty(_fileno(O_opt ? stderr : stdout)) > 0;
282287
#else
283-
t_opt = isatty(fileno(stdout));
288+
t_opt = isatty(fileno(O_opt ? stderr : stdout));
284289
#endif
285290
/* 1 = normal, 0 = no text colors. */
286291
/* 2, 3, ... alternate escape sequences for different terminals. */
@@ -289,8 +294,6 @@ int main (int argc, char *argv[])
289294
if (strcmp(argv[j], "-t") == 0) {
290295
t_opt = atoi (argv[j+1]);
291296
//dw_printf ("DEBUG: text color option = %d.\n", t_opt);
292-
} else if (strcmp(argv[j], "-O") == 0) {
293-
O_opt = 1;
294297
}
295298
}
296299

Diff for: src/textcolor.c

+31-23
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ void text_color_init (int enable_color, int redirect_output)
177177
{
178178
if (redirect_output != 0) {
179179
g_dw_printf_dest = stderr;
180-
enable_color = 0;
181180
} else {
182181
g_dw_printf_dest = stdout;
183182
}
@@ -195,7 +194,12 @@ void text_color_init (int enable_color, int redirect_output)
195194
COORD coord;
196195
DWORD nwritten;
197196

198-
h = GetStdHandle(STD_OUTPUT_HANDLE);
197+
if (redirect_output != 0) {
198+
h = GetStdHandle(STD_ERROR_HANDLE);
199+
} else {
200+
h = GetStdHandle(STD_OUTPUT_HANDLE);
201+
}
202+
199203
if (h != NULL && h != INVALID_HANDLE_VALUE) {
200204

201205
GetConsoleScreenBufferInfo (h, &csbi);
@@ -215,17 +219,17 @@ void text_color_init (int enable_color, int redirect_output)
215219
int t;
216220
for (t = 0; t <= MAX_T; t++) {
217221
text_color_init (t, redirect_output);
218-
printf ("-t %d", t);
219-
if (t) printf (" [white background] ");
220-
printf ("\n");
221-
printf ("%sBlack ", t_black[t]);
222-
printf ("%sRed ", t_red[t]);
223-
printf ("%sGreen ", t_green[t]);
224-
printf ("%sDark-Green ", t_dark_green[t]);
225-
printf ("%sYellow ", t_yellow[t]);
226-
printf ("%sBlue ", t_blue[t]);
227-
printf ("%sMagenta ", t_magenta[t]);
228-
printf ("%sCyan \n", t_cyan[t]);
222+
fprintf (g_dw_printf_dest,"-t %d", t);
223+
if (t) fprintf (g_dw_printf_dest, " [white background] ");
224+
fprintf (g_dw_printf_dest,"\n");
225+
fprintf (g_dw_printf_dest,"%sBlack ", t_black[t]);
226+
fprintf (g_dw_printf_dest,"%sRed ", t_red[t]);
227+
fprintf (g_dw_printf_dest,"%sGreen ", t_green[t]);
228+
fprintf (g_dw_printf_dest,"%sDark-Green ", t_dark_green[t]);
229+
fprintf (g_dw_printf_dest,"%sYellow ", t_yellow[t]);
230+
fprintf (g_dw_printf_dest,"%sBlue ", t_blue[t]);
231+
fprintf (g_dw_printf_dest, "%sMagenta ", t_magenta[t]);
232+
fprintf (g_dw_printf_dest, "%sCyan \n", t_cyan[t]);
229233
}
230234
exit (EXIT_SUCCESS);
231235
}
@@ -238,9 +242,9 @@ void text_color_init (int enable_color, int redirect_output)
238242
if (t < 0) t = 0;
239243
if (t > MAX_T) t = MAX_T;
240244

241-
printf ("%s", t_background_white[t]);
242-
printf ("%s", clear_eos);
243-
printf ("%s", t_black[t]);
245+
fprintf (g_dw_printf_dest, "%s", t_background_white[t]);
246+
fprintf (g_dw_printf_dest, "%s", clear_eos);
247+
fprintf (g_dw_printf_dest, "%s", t_black[t]);
244248
}
245249
#endif
246250
}
@@ -291,7 +295,11 @@ void text_color_set ( enum dw_color_e c )
291295
break;
292296
}
293297

294-
h = GetStdHandle(STD_OUTPUT_HANDLE);
298+
if (dw_printf_redirected()) {
299+
h = GetStdHandle(STD_ERROR_HANDLE);
300+
} else {
301+
h = GetStdHandle(STD_OUTPUT_HANDLE);
302+
}
295303

296304
if (h != NULL && h != INVALID_HANDLE_VALUE) {
297305
SetConsoleTextAttribute (h, attr);
@@ -316,30 +324,30 @@ void text_color_set ( enum dw_color_e c )
316324

317325
default:
318326
case DW_COLOR_INFO:
319-
printf ("%s", t_black[t]);
327+
fprintf (g_dw_printf_dest, "%s", t_black[t]);
320328
break;
321329

322330
case DW_COLOR_ERROR:
323-
printf ("%s", t_red[t]);
331+
fprintf (g_dw_printf_dest, "%s", t_red[t]);
324332
break;
325333

326334
case DW_COLOR_REC:
327335
// Bright green is very difficult to read against a while background.
328336
// Let's use dark green instead. release 1.6.
329337
//printf ("%s", t_green[t]);
330-
printf ("%s", t_dark_green[t]);
338+
fprintf (g_dw_printf_dest, "%s", t_dark_green[t]);
331339
break;
332340

333341
case DW_COLOR_DECODED:
334-
printf ("%s", t_blue[t]);
342+
fprintf (g_dw_printf_dest, "%s", t_blue[t]);
335343
break;
336344

337345
case DW_COLOR_XMIT:
338-
printf ("%s", t_magenta[t]);
346+
fprintf (g_dw_printf_dest, "%s", t_magenta[t]);
339347
break;
340348

341349
case DW_COLOR_DEBUG:
342-
printf ("%s", t_dark_green[t]);
350+
fprintf (g_dw_printf_dest, "%s", t_dark_green[t]);
343351
break;
344352
}
345353
}

0 commit comments

Comments
 (0)