]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/color.c
da1f516cb2492f1c4515274c741218951b969a63
1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <sys/socket.h>
30 static const char * const color_codes
[] = {
49 /* light background */
50 static enum color attr_colors_light
[] = {
61 static enum color attr_colors_dark
[] = {
71 static int is_dark_bg
;
72 static int color_is_enabled
;
74 void enable_color(void)
80 void set_color_palette(void)
82 char *p
= getenv("COLORFGBG");
85 * COLORFGBG environment variable usually contains either two or three
86 * values separated by semicolons; we want the last value in either case.
87 * If this value is 0-6 or 8, background is dark.
89 if (p
&& (p
= strrchr(p
, ';')) != NULL
90 && ((p
[1] >= '0' && p
[1] <= '6') || p
[1] == '8')
95 int color_fprintf(FILE *fp
, enum color_attr attr
, const char *fmt
, ...)
102 if (!color_is_enabled
|| attr
== COLOR_NONE
) {
103 ret
= vfprintf(fp
, fmt
, args
);
107 ret
+= fprintf(fp
, "%s", color_codes
[is_dark_bg
?
108 attr_colors_dark
[attr
] : attr_colors_light
[attr
]]);
110 ret
+= vfprintf(fp
, fmt
, args
);
111 ret
+= fprintf(fp
, "%s", color_codes
[C_CLEAR
]);
118 enum color_attr
ifa_family_color(__u8 ifa_family
)
120 switch (ifa_family
) {
130 enum color_attr
oper_state_color(__u8 state
)
134 return COLOR_OPERSTATE_UP
;
136 return COLOR_OPERSTATE_DOWN
;