Debian Codegraph @

src/utils/color.c

raw
1 #include "utils/color.h"
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 bool color_enable = true;
8 bool log_color_enable = true;
9
10 void
11 set_color (color_mode_t color, FILE *output)
12 {
13 if (color == ALWAYS)
14 {
15 color_enable = true;
16 log_color_enable = true;
17 }
18 else if (color == NEVER)
19 {
20 color_enable = false;
21 log_color_enable = false;
22 }
23
24 else if (color == AUTO)
25 {
26 if (isatty (fileno (output)))
27 color_enable = true;
28 else
29 color_enable = false;
30
31 if (isatty (STDERR_FILENO))
32 log_color_enable = true;
33 else
34 log_color_enable = false;
35 }
36 }
37
38 static bool enable_stack[0x10];
39 // 0x10 is definitely enough;
40 static uint32_t enable_sp = 0;
41
42 void
43 push_color (bool enable)
44 {
45 enable_stack[enable_sp++] = color_enable;
46 color_enable = enable;
47 }
48
49 void
50 pop_color (void)
51 {
52 color_enable = enable_stack[--enable_sp];
53 }