gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
Enumerations | Functions
termcolor.h File Reference
#include <stdbool.h>
#include <stdio.h>

Go to the source code of this file.

Enumerations

enum  term_color_error_t {
  TermColorErrorNone = 0 , TermColorErrorAllocationFailed = 1 , TermColorErrorPrintingFailed = 2 , TermColorErrorInvalidColor = 3 ,
  TermColorErrorUnterminatedColor = 4 , TERM_COLOR_ERROR_COUNT
}
 

Functions

int _tcol_color_generate (char *dst, size_t dstn, size_t *len, int rep, int foreground, int background)
 
const char * tcol_errorstr (const enum term_color_error_t err)
 
int tcol_color_parse (char *dst, size_t dstn, char color[16], size_t k, size_t *len)
 
void tcol_override_color_checks (bool enable_color)
 
bool tcol_has_color (void)
 
int tcol_fprintf (FILE *stream, const char *fmt,...)
 
int tcol_printf (const char *fmt,...)
 
int tcol_snprintf (char *buffer, size_t N, const char *fmt,...)
 

Enumeration Type Documentation

◆ term_color_error_t

Enumerator
TermColorErrorNone 
TermColorErrorAllocationFailed 
TermColorErrorPrintingFailed 
TermColorErrorInvalidColor 
TermColorErrorUnterminatedColor 
TERM_COLOR_ERROR_COUNT 

Definition at line 38 of file termcolor.h.

38 {
45};
@ TermColorErrorInvalidColor
Definition termcolor.h:42
@ TermColorErrorUnterminatedColor
Definition termcolor.h:43
@ TERM_COLOR_ERROR_COUNT
Definition termcolor.h:44
@ TermColorErrorAllocationFailed
Definition termcolor.h:40
@ TermColorErrorNone
Definition termcolor.h:39
@ TermColorErrorPrintingFailed
Definition termcolor.h:41

Function Documentation

◆ _tcol_color_generate()

int _tcol_color_generate ( char * dst,
size_t dstn,
size_t * len,
int rep,
int foreground,
int background )

Definition at line 76 of file termcolor.c.

77 {
78 /* Much of the code here was informed by the following gist:
79 https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 */
80
81 size_t j = 0;
82
83 /* This macro checks to make sure we don't overrun the buffer when we add
84 a new character, */
85 #define __APPEND(c) do { \
86 dst[j++] = (c); \
87 if (j >= dstn) return TermColorErrorNone; \
88 } while (0)
89
90 /* Every escape code begins with "\e[" */
91 __APPEND('\033');
92 __APPEND('[');
93
94 /* Add the appropriate parameters to our escape sequence depending on the
95 flags set in rep(resentation). */
97 __APPEND('1');
98 __APPEND(';');
99 }
101 __APPEND('2');
102 __APPEND(';');
103 }
105 __APPEND('3');
106 __APPEND(';');
107 }
109 __APPEND('4');
110 __APPEND(';');
111 }
113 __APPEND('5');
114 __APPEND(';');
115 }
117 __APPEND('9');
118 __APPEND(';');
119 }
120
121 /* If foreground and background colors were provided, snprintf them to dst. */
122 if (foreground != -1) {
123 j += snprintf(dst + j, dstn - j, "%d;", foreground);
124 if (j >= dstn) return TermColorErrorNone;
125 }
126 if (background != -1) {
127 j += snprintf(dst + j, dstn - j, "%d;", background + 10);
128 if (j >= dstn) return TermColorErrorNone;
129 }
130
131 if (dst[j - 1] == ';') {
132 j--;
133 }
134
135 /* Graphics mode escape sequences end with 'm' */
136 __APPEND('m');
137
138 *len = j;
139 return TermColorErrorNone;
140}
@ _termcolor_internal_color_FANT
Definition termcolor.c:69
@ _termcolor_internal_color_BOLD
Definition termcolor.c:68
@ _termcolor_internal_color_UNDR
Definition termcolor.c:70
@ _termcolor_internal_color_STRK
Definition termcolor.c:72
@ _termcolor_internal_color_BLNK
Definition termcolor.c:71
@ _termcolor_internal_color_ITLC
Definition termcolor.c:73
#define __APPEND(c)

