Skip to contentSkip to conversation

ip: Add color output option

PATCH Superseded Mathias Nyman 2 participants 3 messages Apr 18, 2015 – Apr 20, 2015

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ip: Add color output option

It is hard to quickly find what you are looking for in the output of the ip
command. Color helps.

This patch adds a '-c' flag to highlight these with individual colors:
- interface name
- ip address
- mac address
- up/down state

---
 include/color.h | 17 +++++++++++++++
 ip/ip.c         |  5 ++++-
 ip/ipaddress.c  | 43 +++++++++++++++++++++++++++----------
 lib/Makefile    |  2 +-
 lib/color.c     | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 man/man8/ip.8   |  7 +++++-
 6 files changed, 126 insertions(+), 14 deletions(-)
 create mode 100644 include/color.h
 create mode 100644 lib/color.c
include/color.h new file +17 -0
new file mode 100644
index 0000000..1662c62
@@ -0,0 +1,17 @@
+#ifndef __COLOR_H__
+#define __COLOR_H__ 1
+
+enum color_attr
+{
+	COLOR_IFNAME,
+	COLOR_MAC,
+	COLOR_INET,
+	COLOR_INET6,
+	COLOR_OPERSTATE_UP,
+	COLOR_OPERSTATE_DOWN
+};
+
+void enable_color(void);
+int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...);
+
+#endif
ip/ip.c +4 -1
index f7f214b..c23de74 100644
@@ -23,6 +23,7 @@
 #include "utils.h"
 #include "ip_common.h"
 #include "namespace.h"
+#include "color.h"
 
 int preferred_family = AF_UNSPEC;
 int human_readable = 0;
@@ -56,7 +57,7 @@ static void usage(void)
 "                    -4 | -6 | -I | -D | -B | -0 |\n"
 "                    -l[oops] { maximum-addr-flush-attempts } |\n"
 "                    -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n"
-"                    -rc[vbuf] [size] | -n[etns] name | -a[ll] }\n");
+"                    -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}\n");
 	exit(-1);
 }
 
@@ -257,6 +258,8 @@ int main(int argc, char **argv)
 				exit(-1);
 			}
 			rcvbuf = size;
+		} else if (matches(opt, "-color") == 0) {
+			enable_color();
 		} else if (matches(opt, "-help") == 0) {
 			usage();
 		} else if (matches(opt, "-netns") == 0) {
ip/ipaddress.c +32 -11

73 lines not on this page · raw message

lib/Makefile +1 -1
index 4c7cbc2..1d4045f 100644
@@ -7,7 +7,7 @@ endif
 CFLAGS += -fPIC
 
 UTILOBJ=utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o inet_proto.o namespace.o \
-	names.o
+	names.o color.o
 
 NLOBJ=libgenl.o ll_map.o libnetlink.o
 
lib/color.c new file +66 -0

66 lines not on this page · raw message

man/man8/ip.8 +6 -1
index 44d1ee6..43ddac9 100644
@@ -33,7 +33,8 @@ ip \- show / manipulate routing, devices, policy routing and tunnels
 .BR inet " | " inet6 " | " ipx " | " dnet " | " link " } | "
 \fB\-o\fR[\fIneline\fR] |
 \fB\-n\fR[\fIetns\fR] name |
-\fB\-a\fR[\fIll\fR] }
+\fB\-a\fR[\fIll\fR] |
+\fB\-c\fR[\fIolor\fR] }
 
 
 .SH OPTIONS
@@ -165,6 +166,10 @@ to
 .BR "\-a" , " \-all"
 executes specified command over all objects, it depends if command supports this option.
 
+.TP
+.BR "\-c" , " -color"
+Use color output.
+
 .SH IP - COMMAND SYNTAX
 
 .SS
Signature · 1 line
2.3.5
#1 lore raw
Quoted 11 lines · On Sat, 18 Apr 2015 13:39:45 +0300 Mathias Nyman <[email protected]> wrote:

It is hard to quickly find what you are looking for in the output of the ip
command. Color helps.

This patch adds a '-c' flag to highlight these with individual colors:
- interface name
- ip address
- mac address
- up/down state

I like the idea of this, it would be generally good across the board.

But the patch does not apply cleanly to the current version of iproute2.

And there are minor style issues. iproute2 in general ties to follow kernel style.

WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #36:
new file mode 100644

ERROR: open brace '{' following enum go on the same line #45: FILE: include/color.h:5:
+enum color_attr
+{

ERROR: open brace '{' following enum go on the same line #195: FILE: lib/color.c:7:
+enum color
+{

ERROR: that open brace { should be on the previous line #207: FILE: lib/color.c:19:
+static const char * const color_codes[] =
+{

ERROR: that open brace { should be on the previous line #220: FILE: lib/color.c:32:
+static enum color attr_colors[] =
+{

ERROR: do not initialise statics to 0 or NULL
#229: FILE: lib/color.c:41:
+static int color_is_enabled = 0;

WARNING: Missing a blank line after declarations
#240: FILE: lib/color.c:52:
+ va_list args;
+ va_start(args, fmt);

#2 lore raw

Thanks for bearing with my first patch.

Quoted 13 lines · Stephen Hemminger
Quoted 10 lines · On Sat, 18 Apr 2015 13:39:45 +0300 Mathias Nyman <[email protected]> wrote:

It is hard to quickly find what you are looking for in the output of the ip
command. Color helps.

This patch adds a '-c' flag to highlight these with individual colors:
- interface name
- ip address
- mac address
- up/down state

Quoted 5 lines
Quoted 1 line

I like the idea of this, it would be generally good across the board.

But the patch does not apply cleanly to the current version of iproute2.

What is the current version? I used the net-next branch as a base from here:
http://git.kernel.org/cgit/linux/kernel/git/shemminger/iproute2.git

I thought net-next was used for new features, but master branch now has newer commits. Should I rebase on top of master?

Quoted 32 lines

And there are minor style issues. iproute2 in general ties to follow kernel style.

WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #36:
new file mode 100644

ERROR: open brace '{' following enum go on the same line #45: FILE: include/color.h:5:

include/color.h new file +2 -0
+enum color_attr
+{

ERROR: open brace '{' following enum go on the same line #195: FILE: lib/color.c:7:

(quoted diff) +2 -0
+enum color
+{

ERROR: that open brace { should be on the previous line #207: FILE: lib/color.c:19:

(quoted diff) +2 -0
+static const char * const color_codes[] =
+{

ERROR: that open brace { should be on the previous line #220: FILE: lib/color.c:32:

(quoted diff) +2 -0
+static enum color attr_colors[] =
+{

ERROR: do not initialise statics to 0 or NULL
#229: FILE: lib/color.c:41:
+static int color_is_enabled = 0;

WARNING: Missing a blank line after declarations
#240: FILE: lib/color.c:52:

lib/color.c line 52

Quoted 2 lines
lib/color.c new file +2 -0
+	va_list args;
+	va_start(args, fmt);

Thanks, so checkpatch.pl applies to iproute2 as well. I'll fix these in v3.