Implement the -color option; in this case -co is ambiguous
since it was already used for -conf.
For now this just means putting device name in color.

Signed-off-by: Stephen Hemminger <step...@networkplumber.org>
---
 man/man8/tc.8  |  9 +++++++--
 tc/tc.c        | 15 +++++++++++----
 tc/tc_filter.c |  3 +--
 tc/tc_qdisc.c  |  3 +--
 tc/tc_util.c   | 11 +++++++++++
 tc/tc_util.h   |  1 +
 6 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/man/man8/tc.8 b/man/man8/tc.8
index a58f46542340..a20b06bb43c0 100644
--- a/man/man8/tc.8
+++ b/man/man8/tc.8
@@ -94,10 +94,11 @@ tc \- show / manipulate traffic control settings
 \fB\-s\fR[\fItatistics\fR] |
 \fB\-d\fR[\fIetails\fR] |
 \fB\-r\fR[\fIaw\fR] |
-\fB\-p\fR[\fIretty\fR] |
 \fB\-i\fR[\fIec\fR] |
 \fB\-g\fR[\fIraph\fR] |
-\fB\-j\fR[\fIjson\fR] }
+\fB\-j\fR[\fIjson\fR] |
+\fB\-p\fR[\fIretty\fR] |
+\fB\-col\fR[\fIor\fR] }
 
 .SH DESCRIPTION
 .B Tc
@@ -685,6 +686,10 @@ option was specified. Classes can be filtered only by
 .BR "dev"
 option.
 
+.TP
+.BR "\ -color"
+Use color output.
+
 .TP
 .BR "\-j", " \-json"
 Display results in JSON format.
diff --git a/tc/tc.c b/tc/tc.c
index cfccf8750562..a31f075d1ffe 100644
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -41,6 +41,7 @@ int use_iec;
 int force;
 bool use_names;
 int json;
+int color;
 
 static char *conf_file;
 
@@ -186,10 +187,11 @@ noexist:
 static void usage(void)
 {
        fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
-                       "       tc [-force] -batch filename\n"
-                       "where  OBJECT := { qdisc | class | filter | action | 
monitor | exec }\n"
-                       "       OPTIONS := { -s[tatistics] | -d[etails] | 
-r[aw] | -p[retty] | -b[atch] [filename] | -n[etns] name |\n"
-                       "                    -nm | -nam[es] | { -cf | -conf } 
path } | -j[son]\n");
+               "       tc [-force] -batch filename\n"
+               "where  OBJECT := { qdisc | class | filter | action | monitor | 
exec }\n"
+               "       OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | 
-b[atch] [filename] | -n[etns] name |\n"
+               "                    -nm | -nam[es] | { -cf | -conf } path } 
|\n"
+               "                    -j[son] -p[retty] -c[olor]\n");
 }
 
 static int do_cmd(int argc, char **argv, void *buf, size_t buflen)
@@ -476,6 +478,8 @@ int main(int argc, char **argv)
                                matches(argv[1], "-conf") == 0) {
                        NEXT_ARG();
                        conf_file = argv[1];
+               } else if (matches(argv[1], "-color") == 0) {
+                       ++color;
                } else if (matches(argv[1], "-timestamp") == 0) {
                        timestamp++;
                } else if (matches(argv[1], "-tshort") == 0) {
@@ -490,6 +494,9 @@ int main(int argc, char **argv)
                argc--; argv++;
        }
 
+       if (color & !json)
+               enable_color();
+
        if (batch_file)
                return batch(batch_file);
 
diff --git a/tc/tc_filter.c b/tc/tc_filter.c
index 5c31a4cea658..c7701fd8956e 100644
--- a/tc/tc_filter.c
+++ b/tc/tc_filter.c
@@ -303,8 +303,7 @@ int print_filter(const struct sockaddr_nl *who, struct 
nlmsghdr *n, void *arg)
                                   t->tcm_block_index);
        } else {
                if (!filter_ifindex || filter_ifindex != t->tcm_ifindex)
-                       print_string(PRINT_ANY, "dev", "dev %s ",
-                                    ll_index_to_name(t->tcm_ifindex));
+                       print_devname(PRINT_ANY, t->tcm_ifindex);
 
                if (!filter_parent || filter_parent != t->tcm_parent) {
                        if (t->tcm_parent == TC_H_ROOT)
diff --git a/tc/tc_qdisc.c b/tc/tc_qdisc.c
index 2fcf04c336df..78b80e67cf57 100644
--- a/tc/tc_qdisc.c
+++ b/tc/tc_qdisc.c
@@ -270,8 +270,7 @@ int print_qdisc(const struct sockaddr_nl *who,
        print_string(PRINT_FP, NULL, " ", NULL);
 
        if (filter_ifindex == 0)
-               print_string(PRINT_ANY, "dev", "dev %s ",
-                            ll_index_to_name(t->tcm_ifindex));
+               print_devname(PRINT_ANY, t->tcm_ifindex);
 
        if (t->tcm_parent == TC_H_ROOT)
                print_bool(PRINT_ANY, "root", "root ", true);
diff --git a/tc/tc_util.c b/tc/tc_util.c
index aceb0d944933..7f6a8e3109c4 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -444,6 +444,17 @@ int get_size_and_cell(unsigned int *size, int *cell_log, 
char *str)
        return 0;
 }
 
+void print_devname(enum output_type type, int ifindex)
+{
+       const char *ifname = ll_index_to_name(ifindex);
+
+       if (!is_json_context())
+               printf("dev ");
+
+       print_color_string(type, COLOR_IFNAME,
+                          "dev", "%s ", ifname);
+}
+
 void print_size(char *buf, int len, __u32 sz)
 {
        double tmp = sz;
diff --git a/tc/tc_util.h b/tc/tc_util.h
index cd2ff5964e19..6632c4f9c528 100644
--- a/tc/tc_util.h
+++ b/tc/tc_util.h
@@ -89,6 +89,7 @@ void print_size(char *buf, int len, __u32 size);
 void print_qdisc_handle(char *buf, int len, __u32 h);
 void print_time(char *buf, int len, __u32 time);
 void print_linklayer(char *buf, int len, unsigned int linklayer);
+void print_devname(enum output_type type, int ifindex);
 
 char *sprint_rate(__u64 rate, char *buf);
 char *sprint_size(__u32 size, char *buf);
-- 
2.16.1

Reply via email to