]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/color.c
eaf69e74d673a6e949e68c504d54777927789b79
1 /* SPDX-License-Identifier: GPL-2.0 */
7 #include <sys/socket.h>
32 static const char * const color_codes
[] = {
51 /* light background */
52 static enum color attr_colors_light
[] = {
63 static enum color attr_colors_dark
[] = {
73 static int is_dark_bg
;
74 static int color_is_enabled
;
76 void enable_color(void)
82 bool check_enable_color(int color
, int json
)
84 if (json
|| color
== COLOR_OPT_NEVER
)
87 if (color
== COLOR_OPT_ALWAYS
|| isatty(fileno(stdout
))) {
94 bool matches_color(const char *arg
, int *val
)
102 p
= strchrnul(dup
, '=');
106 if (matches(dup
, "-color"))
109 if (*p
== '\0' || !strcmp(p
, "always"))
110 *val
= COLOR_OPT_ALWAYS
;
111 else if (!strcmp(p
, "auto"))
112 *val
= COLOR_OPT_AUTO
;
113 else if (!strcmp(p
, "never"))
114 *val
= COLOR_OPT_NEVER
;
120 void set_color_palette(void)
122 char *p
= getenv("COLORFGBG");
125 * COLORFGBG environment variable usually contains either two or three
126 * values separated by semicolons; we want the last value in either case.
127 * If this value is 0-6 or 8, background is dark.
129 if (p
&& (p
= strrchr(p
, ';')) != NULL
130 && ((p
[1] >= '0' && p
[1] <= '6') || p
[1] == '8')
135 int color_fprintf(FILE *fp
, enum color_attr attr
, const char *fmt
, ...)
142 if (!color_is_enabled
|| attr
== COLOR_NONE
) {
143 ret
= vfprintf(fp
, fmt
, args
);
147 ret
+= fprintf(fp
, "%s", color_codes
[is_dark_bg
?
148 attr_colors_dark
[attr
] : attr_colors_light
[attr
]]);
150 ret
+= vfprintf(fp
, fmt
, args
);
151 ret
+= fprintf(fp
, "%s", color_codes
[C_CLEAR
]);
158 enum color_attr
ifa_family_color(__u8 ifa_family
)
160 switch (ifa_family
) {
170 enum color_attr
oper_state_color(__u8 state
)
174 return COLOR_OPERSTATE_UP
;
176 return COLOR_OPERSTATE_DOWN
;