c-template
colors.h File Reference

macros and utilities for printing color to stdout from https://www.quora.com/How-do-I-print-a-colored-output-in-C Pass a COLORS_x enum value into print_colored and the color will be printed on stdout Note that if you want to disable printing of that color you'll have to send the COLORS_RESET enum value through to make non-bold change 0->1 (0;31m red) vs (1;31m bold red) More...

#include <stdbool.h>
Include dependency graph for colors.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ANSI_COLOR_RED   "\x1b[1;31m"
 
#define ANSI_COLOR_SOFT_RED   "\x1b[1;38;5;210m"
 
#define ANSI_COLOR_GREEN   "\x1b[1;32m"
 
#define ANSI_COLOR_YELLOW   "\x1b[1;33m"
 
#define ANSI_COLOR_BLUE   "\x1b[1;34m"
 
#define ANSI_COLOR_MAGENTA   "\x1b[1;35m"
 
#define ANSI_COLOR_CYAN   "\x1b[1;36m"
 
#define ANSI_COLOR_RESET   "\x1b[1;0m"
 

Enumerations

enum  COLORS {
  COLORS_RED, COLORS_SOFT_RED, COLORS_GREEN, COLORS_YELLOW,
  COLORS_BLUE, COLORS_MAGENTA, COLORS_CYAN, COLORS_RESET
}
 allows short-handed references to ANSI color schemes, and enables easier color selection anytime you want to extend the available colors with an additional enum, add a switch case in get_ansi_color_scheme More...
 

Functions

char * format_colored (COLORS color, char *message)
 returns a char * with the message formatted with ansi colors More...
 
char * get_ansi_color_scheme (COLORS color)
 returns an ansi color string to be used with printf More...
 
void print_colored (COLORS color, char *message)
 prints message to stdout with the given color More...
 
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 immediately set do_flush to true returns 0 if no error, returns 1 if error More...
 

Detailed Description

macros and utilities for printing color to stdout from https://www.quora.com/How-do-I-print-a-colored-output-in-C Pass a COLORS_x enum value into print_colored and the color will be printed on stdout Note that if you want to disable printing of that color you'll have to send the COLORS_RESET enum value through to make non-bold change 0->1 (0;31m red) vs (1;31m bold red)

Definition in file colors.h.

Macro Definition Documentation

◆ ANSI_COLOR_BLUE

#define ANSI_COLOR_BLUE   "\x1b[1;34m"

Definition at line 17 of file colors.h.

◆ ANSI_COLOR_CYAN

#define ANSI_COLOR_CYAN   "\x1b[1;36m"

Definition at line 19 of file colors.h.

◆ ANSI_COLOR_GREEN

#define ANSI_COLOR_GREEN   "\x1b[1;32m"

Definition at line 15 of file colors.h.

◆ ANSI_COLOR_MAGENTA

#define ANSI_COLOR_MAGENTA   "\x1b[1;35m"

Definition at line 18 of file colors.h.

◆ ANSI_COLOR_RED

#define ANSI_COLOR_RED   "\x1b[1;31m"

Definition at line 13 of file colors.h.

◆ ANSI_COLOR_RESET

#define ANSI_COLOR_RESET   "\x1b[1;0m"

Definition at line 20 of file colors.h.

◆ ANSI_COLOR_SOFT_RED

#define ANSI_COLOR_SOFT_RED   "\x1b[1;38;5;210m"

Definition at line 14 of file colors.h.

◆ ANSI_COLOR_YELLOW

#define ANSI_COLOR_YELLOW   "\x1b[1;33m"

Definition at line 16 of file colors.h.

Enumeration Type Documentation

◆ COLORS

enum COLORS

allows short-handed references to ANSI color schemes, and enables easier color selection anytime you want to extend the available colors with an additional enum, add a switch case in get_ansi_color_scheme

Enumerator
COLORS_RED 
COLORS_SOFT_RED 
COLORS_GREEN 
COLORS_YELLOW 
COLORS_BLUE 
COLORS_MAGENTA 
COLORS_CYAN 
COLORS_RESET 

Definition at line 25 of file colors.h.

Function Documentation

◆ format_colored()

char* format_colored ( COLORS  color,
char *  message 
)

returns a char * with the message formatted with ansi colors

Definition at line 31 of file colors.c.

Here is the call graph for this function:

◆ get_ansi_color_scheme()

char* get_ansi_color_scheme ( COLORS  color)

returns an ansi color string to be used with printf

Definition at line 8 of file colors.c.

Here is the caller graph for this function:

◆ print_colored()

void print_colored ( COLORS  color,
char *  message 
)

prints message to stdout with the given color

Definition at line 43 of file colors.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 immediately set do_flush to true returns 0 if no error, returns 1 if error

Definition at line 47 of file colors.c.

Here is the call graph for this function: