Commit a1121aa1 authored by Phil Sutter's avatar Phil Sutter Committed by Stephen Hemminger

color: introduce color helpers and COLOR_CLEAR

This adds two helper functions which map a given data field to a color,
so color_fprintf() statements don't have to be duplicated with only a
different color value depending on that data field's value. In order for
this to work in a generic way, COLOR_CLEAR has been added to serve as a
fallback default of uncolored output.
Signed-off-by: default avatarPhil Sutter <phil@nwl.cc>
parent 16418561
...@@ -7,10 +7,13 @@ enum color_attr { ...@@ -7,10 +7,13 @@ enum color_attr {
COLOR_INET, COLOR_INET,
COLOR_INET6, COLOR_INET6,
COLOR_OPERSTATE_UP, COLOR_OPERSTATE_UP,
COLOR_OPERSTATE_DOWN COLOR_OPERSTATE_DOWN,
COLOR_CLEAR
}; };
void enable_color(void); void enable_color(void);
int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...); int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...);
enum color_attr ifa_family_color(__u8 ifa_family);
enum color_attr oper_state_color(__u8 state);
#endif #endif
...@@ -135,25 +135,15 @@ static const char *oper_states[] = { ...@@ -135,25 +135,15 @@ static const char *oper_states[] = {
static void print_operstate(FILE *f, __u8 state) static void print_operstate(FILE *f, __u8 state)
{ {
if (state >= ARRAY_SIZE(oper_states)) if (state >= ARRAY_SIZE(oper_states)) {
fprintf(f, "state %#x ", state); fprintf(f, "state %#x ", state);
else { } else if (brief) {
if (brief) { color_fprintf(f, oper_state_color(state),
if (strcmp(oper_states[state], "UP") == 0) "%-14s ", oper_states[state]);
color_fprintf(f, COLOR_OPERSTATE_UP, "%-14s ", oper_states[state]); } else {
else if (strcmp(oper_states[state], "DOWN") == 0) fprintf(f, "state ");
color_fprintf(f, COLOR_OPERSTATE_DOWN, "%-14s ", oper_states[state]); color_fprintf(f, oper_state_color(state),
else "%s ", oper_states[state]);
fprintf(f, "%-14s ", oper_states[state]);
} else {
fprintf(f, "state ");
if (strcmp(oper_states[state], "UP") == 0)
color_fprintf(f, COLOR_OPERSTATE_UP, "%s ", oper_states[state]);
else if (strcmp(oper_states[state], "DOWN") == 0)
color_fprintf(f, COLOR_OPERSTATE_DOWN, "%s ", oper_states[state]);
else
fprintf(f, "%s ", oper_states[state]);
}
} }
} }
...@@ -1067,22 +1057,11 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, ...@@ -1067,22 +1057,11 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
} }
if (rta_tb[IFA_LOCAL]) { if (rta_tb[IFA_LOCAL]) {
if (ifa->ifa_family == AF_INET) color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s",
color_fprintf(fp, COLOR_INET, "%s", format_host(ifa->ifa_family, format_host(ifa->ifa_family,
RTA_PAYLOAD(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
RTA_DATA(rta_tb[IFA_LOCAL]), RTA_DATA(rta_tb[IFA_LOCAL]),
abuf, sizeof(abuf))); abuf, sizeof(abuf)));
else if (ifa->ifa_family == AF_INET6)
color_fprintf(fp, COLOR_INET6, "%s", format_host(ifa->ifa_family,
RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
RTA_DATA(rta_tb[IFA_LOCAL]),
abuf, sizeof(abuf)));
else
fprintf(fp, "%s", format_host(ifa->ifa_family,
RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
RTA_DATA(rta_tb[IFA_LOCAL]),
abuf, sizeof(abuf)));
if (rta_tb[IFA_ADDRESS] == NULL || if (rta_tb[IFA_ADDRESS] == NULL ||
memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]),
ifa->ifa_family == AF_INET ? 4 : 16) == 0) { ifa->ifa_family == AF_INET ? 4 : 16) == 0) {
......
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if.h>
#include "color.h" #include "color.h"
...@@ -32,7 +35,8 @@ static enum color attr_colors[] = { ...@@ -32,7 +35,8 @@ static enum color attr_colors[] = {
C_MAGENTA, C_MAGENTA,
C_BLUE, C_BLUE,
C_GREEN, C_GREEN,
C_RED C_RED,
C_CLEAR
}; };
static int color_is_enabled; static int color_is_enabled;
...@@ -62,3 +66,27 @@ end: ...@@ -62,3 +66,27 @@ end:
va_end(args); va_end(args);
return ret; return ret;
} }
enum color_attr ifa_family_color(__u8 ifa_family)
{
switch (ifa_family) {
case AF_INET:
return COLOR_INET;
case AF_INET6:
return COLOR_INET6;
default:
return COLOR_CLEAR;
}
}
enum color_attr oper_state_color(__u8 state)
{
switch (state) {
case IF_OPER_UP:
return COLOR_OPERSTATE_UP;
case IF_OPER_DOWN:
return COLOR_OPERSTATE_DOWN;
default:
return COLOR_CLEAR;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment