]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/color.c
1 /* SPDX-License-Identifier: GPL-2.0 */
7 #include <sys/socket.h>
14 static void set_color_palette(void);
34 static const char * const color_codes
[] = {
53 /* light background */
54 static enum color attr_colors_light
[] = {
65 static enum color attr_colors_dark
[] = {
75 static int is_dark_bg
;
76 static int color_is_enabled
;
78 static void enable_color(void)
84 bool check_enable_color(int color
, int json
)
86 if (json
|| color
== COLOR_OPT_NEVER
)
89 if (color
== COLOR_OPT_ALWAYS
|| isatty(fileno(stdout
))) {
96 bool matches_color(const char *arg
, int *val
)
104 p
= strchrnul(dup
, '=');
108 if (matches(dup
, "-color"))
111 if (*p
== '\0' || !strcmp(p
, "always"))
112 *val
= COLOR_OPT_ALWAYS
;
113 else if (!strcmp(p
, "auto"))
114 *val
= COLOR_OPT_AUTO
;
115 else if (!strcmp(p
, "never"))
116 *val
= COLOR_OPT_NEVER
;
122 static void set_color_palette(void)
124 char *p
= getenv("COLORFGBG");
127 * COLORFGBG environment variable usually contains either two or three
128 * values separated by semicolons; we want the last value in either case.
129 * If this value is 0-6 or 8, background is dark.
131 if (p
&& (p
= strrchr(p
, ';')) != NULL
132 && ((p
[1] >= '0' && p
[1] <= '6') || p
[1] == '8')
137 __attribute__((format(printf
, 3, 4)))
138 int color_fprintf(FILE *fp
, enum color_attr attr
, const char *fmt
, ...)
145 if (!color_is_enabled
|| attr
== COLOR_NONE
) {
146 ret
= vfprintf(fp
, fmt
, args
);
150 ret
+= fprintf(fp
, "%s", color_codes
[is_dark_bg
?
151 attr_colors_dark
[attr
] : attr_colors_light
[attr
]]);
153 ret
+= vfprintf(fp
, fmt
, args
);
154 ret
+= fprintf(fp
, "%s", color_codes
[C_CLEAR
]);
161 enum color_attr
ifa_family_color(__u8 ifa_family
)
163 switch (ifa_family
) {
173 enum color_attr
oper_state_color(__u8 state
)
177 return COLOR_OPERSTATE_UP
;
179 return COLOR_OPERSTATE_DOWN
;