1919#include <stdlib.h>
2020#include <string.h>
2121
22- #define FOSSIL_IO_BUFFER_SIZE 1024
22+ #define FOSSIL_IO_BUFFER_SIZE 1000
23+
24+ // Function to apply color
25+ void fossil_io_apply_color (const char * color ) {
26+ if (strcmp (color , "red" ) == 0 ) {
27+ printf (FOSSIL_IO_COLOR_RED );
28+ } else if (strcmp (color , "green" ) == 0 ) {
29+ printf (FOSSIL_IO_COLOR_GREEN );
30+ } else if (strcmp (color , "yellow" ) == 0 ) {
31+ printf (FOSSIL_IO_COLOR_YELLOW );
32+ } else if (strcmp (color , "blue" ) == 0 ) {
33+ printf (FOSSIL_IO_COLOR_BLUE );
34+ } else if (strcmp (color , "magenta" ) == 0 ) {
35+ printf (FOSSIL_IO_COLOR_MAGENTA );
36+ } else if (strcmp (color , "cyan" ) == 0 ) {
37+ printf (FOSSIL_IO_COLOR_CYAN );
38+ } else if (strcmp (color , "white" ) == 0 ) {
39+ printf (FOSSIL_IO_COLOR_WHITE );
40+ }
41+ // Add more colors if needed
42+ }
43+
44+ // Function to apply text attributes (e.g., bold, underline)
45+ void fossil_io_apply_attribute (const char * attribute ) {
46+ if (strcmp (attribute , "bold" ) == 0 ) {
47+ printf ("\033[1m" );
48+ } else if (strcmp (attribute , "underline" ) == 0 ) {
49+ printf ("\033[4m" );
50+ } else if (strcmp (attribute , "reset" ) == 0 ) {
51+ printf (FOSSIL_IO_COLOR_RESET );
52+ } else if (strcmp (attribute , "normal" ) == 0 ) {
53+ printf (FOSSIL_IO_ATTR_NORMAL );
54+ } else if (strcmp (attribute , "reversed" ) == 0 ) {
55+ printf (FOSSIL_IO_ATTR_REVERSED );
56+ } else if (strcmp (attribute , "blink" ) == 0 ) {
57+ printf (FOSSIL_IO_ATTR_BLINK );
58+ } else if (strcmp (attribute , "hidden" ) == 0 ) {
59+ printf (FOSSIL_IO_ATTR_HIDDEN );
60+ }
61+ // Add more attributes as needed
62+ }
63+
64+ // Function to print text with attributes, colors, and format specifiers
65+ void fossil_io_print_with_attributes (const char * format , ...) {
66+ va_list args ;
67+ va_start (args , format );
68+
69+ // Create a buffer to hold the formatted string
70+ char buffer [FOSSIL_IO_BUFFER_SIZE ];
71+ vsnprintf (buffer , sizeof (buffer ), format , args );
72+
73+ // Variable to keep track of the current position in the buffer
74+ const char * current_pos = buffer ;
75+ const char * start = NULL ;
76+ const char * end = NULL ;
77+
78+ // Iterate over the buffer and process color/attribute inside `{}` and format specifiers
79+ while ((start = strchr (current_pos , '{' )) != NULL ) {
80+ // Print text before '{'
81+ printf ("%.*s" , (int )(start - current_pos ), current_pos );
82+
83+ // Find the matching '}'
84+ end = strchr (start , '}' );
85+ if (end ) {
86+ // Extract attributes inside '{}'
87+ size_t length = end - start - 1 ;
88+ char attributes [length + 1 ];
89+ strncpy (attributes , start + 1 , length );
90+ attributes [length ] = '\0' ;
91+
92+ // Split by comma to separate color and attribute
93+ char * color = NULL ;
94+ char * attribute = NULL ;
95+ char * comma_pos = strchr (attributes , ',' );
96+ if (comma_pos ) {
97+ * comma_pos = '\0' ; // Null-terminate the color string
98+ color = attributes ; // Color part
99+ attribute = comma_pos + 1 ; // Attribute part
100+ } else {
101+ color = attributes ; // Only one part (could be color or attribute)
102+ }
103+
104+ // Apply color and/or attribute
105+ if (color ) {
106+ fossil_io_apply_color (color );
107+ }
108+ if (attribute ) {
109+ fossil_io_apply_attribute (attribute );
110+ }
111+
112+ // Move past '}' and continue processing
113+ current_pos = end + 1 ;
114+ }
115+ }
116+
117+ // Print remaining text after last '}'
118+ printf ("%s" , current_pos );
119+
120+ va_end (args );
121+ }
23122
24123// Internal utility function for color printing
25124void fossil_io_print_color (const char * color , const char * format , ...) {
@@ -31,14 +130,16 @@ void fossil_io_print_color(const char *color, const char *format, ...) {
31130 va_end (args );
32131}
33132
34- // Function to print a sanitized string with a newline
133+ // Function to print a sanitized string with attributes inside {}
35134void fossil_io_puts (const char * str ) {
36135 if (str != NULL ) {
37136 char sanitized_str [FOSSIL_IO_BUFFER_SIZE ];
38137 strncpy (sanitized_str , str , sizeof (sanitized_str ));
39138 sanitized_str [sizeof (sanitized_str ) - 1 ] = '\0' ; // Ensure null termination
40139 fossil_soap_sanitize (sanitized_str );
41- puts (sanitized_str );
140+
141+ // Print the sanitized string with attributes
142+ fossil_io_print_with_attributes (sanitized_str );
42143 } else {
43144 fputs ("NULL\n" , stderr );
44145 }
@@ -54,7 +155,7 @@ void fossil_io_putchar_color(char c, const char *color) {
54155 printf ("%s%c%s" , color , c , FOSSIL_IO_COLOR_RESET );
55156}
56157
57- // Function to print sanitized formatted output
158+ // Function to print sanitized formatted output with attributes
58159void fossil_io_printf (const char * format , ...) {
59160 va_list args ;
60161 va_start (args , format );
@@ -66,8 +167,8 @@ void fossil_io_printf(const char *format, ...) {
66167 // Sanitize the buffer
67168 fossil_soap_sanitize (buffer );
68169
69- // Print the sanitized output
70- printf ( "%s" , buffer );
170+ // Print the sanitized output with attributes
171+ fossil_io_print_with_attributes ( buffer );
71172
72173 va_end (args );
73174}
0 commit comments