Skip to contentSkip to conversation

ip: Add color output option

PATCH Superseded Mathias Nyman 2 participants 3 messages Apr 24, 2015 – Apr 28, 2015

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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

---

v3 - Rebase on top of iproute2 master branch
- Fix checkpatch.pl issues

 include/color.h | 16 +++++++++++++++
 ip/ip.c         |  5 ++++-
 ip/ipaddress.c  | 43 ++++++++++++++++++++++++++++----------
 lib/Makefile    |  2 +-
 lib/color.c     | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 man/man8/ip.8   |  7 ++++++-
 6 files changed, 123 insertions(+), 14 deletions(-)
 create mode 100644 include/color.h
 create mode 100644 lib/color.c
include/color.h new file +16 -0
new file mode 100644
index 0000000..b85003a
@@ -0,0 +1,16 @@
+#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 +64 -0

64 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.6
#1 lore raw

Patch does not apply cleanly to current version in git. All patches should be based off of

http://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git

I suspect your mailer is corrupting whitespace.
Maybe easier to send patch as an attachment.

$ patch -p1 < ~/Downloads/v3-iproute2-ip-Add-color-output-option.patch 
patching file include/color.h
patching file ip/ip.c
Hunk #1 FAILED at 23.
Hunk #2 FAILED at 56.
Hunk #3 FAILED at 257.
3 out of 3 hunks FAILED -- saving rejects to file ip/ip.c.rej
patching file ip/ipaddress.c
Hunk #1 FAILED at 34.
Hunk #2 FAILED at 136.
Hunk #3 FAILED at 606.
Hunk #4 FAILED at 666.
Hunk #5 FAILED at 849.
5 out of 5 hunks FAILED -- saving rejects to file ip/ipaddress.c.rej
patching file lib/Makefile
Hunk #1 FAILED at 7.
1 out of 1 hunk FAILED -- saving rejects to file lib/Makefile.rej
patching file lib/color.c
patching file man/man8/ip.8
Hunk #1 FAILED at 33.
Hunk #2 FAILED at 165.
2 out of 2 hunks FAILED -- saving rejects to file man/man8/ip.8.rej
#2 lore raw
Quoted 22 lines · Stephen Hemminger

Patch does not apply cleanly to current version in git. All patches should be based off of

http://git.kernel.org/pub/scm/linux/kernel/git/shemminger/iproute2.git

I suspect your mailer is corrupting whitespace.
Maybe easier to send patch as an attachment.

$ patch -p1 < ~/Downloads/v3-iproute2-ip-Add-color-output-option.patch 
patching file include/color.h
patching file ip/ip.c
Hunk #1 FAILED at 23.
Hunk #2 FAILED at 56.
Hunk #3 FAILED at 257.
3 out of 3 hunks FAILED -- saving rejects to file ip/ip.c.rej
patching file ip/ipaddress.c
Hunk #1 FAILED at 34.
Hunk #2 FAILED at 136.
Hunk #3 FAILED at 606.
Hunk #4 FAILED at 666.
Hunk #5 FAILED at 849.
5 out of 5 hunks FAILED -- saving rejects to file ip/ipaddress.c.rej
Quoted 9 lines
patching file lib/Makefile
Hunk #1 FAILED at 7.
1 out of 1 hunk FAILED -- saving rejects to file lib/Makefile.rej
patching file lib/color.c
patching file man/man8/ip.8
Hunk #1 FAILED at 33.
Hunk #2 FAILED at 165.
2 out of 2 hunks FAILED -- saving rejects to file man/man8/ip.8.rej

My email client (mutt) was adding leading spaces. Sorry for the hassle. Sending new patch set (v4).