]> git.proxmox.com Git - mirror_iproute2.git/blame - lib/color.c
Merge common code for conditionally colored output
[mirror_iproute2.git] / lib / color.c
CommitLineData
6054c1eb 1/* SPDX-License-Identifier: GPL-2.0 */
d7bd2db5
MN
2#include <stdio.h>
3#include <stdarg.h>
54eab4c7
PV
4#include <stdlib.h>
5#include <string.h>
a1121aa1
PS
6#include <sys/socket.h>
7#include <sys/types.h>
8#include <linux/if.h>
d7bd2db5
MN
9
10#include "color.h"
11
12enum color {
13 C_RED,
14 C_GREEN,
15 C_YELLOW,
16 C_BLUE,
17 C_MAGENTA,
18 C_CYAN,
19 C_WHITE,
54eab4c7
PV
20 C_BOLD_RED,
21 C_BOLD_GREEN,
22 C_BOLD_YELLOW,
23 C_BOLD_BLUE,
24 C_BOLD_MAGENTA,
25 C_BOLD_CYAN,
26 C_BOLD_WHITE,
d7bd2db5
MN
27 C_CLEAR
28};
29
30static const char * const color_codes[] = {
31 "\e[31m",
32 "\e[32m",
33 "\e[33m",
34 "\e[34m",
35 "\e[35m",
36 "\e[36m",
37 "\e[37m",
54eab4c7
PV
38 "\e[1;31m",
39 "\e[1;32m",
40 "\e[1;33m",
41 "\e[1;34m",
42 "\e[1;35m",
43 "\e[1;36m",
44 "\e[1;37m",
d7bd2db5
MN
45 "\e[0m",
46 NULL,
47};
48
99b89c51
PV
49/* light background */
50static enum color attr_colors_light[] = {
d7bd2db5
MN
51 C_CYAN,
52 C_YELLOW,
53 C_MAGENTA,
54 C_BLUE,
55 C_GREEN,
a1121aa1 56 C_RED,
54eab4c7 57 C_CLEAR,
99b89c51 58};
54eab4c7 59
99b89c51
PV
60/* dark background */
61static enum color attr_colors_dark[] = {
54eab4c7
PV
62 C_BOLD_CYAN,
63 C_BOLD_YELLOW,
64 C_BOLD_MAGENTA,
65 C_BOLD_BLUE,
66 C_BOLD_GREEN,
67 C_BOLD_RED,
a1121aa1 68 C_CLEAR
d7bd2db5
MN
69};
70
54eab4c7 71static int is_dark_bg;
d7bd2db5
MN
72static int color_is_enabled;
73
74void enable_color(void)
75{
76 color_is_enabled = 1;
54eab4c7
PV
77 set_color_palette();
78}
79
4d82962c
PS
80int check_enable_color(int color, int json)
81{
82 if (color && !json) {
83 enable_color();
84 return 0;
85 }
86 return 1;
87}
88
54eab4c7
PV
89void set_color_palette(void)
90{
91 char *p = getenv("COLORFGBG");
92
93 /*
94 * COLORFGBG environment variable usually contains either two or three
95 * values separated by semicolons; we want the last value in either case.
96 * If this value is 0-6 or 8, background is dark.
97 */
98 if (p && (p = strrchr(p, ';')) != NULL
99 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
100 && p[2] == '\0')
101 is_dark_bg = 1;
d7bd2db5
MN
102}
103
104int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...)
105{
106 int ret = 0;
107 va_list args;
108
109 va_start(args, fmt);
110
4b73d52f 111 if (!color_is_enabled || attr == COLOR_NONE) {
d7bd2db5
MN
112 ret = vfprintf(fp, fmt, args);
113 goto end;
114 }
115
99b89c51
PV
116 ret += fprintf(fp, "%s", color_codes[is_dark_bg ?
117 attr_colors_dark[attr] : attr_colors_light[attr]]);
118
d7bd2db5
MN
119 ret += vfprintf(fp, fmt, args);
120 ret += fprintf(fp, "%s", color_codes[C_CLEAR]);
121
122end:
123 va_end(args);
124 return ret;
125}
a1121aa1
PS
126
127enum color_attr ifa_family_color(__u8 ifa_family)
128{
129 switch (ifa_family) {
130 case AF_INET:
131 return COLOR_INET;
132 case AF_INET6:
133 return COLOR_INET6;
134 default:
4b73d52f 135 return COLOR_NONE;
a1121aa1
PS
136 }
137}
138
139enum color_attr oper_state_color(__u8 state)
140{
141 switch (state) {
142 case IF_OPER_UP:
143 return COLOR_OPERSTATE_UP;
144 case IF_OPER_DOWN:
145 return COLOR_OPERSTATE_DOWN;
146 default:
4b73d52f 147 return COLOR_NONE;
a1121aa1
PS
148 }
149}