2828 *
2929 */
3030#include "include/cprintf.h"
31- #if __STDC_VERSION__ < 202000L
32- #define bool _Bool
33- #define true ((_Bool) + 1u)
34- #define false ((_Bool) + 0u)
35- #endif
3631char * cprintf_base_color = "254;228;208" ;
32+ bool cprintf_print_color_if_not_fifo = true;
33+ #define fprintf_if_not_fifo (stream , ...) \
34+ { \
35+ if (!cprintf_print_color_if_not_fifo) { \
36+ fprintf(stream, __VA_ARGS__); \
37+ } else { \
38+ struct stat _stat_buf; \
39+ if (fstat(fileno(stream), &_stat_buf) == 0 && !S_ISFIFO(_stat_buf.st_mode)) { \
40+ fprintf(stream, __VA_ARGS__); \
41+ } \
42+ } \
43+ }
3744static void fprint_rgb_fg_color (FILE * _Nonnull stream , const char * _Nonnull color )
3845{
3946 /*
@@ -44,7 +51,7 @@ static void fprint_rgb_fg_color(FILE *_Nonnull stream, const char *_Nonnull colo
4451 buf [i - 1 ] = color [i ];
4552 buf [i ] = 0 ;
4653 }
47- fprintf (stream , "\033[38;2;%sm" , buf );
54+ fprintf_if_not_fifo (stream , "\033[38;2;%sm" , buf );
4855}
4956static void fprint_rgb_bg_color (FILE * _Nonnull stream , const char * _Nonnull color )
5057{
@@ -56,7 +63,7 @@ static void fprint_rgb_bg_color(FILE *_Nonnull stream, const char *_Nonnull colo
5663 buf [i - 1 ] = color [i ];
5764 buf [i ] = 0 ;
5865 }
59- fprintf (stream , "\033[48;2;%sm" , buf );
66+ fprintf_if_not_fifo (stream , "\033[48;2;%sm" , buf );
6067}
6168static bool is_rgb_color (const char * _Nonnull color )
6269{
@@ -112,29 +119,29 @@ static const char *cfprintf_print_fg_color(FILE *_Nonnull stream, const char *_N
112119 color [i + 1 ] = 0 ;
113120 }
114121 if (strcmp (color , "{clear}" ) == 0 ) {
115- fprintf (stream , "\033[0m" );
122+ fprintf_if_not_fifo (stream , "\033[0m" );
116123 } else if (strcmp (color , "{black}" ) == 0 ) {
117- fprintf (stream , "\033[30m" );
124+ fprintf_if_not_fifo (stream , "\033[30m" );
118125 } else if (strcmp (color , "{red}" ) == 0 ) {
119- fprintf (stream , "\033[31m" );
126+ fprintf_if_not_fifo (stream , "\033[31m" );
120127 } else if (strcmp (color , "{green}" ) == 0 ) {
121- fprintf (stream , "\033[32m" );
128+ fprintf_if_not_fifo (stream , "\033[32m" );
122129 } else if (strcmp (color , "{yellow}" ) == 0 ) {
123- fprintf (stream , "\033[33m" );
130+ fprintf_if_not_fifo (stream , "\033[33m" );
124131 } else if (strcmp (color , "{blue}" ) == 0 ) {
125- fprintf (stream , "\033[34m" );
132+ fprintf_if_not_fifo (stream , "\033[34m" );
126133 } else if (strcmp (color , "{purple}" ) == 0 ) {
127- fprintf (stream , "\033[35m" );
134+ fprintf_if_not_fifo (stream , "\033[35m" );
128135 } else if (strcmp (color , "{cyan}" ) == 0 ) {
129- fprintf (stream , "\033[36m" );
136+ fprintf_if_not_fifo (stream , "\033[36m" );
130137 } else if (strcmp (color , "{white}" ) == 0 ) {
131- fprintf (stream , "\033[37m" );
138+ fprintf_if_not_fifo (stream , "\033[37m" );
132139 } else if (strcmp (color , "{base}" ) == 0 ) {
133- fprintf (stream , "\033[1;38;2;%sm" , cprintf_base_color );
140+ fprintf_if_not_fifo (stream , "\033[1;38;2;%sm" , cprintf_base_color );
134141 } else if (strcmp (color , "{underline}" ) == 0 ) {
135- fprintf (stream , "\033[4m" );
142+ fprintf_if_not_fifo (stream , "\033[4m" );
136143 } else if (strcmp (color , "{highlight}" ) == 0 ) {
137- fprintf (stream , "\033[1m" );
144+ fprintf_if_not_fifo (stream , "\033[1m" );
138145 } else if (is_rgb_color (color )) {
139146 fprint_rgb_fg_color (stream , color );
140147 } else {
@@ -164,29 +171,29 @@ static const char *cfprintf_print_bg_color(FILE *_Nonnull stream, const char *_N
164171 color [i + 1 ] = 0 ;
165172 }
166173 if (strcmp (color , "[clear]" ) == 0 ) {
167- fprintf (stream , "\033[0m" );
174+ fprintf_if_not_fifo (stream , "\033[0m" );
168175 } else if (strcmp (color , "[black]" ) == 0 ) {
169- fprintf (stream , "\033[40m" );
176+ fprintf_if_not_fifo (stream , "\033[40m" );
170177 } else if (strcmp (color , "[red]" ) == 0 ) {
171- fprintf (stream , "\033[41m" );
178+ fprintf_if_not_fifo (stream , "\033[41m" );
172179 } else if (strcmp (color , "[green]" ) == 0 ) {
173- fprintf (stream , "\033[42m" );
180+ fprintf_if_not_fifo (stream , "\033[42m" );
174181 } else if (strcmp (color , "[yellow]" ) == 0 ) {
175- fprintf (stream , "\033[43m" );
182+ fprintf_if_not_fifo (stream , "\033[43m" );
176183 } else if (strcmp (color , "[blue]" ) == 0 ) {
177- fprintf (stream , "\033[44m" );
184+ fprintf_if_not_fifo (stream , "\033[44m" );
178185 } else if (strcmp (color , "[purple]" ) == 0 ) {
179- fprintf (stream , "\033[45m" );
186+ fprintf_if_not_fifo (stream , "\033[45m" );
180187 } else if (strcmp (color , "[cyan]" ) == 0 ) {
181- fprintf (stream , "\033[46m" );
188+ fprintf_if_not_fifo (stream , "\033[46m" );
182189 } else if (strcmp (color , "[white]" ) == 0 ) {
183- fprintf (stream , "\033[47m" );
190+ fprintf_if_not_fifo (stream , "\033[47m" );
184191 } else if (strcmp (color , "[base]" ) == 0 ) {
185- fprintf (stream , "\033[1;48;2;%sm" , cprintf_base_color );
192+ fprintf_if_not_fifo (stream , "\033[1;48;2;%sm" , cprintf_base_color );
186193 } else if (strcmp (color , "[underline]" ) == 0 ) {
187- fprintf (stream , "\033[4m" );
194+ fprintf_if_not_fifo (stream , "\033[4m" );
188195 } else if (strcmp (color , "[highlight]" ) == 0 ) {
189- fprintf (stream , "\033[1m" );
196+ fprintf_if_not_fifo (stream , "\033[1m" );
190197 } else if (is_rgb_color (color )) {
191198 fprint_rgb_bg_color (stream , color );
192199 } else {
@@ -240,6 +247,6 @@ void cfprintf__(FILE *_Nonnull stream, const char *_Nonnull buf)
240247 p = & (p [1 ]);
241248 }
242249 // We will always reset the color in the end.
243- fprintf (stream , "\033[0m" );
250+ fprintf_if_not_fifo (stream , "\033[0m" );
244251 fflush (stream );
245252}
0 commit comments