◆ tcol_color_parse()

int tcol_color_parse ( char * dst,
size_t dstn,
char color[16],
size_t k,
size_t * len )

Definition at line 142 of file termcolor.c.

143 {
144 if (!use_color) {
145 *len = 0;
146 return TermColorErrorNone;
147 }
148
149 /* '0' signifies no color, i.e. a reset */
150 if (k == 1 && *color == '0') {
151 size_t j = 0;
152 __APPEND('\033');
153 __APPEND('[');
154 __APPEND('m');
155 *len = j;
156 return TermColorErrorNone;
157 }
158
159 /* As -1 they are ignored. Otherwise they are set to the foreground color
160 code. The backround code is later added 10 to. */
161 int foreground = -1;
162 int background = -1;
163
164 enum _termcolor_internal_color rep = 0;
165 size_t i;
166 for (i = 0; i < k; i++) {
167 switch (color[i]) {
168 /* Farily straightforward; just sets the flags corresponding to the
169 color attributes. */
170 case '+':
172 break;
173 case '-':
175 break;
176 case '_':
178 break;
179 case '*':
181 break;
182 case '~':
184 break;
185 case '/':
187 break;
188
189 /* Here we take the character after the ',' and use that as our
190 background color code, like Nano does. */
191 case ',': {
192 if (i + 1 >= k) {
194 }
195 const int result = _termcolor_internal_lookup(color[i + 1]);
196 if (result == -1) {
198 }
199 background = result;
200 i++;
201 break;
202 }
203 default: {
204 /* If we haven't matched anything, we assume its a foreground
205 color. Make sure it is and error if not. */
206 const int result = _termcolor_internal_lookup(color[i]);
207 if (result != -1) {
208 foreground = result;
209 break;
210 }
212 }
213 }
214 }
215
216 return _tcol_color_generate(dst, dstn, len, rep, foreground, background);
217}
int _tcol_color_generate(char *dst, size_t dstn, size_t *len, int rep, int foreground, int background)
Definition termcolor.c:76
_termcolor_internal_color
Definition termcolor.c:67
static bool use_color
Definition termcolor.c:22
int _termcolor_internal_lookup(const char color_name)
Definition termcolor.c:44

◆ tcol_errorstr()

const char * tcol_errorstr ( const enum term_color_error_t err)
inline

Definition at line 32 of file termcolor.c.

32 {
33 return tcol_errorstrs[err];
34}
const char * tcol_errorstrs[TERM_COLOR_ERROR_COUNT]
Definition termcolor.c:24

◆ tcol_fprintf()

int tcol_fprintf ( FILE * stream,
const char * fmt,
... )

Definition at line 365 of file termcolor.c.

365 {
366 va_list ap;
367 va_start(ap, fmt);
368 const int status = tcol_vfprintf(stream, fmt, ap);
369 va_end(ap);
370 return status;
371}
char const char * fmt
Definition gwion_print.h:2
static int tcol_vfprintf(FILE *stream, const char *fmt, va_list ap)
Definition termcolor.c:324

◆ tcol_has_color()

bool tcol_has_color ( void )

Definition at line 36 of file termcolor.c.

36 {
37 return use_color;
38}

◆ tcol_override_color_checks()

void tcol_override_color_checks ( bool enable_color)

Definition at line 40 of file termcolor.c.

40 {
41 use_color = enable_color;
42}

◆ tcol_printf()

int tcol_printf ( const char * fmt,
... )

Definition at line 373 of file termcolor.c.

373 {
374 va_list ap;
375 va_start(ap, fmt);
376 const int status = tcol_vfprintf(stdout, fmt, ap);
377 va_end(ap);
378 return status;
379}

◆ tcol_snprintf()

int tcol_snprintf ( char * buffer,
size_t N,
const char * fmt,
... )

Definition at line 382 of file termcolor.c.

382 {
383 va_list ap;
384 va_start(ap, fmt);
385 const int status = tcol_vsnprintf(stream, N, fmt, ap);
386 va_end(ap);
387 return status;
388}
static int tcol_vsnprintf(char *stream, size_t N, const char *fmt, va_list ap)
Definition termcolor.c:292