]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/color.c
5 #include <sys/socket.h>
29 static const char * const color_codes
[] = {
48 static enum color attr_colors
[] = {
49 /* light background */
68 static int is_dark_bg
;
69 static int color_is_enabled
;
71 void enable_color(void)
77 void set_color_palette(void)
79 char *p
= getenv("COLORFGBG");
82 * COLORFGBG environment variable usually contains either two or three
83 * values separated by semicolons; we want the last value in either case.
84 * If this value is 0-6 or 8, background is dark.
86 if (p
&& (p
= strrchr(p
, ';')) != NULL
87 && ((p
[1] >= '0' && p
[1] <= '6') || p
[1] == '8')
92 void check_if_color_enabled(void)
94 if (color_is_enabled
) {
95 fprintf(stderr
, "Option \"-json\" conflicts with \"-color\".\n");
100 int color_fprintf(FILE *fp
, enum color_attr attr
, const char *fmt
, ...)
107 if (!color_is_enabled
|| attr
== COLOR_NONE
) {
108 ret
= vfprintf(fp
, fmt
, args
);
112 ret
+= fprintf(fp
, "%s",
113 color_codes
[attr_colors
[is_dark_bg
? attr
+ 8 : attr
]]);
114 ret
+= vfprintf(fp
, fmt
, args
);
115 ret
+= fprintf(fp
, "%s", color_codes
[C_CLEAR
]);
122 enum color_attr
ifa_family_color(__u8 ifa_family
)
124 switch (ifa_family
) {
134 enum color_attr
oper_state_color(__u8 state
)
138 return COLOR_OPERSTATE_UP
;
140 return COLOR_OPERSTATE_DOWN
;