netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2 1/2] color: Introduce and use default_color_opt() function
@ 2025-03-19 21:51 Ben Hutchings
  2025-03-19 21:51 ` [PATCH iproute2 2/2] color: Handle NO_COLOR environment variable in default_color_opt() Ben Hutchings
  0 siblings, 1 reply; 2+ messages in thread
From: Ben Hutchings @ 2025-03-19 21:51 UTC (permalink / raw)
  To: netdev; +Cc: 1088739

[-- Attachment #1: Type: text/plain, Size: 2225 bytes --]

As a preparatory step for supporting the NO_COLOR environment
variable, replace the direct use of CONF_COLOR with a
default_color_opt() function which initially returns CONF_COLOR.

Signed-off-by: Ben Hutchings <benh@debian.org>
---
 bridge/bridge.c | 2 +-
 include/color.h | 1 +
 ip/ip.c         | 2 +-
 lib/color.c     | 5 +++++
 tc/tc.c         | 2 +-
 5 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/bridge/bridge.c b/bridge/bridge.c
index f8b5646a..d993ba19 100644
--- a/bridge/bridge.c
+++ b/bridge/bridge.c
@@ -103,7 +103,7 @@ static int batch(const char *name)
 int
 main(int argc, char **argv)
 {
-	int color = CONF_COLOR;
+	int color = default_color_opt();
 
 	while (argc > 1) {
 		const char *opt = argv[1];
diff --git a/include/color.h b/include/color.h
index 17ec56f3..b543c267 100644
--- a/include/color.h
+++ b/include/color.h
@@ -20,6 +20,7 @@ enum color_opt {
 	COLOR_OPT_ALWAYS = 2
 };
 
+int default_color_opt(void);
 bool check_enable_color(int color, int json);
 bool matches_color(const char *arg, int *val);
 int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...);
diff --git a/ip/ip.c b/ip/ip.c
index c7151fbd..e4b71bde 100644
--- a/ip/ip.c
+++ b/ip/ip.c
@@ -166,7 +166,7 @@ int main(int argc, char **argv)
 	const char *libbpf_version;
 	char *batch_file = NULL;
 	char *basename;
-	int color = CONF_COLOR;
+	int color = default_color_opt();
 
 	/* to run vrf exec without root, capabilities might be set, drop them
 	 * if not needed as the first thing.
diff --git a/lib/color.c b/lib/color.c
index cd0f9f75..5c4cc329 100644
--- a/lib/color.c
+++ b/lib/color.c
@@ -81,6 +81,11 @@ static void enable_color(void)
 	set_color_palette();
 }
 
+int default_color_opt(void)
+{
+	return CONF_COLOR;
+}
+
 bool check_enable_color(int color, int json)
 {
 	if (json || color == COLOR_OPT_NEVER)
diff --git a/tc/tc.c b/tc/tc.c
index beb88111..0fc658c8 100644
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -254,7 +254,7 @@ int main(int argc, char **argv)
 {
 	const char *libbpf_version;
 	char *batch_file = NULL;
-	int color = CONF_COLOR;
+	int color = default_color_opt();
 	int ret;
 
 	while (argc > 1) {


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH iproute2 2/2] color: Handle NO_COLOR environment variable in default_color_opt()
  2025-03-19 21:51 [PATCH iproute2 1/2] color: Introduce and use default_color_opt() function Ben Hutchings
@ 2025-03-19 21:51 ` Ben Hutchings
  0 siblings, 0 replies; 2+ messages in thread
From: Ben Hutchings @ 2025-03-19 21:51 UTC (permalink / raw)
  To: netdev; +Cc: 1088739

[-- Attachment #1: Type: text/plain, Size: 905 bytes --]

The NO_COLOR environment variable is a widely supported way for users
to disable coloured text output.  See <https://no-color.org/>.  In
case iproute2 is configured to use colours by default, allow this to
be overridden by setting NO_COLOR.

This is done in default_color_opt() so that colours can still be
explicitly enabled with a command-line option.

Signed-off-by: Ben Hutchings <benh@debian.org>
---
 lib/color.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lib/color.c b/lib/color.c
index 5c4cc329..3c6db08d 100644
--- a/lib/color.c
+++ b/lib/color.c
@@ -83,6 +83,13 @@ static void enable_color(void)
 
 int default_color_opt(void)
 {
+	const char *no_color;
+
+	/* If NO_COLOR has a non-empty value, coloured output is never wanted */
+	no_color = getenv("NO_COLOR");
+	if (no_color && *no_color)
+		return COLOR_OPT_NEVER;
+
 	return CONF_COLOR;
 }
 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-03-19 21:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-19 21:51 [PATCH iproute2 1/2] color: Introduce and use default_color_opt() function Ben Hutchings
2025-03-19 21:51 ` [PATCH iproute2 2/2] color: Handle NO_COLOR environment variable in default_color_opt() Ben Hutchings

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).