Skip to contentSkip to conversation

color: disable color when json output is requested

PATCH In review Vincent Bernat 2 participants 3 messages Feb 20, 2018 – Feb 21, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 color: disable color when json output is requested

Instead of declaring -color and -json exclusive, ignore -color when -json is provided. The rationale is to allow to put -color in an alias for ip while still being able to use -json. -color is merely a presentation suggestion and we can assume there is nothing to color in the JSON output.

---
 include/color.h | 1 -
 ip/ip.c         | 7 ++++---
 lib/color.c     | 8 --------
 3 files changed, 4 insertions(+), 12 deletions(-)
include/color.h +0 -1
index f6c351b77746..c80359d3e2e9 100644
@@ -13,7 +13,6 @@ enum color_attr {
 };
 
 void enable_color(void);
-void check_if_color_enabled(void);
 void set_color_palette(void);
 int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...);
 enum color_attr ifa_family_color(__u8 ifa_family);
ip/ip.c +4 -3
index b15e6b66b3f6..ebf77aa2ee37 100644
@@ -172,6 +172,7 @@ int main(int argc, char **argv)
 {
 	char *basename;
 	char *batch_file = NULL;
+	int color = 0;
 
 	basename = strrchr(argv[0], '/');
 	if (basename == NULL)
@@ -273,7 +274,7 @@ int main(int argc, char **argv)
 			}
 			rcvbuf = size;
 		} else if (matches(opt, "-color") == 0) {
-			enable_color();
+			++color;
 		} else if (matches(opt, "-help") == 0) {
 			usage();
 		} else if (matches(opt, "-netns") == 0) {
@@ -293,8 +294,8 @@ int main(int argc, char **argv)
 
 	_SL_ = oneline ? "\\" : "\n";
 
-	if (json)
-		check_if_color_enabled();
+	if (color && !json)
+		enable_color();
 
 	if (batch_file)
 		return batch(batch_file);
lib/color.c +0 -8
index a13a4930b10c..da1f516cb249 100644
@@ -92,14 +92,6 @@ void set_color_palette(void)
 		is_dark_bg = 1;
 }
 
-void check_if_color_enabled(void)
-{
-	if (color_is_enabled) {
-		fprintf(stderr, "Option \"-json\" conflicts with \"-color\".\n");
-		exit(1);
-	}
-}
-
 int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...)
 {
 	int ret = 0;
Signature · 1 line
2.16.1
#1 lore raw
Quoted 7 lines · On Wed, 21 Feb 2018 00:28:04 +0100 Vincent Bernat <[email protected]> wrote:

Instead of declaring -color and -json exclusive, ignore -color when -json is provided. The rationale is to allow to put -color in an alias for ip while still being able to use -json. -color is merely a presentation suggestion and we can assume there is nothing to color in the JSON output.

Looks fine to me, this could even go into master. Need to update man page and make sure behavior is consistent across ip, tc, and bridge commands.

#2 lore raw

❦ 20 février 2018 16:04 -0800, Stephen Hemminger <[email protected]> :

Quoted 11 lines
Quoted 7 lines

Instead of declaring -color and -json exclusive, ignore -color when -json is provided. The rationale is to allow to put -color in an alias for ip while still being able to use -json. -color is merely a presentation suggestion and we can assume there is nothing to color in the JSON output.

Looks fine to me, this could even go into master. Need to update man page and make sure behavior is consistent across ip, tc, and bridge commands.

Currently, in master or net-next, only the "ip" command has a -color option.

Signature · 3 lines
The human race is a race of cowards; and I am not only marching in that
procession but carrying a banner.
		-- Mark Twain