@@ -314,18 +314,18 @@ char *cprintf_get_string_unknown(char *f, ...)
314314 return buf ;
315315}
316316char * cprintf_base_color = "254;228;208" ;
317- bool cprintf_print_color_if_not_fifo = true;
317+ bool cprintf_print_color_only_tty = true;
318318#ifdef _GNU_SOURCE
319- #define fprintf_if_not_fifo (stream , ...) \
320- { \
321- if (!cprintf_print_color_if_not_fifo ) { \
322- fprintf(stream, __VA_ARGS__); \
323- } else { \
324- struct stat _stat_buf; \
325- if (fstat(fileno(stream), &_stat_buf) == 0 && !S_ISFIFO (_stat_buf.st_mode)) { \
326- fprintf(stream, __VA_ARGS__); \
327- } \
328- } \
319+ #define fprintf_only_tty (stream , ...) \
320+ { \
321+ if (!cprintf_print_color_only_tty ) { \
322+ fprintf(stream, __VA_ARGS__); \
323+ } else { \
324+ struct stat _stat_buf; \
325+ if (fstat(fileno(stream), &_stat_buf) == 0 && S_ISCHR (_stat_buf.st_mode)) { \
326+ fprintf(stream, __VA_ARGS__); \
327+ } \
328+ } \
329329 }
330330#else
331331#define fprintf_if_not_fifo (stream , ...) fprintf(stream, __VA_ARGS__)
@@ -340,7 +340,7 @@ static void fprint_rgb_fg_color(FILE *_Nonnull stream, const char *_Nonnull colo
340340 buf [i - 1 ] = color [i ];
341341 buf [i ] = 0 ;
342342 }
343- fprintf_if_not_fifo (stream , "\033[38;2;%sm" , buf );
343+ fprintf_only_tty (stream , "\033[38;2;%sm" , buf );
344344}
345345static void fprint_rgb_bg_color (FILE * _Nonnull stream , const char * _Nonnull color )
346346{
@@ -352,7 +352,7 @@ static void fprint_rgb_bg_color(FILE *_Nonnull stream, const char *_Nonnull colo
352352 buf [i - 1 ] = color [i ];
353353 buf [i ] = 0 ;
354354 }
355- fprintf_if_not_fifo (stream , "\033[48;2;%sm" , buf );
355+ fprintf_only_tty (stream , "\033[48;2;%sm" , buf );
356356}
357357static bool is_rgb_color (const char * _Nonnull color )
358358{
@@ -408,29 +408,29 @@ static const char *cfprintf_print_fg_color(FILE *_Nonnull stream, const char *_N
408408 color [i + 1 ] = 0 ;
409409 }
410410 if (strcmp (color , "{clear}" ) == 0 ) {
411- fprintf_if_not_fifo (stream , "\033[0m" );
411+ fprintf_only_tty (stream , "\033[0m" );
412412 } else if (strcmp (color , "{black}" ) == 0 ) {
413- fprintf_if_not_fifo (stream , "\033[30m" );
413+ fprintf_only_tty (stream , "\033[30m" );
414414 } else if (strcmp (color , "{red}" ) == 0 ) {
415- fprintf_if_not_fifo (stream , "\033[31m" );
415+ fprintf_only_tty (stream , "\033[31m" );
416416 } else if (strcmp (color , "{green}" ) == 0 ) {
417- fprintf_if_not_fifo (stream , "\033[32m" );
417+ fprintf_only_tty (stream , "\033[32m" );
418418 } else if (strcmp (color , "{yellow}" ) == 0 ) {
419- fprintf_if_not_fifo (stream , "\033[33m" );
419+ fprintf_only_tty (stream , "\033[33m" );
420420 } else if (strcmp (color , "{blue}" ) == 0 ) {
421- fprintf_if_not_fifo (stream , "\033[34m" );
421+ fprintf_only_tty (stream , "\033[34m" );
422422 } else if (strcmp (color , "{purple}" ) == 0 ) {
423- fprintf_if_not_fifo (stream , "\033[35m" );
423+ fprintf_only_tty (stream , "\033[35m" );
424424 } else if (strcmp (color , "{cyan}" ) == 0 ) {
425- fprintf_if_not_fifo (stream , "\033[36m" );
425+ fprintf_only_tty (stream , "\033[36m" );
426426 } else if (strcmp (color , "{white}" ) == 0 ) {
427- fprintf_if_not_fifo (stream , "\033[37m" );
427+ fprintf_only_tty (stream , "\033[37m" );
428428 } else if (strcmp (color , "{base}" ) == 0 ) {
429- fprintf_if_not_fifo (stream , "\033[1;38;2;%sm" , cprintf_base_color );
429+ fprintf_only_tty (stream , "\033[1;38;2;%sm" , cprintf_base_color );
430430 } else if (strcmp (color , "{underline}" ) == 0 ) {
431- fprintf_if_not_fifo (stream , "\033[4m" );
431+ fprintf_only_tty (stream , "\033[4m" );
432432 } else if (strcmp (color , "{highlight}" ) == 0 ) {
433- fprintf_if_not_fifo (stream , "\033[1m" );
433+ fprintf_only_tty (stream , "\033[1m" );
434434 } else if (is_rgb_color (color )) {
435435 fprint_rgb_fg_color (stream , color );
436436 } else {
@@ -460,29 +460,29 @@ static const char *cfprintf_print_bg_color(FILE *_Nonnull stream, const char *_N
460460 color [i + 1 ] = 0 ;
461461 }
462462 if (strcmp (color , "[clear]" ) == 0 ) {
463- fprintf_if_not_fifo (stream , "\033[0m" );
463+ fprintf_only_tty (stream , "\033[0m" );
464464 } else if (strcmp (color , "[black]" ) == 0 ) {
465- fprintf_if_not_fifo (stream , "\033[40m" );
465+ fprintf_only_tty (stream , "\033[40m" );
466466 } else if (strcmp (color , "[red]" ) == 0 ) {
467- fprintf_if_not_fifo (stream , "\033[41m" );
467+ fprintf_only_tty (stream , "\033[41m" );
468468 } else if (strcmp (color , "[green]" ) == 0 ) {
469- fprintf_if_not_fifo (stream , "\033[42m" );
469+ fprintf_only_tty (stream , "\033[42m" );
470470 } else if (strcmp (color , "[yellow]" ) == 0 ) {
471- fprintf_if_not_fifo (stream , "\033[43m" );
471+ fprintf_only_tty (stream , "\033[43m" );
472472 } else if (strcmp (color , "[blue]" ) == 0 ) {
473- fprintf_if_not_fifo (stream , "\033[44m" );
473+ fprintf_only_tty (stream , "\033[44m" );
474474 } else if (strcmp (color , "[purple]" ) == 0 ) {
475- fprintf_if_not_fifo (stream , "\033[45m" );
475+ fprintf_only_tty (stream , "\033[45m" );
476476 } else if (strcmp (color , "[cyan]" ) == 0 ) {
477- fprintf_if_not_fifo (stream , "\033[46m" );
477+ fprintf_only_tty (stream , "\033[46m" );
478478 } else if (strcmp (color , "[white]" ) == 0 ) {
479- fprintf_if_not_fifo (stream , "\033[47m" );
479+ fprintf_only_tty (stream , "\033[47m" );
480480 } else if (strcmp (color , "[base]" ) == 0 ) {
481- fprintf_if_not_fifo (stream , "\033[1;48;2;%sm" , cprintf_base_color );
481+ fprintf_only_tty (stream , "\033[1;48;2;%sm" , cprintf_base_color );
482482 } else if (strcmp (color , "[underline]" ) == 0 ) {
483- fprintf_if_not_fifo (stream , "\033[4m" );
483+ fprintf_only_tty (stream , "\033[4m" );
484484 } else if (strcmp (color , "[highlight]" ) == 0 ) {
485- fprintf_if_not_fifo (stream , "\033[1m" );
485+ fprintf_only_tty (stream , "\033[1m" );
486486 } else if (is_rgb_color (color )) {
487487 fprint_rgb_bg_color (stream , color );
488488 } else {
@@ -512,7 +512,7 @@ void cprintf__(const char *_Nonnull buf)
512512 p = & (p [1 ]);
513513 }
514514 // We will always reset the color in the end.
515- fprintf_if_not_fifo (stdout , "\033[0m" );
515+ fprintf_only_tty (stdout , "\033[0m" );
516516 fflush (stdout );
517517}
518518void cfprintf__ (FILE * _Nonnull stream , const char * _Nonnull buf )
@@ -536,6 +536,6 @@ void cfprintf__(FILE *_Nonnull stream, const char *_Nonnull buf)
536536 p = & (p [1 ]);
537537 }
538538 // We will always reset the color in the end.
539- fprintf_if_not_fifo (stream , "\033[0m" );
539+ fprintf_only_tty (stream , "\033[0m" );
540540 fflush (stream );
541541}
0 commit comments