3434#define false ((_Bool) + 0u)
3535#endif
3636char * cprintf_base_color = "254;228;208" ;
37- static void print_rgb_fg_color (const char * _Nonnull color )
38- {
39- /*
40- * print \033[1;38;2;R;G;Bm format color.
41- */
42- char buf [17 ];
43- for (size_t i = 1 ; i < strlen (color ) - 1 ; i ++ ) {
44- buf [i - 1 ] = color [i ];
45- buf [i ] = 0 ;
46- }
47- printf ("\033[38;2;%sm" , buf );
48- }
4937static void fprint_rgb_fg_color (FILE * _Nonnull stream , const char * _Nonnull color )
5038{
5139 /*
@@ -58,18 +46,6 @@ static void fprint_rgb_fg_color(FILE *_Nonnull stream, const char *_Nonnull colo
5846 }
5947 fprintf (stream , "\033[38;2;%sm" , buf );
6048}
61- static void print_rgb_bg_color (const char * _Nonnull color )
62- {
63- /*
64- * print \033[1;38;2;R;G;Bm format color.
65- */
66- char buf [17 ];
67- for (size_t i = 1 ; i < strlen (color ) - 1 ; i ++ ) {
68- buf [i - 1 ] = color [i ];
69- buf [i ] = 0 ;
70- }
71- printf ("\033[48;2;%sm" , buf );
72- }
7349static void fprint_rgb_bg_color (FILE * _Nonnull stream , const char * _Nonnull color )
7450{
7551 /*
@@ -167,58 +143,6 @@ static const char *cfprintf_print_fg_color(FILE *_Nonnull stream, const char *_N
167143 }
168144 return ret ;
169145}
170- static const char * cprintf_print_fg_color (const char * _Nonnull buf )
171- {
172- /*
173- * Only valid {color} will be recognized,
174- * and for other '{' without 'color}', we print a '{'.
175- * we return the pointer to the last character that is
176- * not recognized as color.
177- */
178- const char * ret = buf ;
179- char color [17 ] = { '\0' };
180- for (int i = 0 ; i < 16 ; i ++ ) {
181- if (buf [i ] == '}' ) {
182- color [i ] = buf [i ];
183- color [i + 1 ] = 0 ;
184- ret = & (buf [i ]);
185- break ;
186- }
187- color [i ] = buf [i ];
188- color [i + 1 ] = 0 ;
189- }
190- if (strcmp (color , "{clear}" ) == 0 ) {
191- printf ("\033[0m" );
192- } else if (strcmp (color , "{black}" ) == 0 ) {
193- printf ("\033[30m" );
194- } else if (strcmp (color , "{red}" ) == 0 ) {
195- printf ("\033[31m" );
196- } else if (strcmp (color , "{green}" ) == 0 ) {
197- printf ("\033[32m" );
198- } else if (strcmp (color , "{yellow}" ) == 0 ) {
199- printf ("\033[33m" );
200- } else if (strcmp (color , "{blue}" ) == 0 ) {
201- printf ("\033[34m" );
202- } else if (strcmp (color , "{purple}" ) == 0 ) {
203- printf ("\033[35m" );
204- } else if (strcmp (color , "{cyan}" ) == 0 ) {
205- printf ("\033[36m" );
206- } else if (strcmp (color , "{white}" ) == 0 ) {
207- printf ("\033[37m" );
208- } else if (strcmp (color , "{base}" ) == 0 ) {
209- printf ("\033[1;38;2;%sm" , cprintf_base_color );
210- } else if (strcmp (color , "{underline}" ) == 0 ) {
211- printf ("\033[4m" );
212- } else if (strcmp (color , "{highlight}" ) == 0 ) {
213- printf ("\033[1m" );
214- } else if (is_rgb_color (color )) {
215- print_rgb_fg_color (color );
216- } else {
217- ret = buf ;
218- printf ("{" );
219- }
220- return ret ;
221- }
222146static const char * cfprintf_print_bg_color (FILE * _Nonnull stream , const char * _Nonnull buf )
223147{
224148 /*
@@ -271,70 +195,18 @@ static const char *cfprintf_print_bg_color(FILE *_Nonnull stream, const char *_N
271195 }
272196 return ret ;
273197}
274- static const char * cprintf_print_bg_color (const char * _Nonnull buf )
275- {
276- /*
277- * Only valid [color] will be recognized,
278- * and for other '[' without 'color]', we print a '['.
279- * we return the pointer to the last character that is
280- * not recognized as color.
281- */
282- const char * ret = buf ;
283- char color [17 ] = { '\0' };
284- for (int i = 0 ; i < 16 ; i ++ ) {
285- if (buf [i ] == ']' ) {
286- color [i ] = buf [i ];
287- color [i + 1 ] = 0 ;
288- ret = & (buf [i ]);
289- break ;
290- }
291- color [i ] = buf [i ];
292- color [i + 1 ] = 0 ;
293- }
294- if (strcmp (color , "[clear]" ) == 0 ) {
295- printf ("\033[0m" );
296- } else if (strcmp (color , "[black]" ) == 0 ) {
297- printf ("\033[40m" );
298- } else if (strcmp (color , "[red]" ) == 0 ) {
299- printf ("\033[41m" );
300- } else if (strcmp (color , "[green]" ) == 0 ) {
301- printf ("\033[42m" );
302- } else if (strcmp (color , "[yellow]" ) == 0 ) {
303- printf ("\033[43m" );
304- } else if (strcmp (color , "[blue]" ) == 0 ) {
305- printf ("\033[44m" );
306- } else if (strcmp (color , "[purple]" ) == 0 ) {
307- printf ("\033[45m" );
308- } else if (strcmp (color , "[cyan]" ) == 0 ) {
309- printf ("\033[46m" );
310- } else if (strcmp (color , "[white]" ) == 0 ) {
311- printf ("\033[47m" );
312- } else if (strcmp (color , "[base]" ) == 0 ) {
313- printf ("\033[1;48;2;%sm" , cprintf_base_color );
314- } else if (strcmp (color , "[underline]" ) == 0 ) {
315- printf ("\033[4m" );
316- } else if (strcmp (color , "[highlight]" ) == 0 ) {
317- printf ("\033[1m" );
318- } else if (is_rgb_color (color )) {
319- print_rgb_bg_color (color );
320- } else {
321- ret = buf ;
322- printf ("[" );
323- }
324- return ret ;
325- }
326- void __cprintf (const char * _Nonnull buf )
198+ void cprintf__ (const char * _Nonnull buf )
327199{
328200 const char * p = NULL ;
329201 p = buf ;
330202 for (size_t i = 0 ; i < strlen (buf ); i ++ ) {
331203 // Search for '{'.
332204 if (* p == '{' ) {
333205 // *p will be moved because we need to skip the {color} string.
334- p = cprintf_print_fg_color ( p );
206+ p = cfprintf_print_fg_color ( stdout , p );
335207 } else if (* p == '[' ) {
336208 // *p will be moved because we need to skip the [color] string.
337- p = cprintf_print_bg_color ( p );
209+ p = cfprintf_print_bg_color ( stdout , p );
338210 } else {
339211 printf ("%c" , * p );
340212 }
@@ -347,7 +219,7 @@ void __cprintf(const char *_Nonnull buf)
347219 printf ("\033[0m" );
348220 fflush (stdout );
349221}
350- void __cfprintf (FILE * _Nonnull stream , const char * _Nonnull buf )
222+ void cfprintf__ (FILE * _Nonnull stream , const char * _Nonnull buf )
351223{
352224 const char * p = NULL ;
353225 p = buf ;
0 commit comments