Skip to content

Commit 98e2ec1

Browse files
Merge pull request #6 from dreamer-coding/fstring_print
Update output
2 parents 90d0c8c + f360b6b commit 98e2ec1

File tree

2 files changed

+195
-14
lines changed

2 files changed

+195
-14
lines changed

code/logic/fossil/io/output.h

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,29 @@
1414
#ifndef FOSSIL_IO_OUTPUT_H
1515
#define FOSSIL_IO_OUTPUT_H
1616

17+
#include <stdarg.h>
18+
1719
#ifdef __cplusplus
1820
extern "C" {
1921
#endif
2022

2123
// Define color codes for output
22-
#define FOSSIL_IO_COLOR_RESET "\033[0m"
23-
#define FOSSIL_IO_COLOR_RED "\033[31m"
24-
#define FOSSIL_IO_COLOR_GREEN "\033[32m"
25-
#define FOSSIL_IO_COLOR_YELLOW "\033[33m"
26-
#define FOSSIL_IO_COLOR_BLUE "\033[34m"
27-
#define FOSSIL_IO_COLOR_MAGENTA "\033[35m"
28-
#define FOSSIL_IO_COLOR_CYAN "\033[36m"
29-
#define FOSSIL_IO_COLOR_WHITE "\033[37m"
24+
#define FOSSIL_IO_COLOR_RESET "\033[0m"
25+
#define FOSSIL_IO_COLOR_RED "\033[31m"
26+
#define FOSSIL_IO_COLOR_GREEN "\033[32m"
27+
#define FOSSIL_IO_COLOR_YELLOW "\033[33m"
28+
#define FOSSIL_IO_COLOR_BLUE "\033[34m"
29+
#define FOSSIL_IO_COLOR_MAGENTA "\033[35m"
30+
#define FOSSIL_IO_COLOR_CYAN "\033[36m"
31+
#define FOSSIL_IO_COLOR_WHITE "\033[37m"
32+
33+
// Define text attributes
34+
#define FOSSIL_IO_ATTR_BOLD "\033[1m"
35+
#define FOSSIL_IO_ATTR_UNDERLINE "\033[4m"
36+
#define FOSSIL_IO_ATTR_REVERSED "\033[7m"
37+
#define FOSSIL_IO_ATTR_BLINK "\033[5m"
38+
#define FOSSIL_IO_ATTR_HIDDEN "\033[8m"
39+
#define FOSSIL_IO_ATTR_NORMAL "\033[22m" // For reverting to normal text
3040

3141
/**
3242
* Prints a string to the output.
@@ -68,6 +78,76 @@ void fossil_io_putchar_color(char c, const char *color);
6878

6979
#ifdef __cplusplus
7080
}
81+
/**
82+
* C++ wrapper for the output functions.
83+
*/
84+
namespace fossil {
85+
/**
86+
* Namespace for input/output operations.
87+
*/
88+
namespace io {
89+
/**
90+
* Class for output operations.
91+
*/
92+
class Output {
93+
public:
94+
/**
95+
* Prints a string to the output.
96+
*
97+
* @param str The string to be printed.
98+
*/
99+
static void puts(const char *str) {
100+
fossil_io_puts(str);
101+
}
102+
103+
/**
104+
* Prints a formatted string to the output.
105+
*
106+
* @param format The format string.
107+
* @param ... The additional arguments to be formatted.
108+
*/
109+
static void printf(const char *format, ...) {
110+
va_list args;
111+
va_start(args, format);
112+
fossil_io_printf(format, args);
113+
va_end(args);
114+
}
115+
116+
/**
117+
* Prints a string to the output with a specified color.
118+
*
119+
* @param color The color code to be applied.
120+
* @param str The string to be printed.
121+
*/
122+
static void print_color(const char *color, const char *format, ...) {
123+
va_list args;
124+
va_start(args, format);
125+
fossil_io_print_color(color, format, args);
126+
va_end(args);
127+
}
128+
129+
/**
130+
* Prints a character to the output.
131+
*
132+
* @param c The character to be printed.
133+
*/
134+
static void putchar(char c) {
135+
fossil_io_putchar(c);
136+
}
137+
138+
/**
139+
* Prints a character to the output with a specified color.
140+
*
141+
* @param c The character to be printed.
142+
* @param color The color code to be applied.
143+
*/
144+
static void putchar_color(char c, const char *color) {
145+
fossil_io_putchar_color(c, color);
146+
}
147+
};
148+
}
149+
}
150+
71151
#endif
72152

73153
#endif /* FOSSIL_IO_FRAMEWORK_H */

code/logic/output.c

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,106 @@
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
25124
void 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 {}
35134
void 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
58159
void 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

Comments
 (0)