]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
tc: implement color output
authorStephen Hemminger <stephen@networkplumber.org>
Wed, 21 Feb 2018 00:39:34 +0000 (16:39 -0800)
committerDavid Ahern <dsahern@gmail.com>
Wed, 21 Feb 2018 17:12:28 +0000 (09:12 -0800)
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 <stephen@networkplumber.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
man/man8/tc.8
tc/tc.c
tc/tc_filter.c
tc/tc_qdisc.c
tc/tc_util.c
tc/tc_util.h

index a58f46542340948c0066e24e517aa79ecf6da9aa..a20b06bb43c0c9162aa0aabaebeabe65c5e3ceb3 100644 (file)
@@ -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 cfccf8750562f4ce76a269b7480170ce99c95c12..a31f075d1ffe640c236192150d71432280e69613 100644 (file)
--- 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);
 
index 5c31a4cea658b229fb21b6888634b773acb699d0..c7701fd8956ec9736c972fb04856676c2fa349a6 100644 (file)
@@ -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)
index 2fcf04c336dfe9da17362cc0f93768080b87b5e4..78b80e67cf57fe1dac6e282f99100fd524d93082 100644 (file)
@@ -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);
index aceb0d9449337a64b71596072b5df370f49ea97b..7f6a8e3109c41967c18311c2e62b2b5644b1fdb0 100644 (file)
@@ -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;
index cd2ff5964e19938adf1eae0db81b09049278cf13..6632c4f9c528265a2568f94f5e17249c3f94551e 100644 (file)
@@ -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);