-
Notifications
You must be signed in to change notification settings - Fork 0
/
printfcolor.h
262 lines (203 loc) · 6.42 KB
/
printfcolor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#ifndef PRINTFCOLOR_H
#define PRINTFCOLOR_H
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#ifdef _WIN32
#include <Windows.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef enum PfcColor {
GRAY = 0,
GREY = 0,
BLUE = 1,
GREEN = 2,
CYAN = 3,
RED = 4,
MAGENTA = 5,
YELLOW = 6,
WHITE = 7
} PfcColor;
/**
* Prints formatted output to the console with specified foreground and background colors.
*
* Colors: `RED, GREEN, BLUE, WHITE, GRAY (GREY), CYAN, MAGENTA, YELLOW`
*
* @param fg_color The color code for the text foreground color.
* @param bg_color The color code for the text background color.
* @param format A format string for the output. This is followed by additional arguments
* to be formatted according to the format string.
*
* @return The number of characters written to the console, or `-1` if an error occurs.
*
* @warning If the color codes are invalid or if the console screen buffer information cannot
* be retrieved, `errno` is set to `EINVAL` and `-1` is returned.
*/
int printfc(PfcColor fg_color, PfcColor bg_color, const char *format, ...);
/**
* Prints formatted output to the console with specified foreground color.
*
* Colors: `RED, GREEN, BLUE, WHITE, GRAY (GREY), CYAN, MAGENTA, YELLOW`
*
* @param fg_color The color code for the text foreground color.
* @param format A format string for the output. This is followed by additional arguments
* to be formatted according to the format string.
*
* @return The number of characters written to the console, or `-1` if an error occurs.
*
* @warning If the color code is invalid or if the console screen buffer information cannot
* be retrieved, `errno` is set to `EINVAL` and `-1` is returned.
*/
int printfc_fg(PfcColor fg_color, const char *format, ...);
/**
* Prints formatted output to the console with specified background color.
*
* Colors: `RED, GREEN, BLUE, WHITE, GRAY (GREY), CYAN, MAGENTA, YELLOW`
*
* @param bg_color The color code for the text background color.
* @param format A format string for the output. This is followed by additional arguments
* to be formatted according to the format string.
*
* @return The number of characters written to the console, or `-1` if an error occurs.
*
* @warning If the color code is invalid or if the console screen buffer information cannot
* be retrieved, `errno` is set to `EINVAL` and `-1` is returned.
*/
int printfc_bg(PfcColor bg_color, const char *format, ...);
// Helper functions
static bool is_color_valid(PfcColor color)
{
return color >= 0 && color <= 7;
}
static const char *get_fg_color_code(PfcColor color) {
const char *colors[] = {
"\033[30m", "\033[34m", "\033[32m", "\033[36m",
"\033[31m", "\033[35m", "\033[33m", "\033[37m"
};
if (is_color_valid(color))
return colors[color];
else
return NULL;
}
static const char *get_bg_color_code(PfcColor color) {
const char *colors[] = {
"\033[40m", "\033[44m", "\033[42m", "\033[46m",
"\033[41m", "\033[45m", "\033[43m", "\033[47m"
};
if (is_color_valid(color))
return colors[color];
else
return NULL;
}
#ifdef _WIN32 // Windows
int printfc(PfcColor fg_color, PfcColor bg_color, const char *format, ...)
{
if (!is_color_valid(fg_color) || !is_color_valid(bg_color)) {
errno = EINVAL;
return -1;
}
HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi_info;
if (!GetConsoleScreenBufferInfo(h_console, &csbi_info)) {
errno = EINVAL;
return -1;
}
va_list args;
va_start(args, format);
SetConsoleTextAttribute(h_console, fg_color | (bg_color << 4));
int chars_written = vprintf(format, args);
SetConsoleTextAttribute(h_console, csbi_info.wAttributes);
va_end(args);
return chars_written;
}
int printfc_fg(PfcColor fg_color, const char *format, ...)
{
if (!is_color_valid(fg_color)) {
errno = EINVAL;
return -1;
}
HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi_info;
if (!GetConsoleScreenBufferInfo(h_console, &csbi_info)) {
errno = EINVAL;
return -1;
}
va_list args;
va_start(args, format);
SetConsoleTextAttribute(h_console, fg_color);
int chars_written = vprintf(format, args);
SetConsoleTextAttribute(h_console, csbi_info.wAttributes);
va_end(args);
return chars_written;
}
int printfc_bg(PfcColor bg_color, const char *format, ...)
{
if (!is_color_valid(bg_color)) {
errno = EINVAL;
return -1;
}
HANDLE h_console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi_info;
if (!GetConsoleScreenBufferInfo(h_console, &csbi_info)) {
errno = EINVAL;
return -1;
}
va_list args;
va_start(args, format);
SetConsoleTextAttribute(h_console, (csbi_info.wAttributes & 0x0F) | (bg_color << 4));
int chars_written = vprintf(format, args);
SetConsoleTextAttribute(h_console, csbi_info.wAttributes);
va_end(args);
return chars_written;
}
#else // Linux and Unix-like
int printfc(PfcColor fg_color, PfcColor bg_color, const char *format, ...)
{
if (!is_color_valid(fg_color) || !is_color_valid(bg_color)) {
errno = EINVAL;
return -1;
}
va_list args;
va_start(args, format);
printf("%s%s", get_fg_color_code(fg_color), get_bg_color_code(bg_color));
int chars_written = vprintf(format, args);
printf("\033[0m");
va_end(args);
return chars_written;
}
int printfc_fg(PfcColor fg_color, const char *format, ...)
{
if (!is_color_valid(fg_color)) {
errno = EINVAL;
return -1;
}
va_list args;
va_start(args, format);
printf("%s", get_fg_color_code(fg_color));
int chars_written = vprintf(format, args);
printf("\033[0m");
va_end(args);
return chars_written;
}
int printfc_bg(PfcColor bg_color, const char *format, ...)
{
if (!is_color_valid(bg_color)) {
errno = EINVAL;
return -1;
}
va_list args;
va_start(args, format);
printf("%s", get_bg_color_code(bg_color));
int chars_written = vprintf(format, args);
printf("\033[0m");
va_end(args);
return chars_written;
}
#endif // _WIN32
#ifdef __cplusplus
}
#endif
#endif // PRINTFCOLOR_H