c-template
colors.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdbool.h>
5 #include <unistd.h>
6 #include "../../include/utils/colors.h"
7 
9  switch (color) {
10  case COLORS_RED:
11  return ANSI_COLOR_RED;
12  case COLORS_SOFT_RED:
13  return ANSI_COLOR_SOFT_RED;
14  case COLORS_GREEN:
15  return ANSI_COLOR_GREEN;
16  case COLORS_YELLOW:
17  return ANSI_COLOR_YELLOW;
18  case COLORS_BLUE:
19  return ANSI_COLOR_BLUE;
20  case COLORS_MAGENTA:
21  return ANSI_COLOR_MAGENTA;
22  case COLORS_CYAN:
23  return ANSI_COLOR_CYAN;
24  case COLORS_RESET:
25  return ANSI_COLOR_RESET;
26  default:
27  return NULL;
28  }
29 }
30 
31 char *format_colored(COLORS color, char *message) {
32  char *pcolor = get_ansi_color_scheme(color);
33  char *formatted = malloc(sizeof(message) + sizeof(pcolor));
34  if (formatted == NULL) {
35  printf("failed to format colored string\n");
36  return NULL;
37  }
38  strcat(formatted, pcolor);
39  strcat(formatted, message);
40  return formatted;
41 }
42 
43 void print_colored(COLORS color, char *message) {
44  printf("%s%s%s\n", get_ansi_color_scheme(color), message, ANSI_COLOR_RESET);
45 }
46 
47 int write_colored(COLORS color, int file_descriptor, char *message) {
48  char *pcolor = get_ansi_color_scheme(color);
49  char *reset = get_ansi_color_scheme(COLORS_RESET);
50  // 2 for \n
51  char *write_message = calloc(sizeof(char), strlen(pcolor) + strlen(reset) + strlen(message) + 2);
52  if (write_message == NULL) {
53  printf("failed to calloc write_message\n");
54  return -1;
55  }
56  strcat(write_message, pcolor);
57  strcat(write_message, message);
58  strcat(write_message, reset);
59  strcat(write_message, "\n");
60  int response = write(file_descriptor, write_message, strlen(write_message));
61  free(write_message);
62  if (response == -1) {
63  printf("failed to write colored message\n");
64  return response;
65  }
66  return 0;
67 }
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
COLORS_MAGENTA
@ COLORS_MAGENTA
Definition: colors.h:26
COLORS_GREEN
@ COLORS_GREEN
Definition: colors.h:26
ANSI_COLOR_RESET
#define ANSI_COLOR_RESET
Definition: colors.h:20
ANSI_COLOR_YELLOW
#define ANSI_COLOR_YELLOW
Definition: colors.h:16
format_colored
char * format_colored(COLORS color, char *message)
returns a char * with the message formatted with ansi colors
Definition: colors.c:31
COLORS_SOFT_RED
@ COLORS_SOFT_RED
Definition: colors.h:26
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_RESET
@ COLORS_RESET
Definition: colors.h:26
COLORS_BLUE
@ COLORS_BLUE
Definition: colors.h:26
COLORS_YELLOW
@ COLORS_YELLOW
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
ANSI_COLOR_RED
#define ANSI_COLOR_RED
Definition: colors.h:13
ANSI_COLOR_CYAN
#define ANSI_COLOR_CYAN
Definition: colors.h:19
ANSI_COLOR_MAGENTA
#define ANSI_COLOR_MAGENTA
Definition: colors.h:18
ANSI_COLOR_SOFT_RED
#define ANSI_COLOR_SOFT_RED
Definition: colors.h:14
ANSI_COLOR_BLUE
#define ANSI_COLOR_BLUE
Definition: colors.h:17
ANSI_COLOR_GREEN
#define ANSI_COLOR_GREEN
Definition: colors.h:15
print_colored
void print_colored(COLORS color, char *message)
prints message to stdout with the given color
Definition: colors.c:43