c-template
colors.h
Go to the documentation of this file.
1 /*! @file colors.h
2  * @brief macros and utilities for printing color to stdout
3  * from https://www.quora.com/How-do-I-print-a-colored-output-in-C
4  * Pass a `COLORS_x` enum value into `print_colored` and the color will be printed on stdout
5  * Note that if you want to disable printing of that color you'll have to send the `COLORS_RESET` enum value through
6  * to make non-bold change 0->1 (0;31m red) vs (1;31m bold red)
7 */
8 
9 #pragma once
10 
11 #include <stdbool.h>
12 
13 #define ANSI_COLOR_RED "\x1b[1;31m"
14 #define ANSI_COLOR_SOFT_RED "\x1b[1;38;5;210m"
15 #define ANSI_COLOR_GREEN "\x1b[1;32m"
16 #define ANSI_COLOR_YELLOW "\x1b[1;33m"
17 #define ANSI_COLOR_BLUE "\x1b[1;34m"
18 #define ANSI_COLOR_MAGENTA "\x1b[1;35m"
19 #define ANSI_COLOR_CYAN "\x1b[1;36m"
20 #define ANSI_COLOR_RESET "\x1b[1;0m"
21 
22 /*! @brief allows short-handed references to ANSI color schemes, and enables easier color selection
23  * anytime you want to extend the available colors with an additional enum, add a switch case in get_ansi_color_scheme
24 */
25 typedef enum {
27 } COLORS;
28 
29 /*! @brief returns a `char *` with the message formatted with ansi colors
30 */
31 char *format_colored(COLORS color, char *message);
32 
33 /*! @brief returns an ansi color string to be used with printf
34 */
35 char *get_ansi_color_scheme(COLORS color);
36 
37 /*! @brief prints message to stdout with the given color
38 */
39 void print_colored(COLORS color, char *message);
40 
41 /*! @brief writes a message to fh with the given color
42  * For "sync writes" and to always flush logs to disk immediately set do_flush to true
43  * returns 0 if no error, returns 1 if error
44 */
45 int write_colored(COLORS color, int file_descriptor, char *message);
COLORS_CYAN
@ COLORS_CYAN
Definition: colors.h:26
COLORS_RED
@ COLORS_RED
Definition: colors.h:26
COLORS
COLORS
allows short-handed references to ANSI color schemes, and enables easier color selection anytime you ...
Definition: colors.h:25
format_colored
char * format_colored(COLORS color, char *message)
returns a char * with the message formatted with ansi colors
Definition: colors.c:31
write_colored
int write_colored(COLORS color, int file_descriptor, char *message)
writes a message to fh with the given color For "sync writes" and to always flush logs to disk immedi...
Definition: colors.c:47
COLORS_MAGENTA
@ COLORS_MAGENTA
Definition: colors.h:26
COLORS_GREEN
@ COLORS_GREEN
Definition: colors.h:26
COLORS_SOFT_RED
@ COLORS_SOFT_RED
Definition: colors.h:26
get_ansi_color_scheme
char * get_ansi_color_scheme(COLORS color)
returns an ansi color string to be used with printf
Definition: colors.c:8
COLORS_RESET
@ COLORS_RESET
Definition: colors.h:26
COLORS_BLUE
@ COLORS_BLUE
Definition: colors.h:26
COLORS_YELLOW
@ COLORS_YELLOW
Definition: colors.h:26
print_colored
void print_colored(COLORS color, char *message)
prints message to stdout with the given color
Definition: colors.c:43