From 63017f1a3f26d135346d39f0c8f7cee1bc68e2db Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 14 Sep 2023 15:24:58 +0200 Subject: [PATCH 1/2] cli: fix handling empty NO_COLOR= variable An empty value NO_COLOR= should not be treated to disable colors. This is also what [1] says (changed a while ago [2]). [1] https://no-color.org/ [2] https://github.com/jcs/no_color/commit/99f90e27d0fac6a34d07998f8f577e1f2f620c61 (cherry picked from commit 0ac5221c40f3b3d47533fc80b23a852278e259df) --- man/nmcli.xml | 2 +- src/nmcli/nmcli.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/man/nmcli.xml b/man/nmcli.xml index 1caafd3176..0404c55e29 100644 --- a/man/nmcli.xml +++ b/man/nmcli.xml @@ -129,7 +129,7 @@ terminal-colors.d5. Please refer to the section for a list of color names supported by nmcli. - If the environment variable NO_COLOR is set (to any value), + If the environment variable NO_COLOR is set (to any non-empty value), then coloring is disabled with mode "auto". Explicitly enabling coloring overrides the environment variable. diff --git a/src/nmcli/nmcli.c b/src/nmcli/nmcli.c index 792edf6e0e..82df12ee8d 100644 --- a/src/nmcli/nmcli.c +++ b/src/nmcli/nmcli.c @@ -481,7 +481,7 @@ check_colors(NmcColorOption color_option, char **out_palette_str) return FALSE; } - if (color_option == NMC_USE_COLOR_AUTO && g_getenv("NO_COLOR")) { + if (color_option == NMC_USE_COLOR_AUTO && nm_str_not_empty(g_getenv("NO_COLOR"))) { /* https://no-color.org/ */ return FALSE; } From 9233df8c5dd6b8dd14c8ba424cc96505751d0920 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 14 Sep 2023 15:24:58 +0200 Subject: [PATCH 2/2] cli: honor CLICOLOR_FORCE= variable to enable colors with nmcli Note that [1] suggests to also accept an empty value as having the variable set. That is likely a bug ([2]) in the documentation, makes little sense, and is not the case with NO_COLOR ([3]). [1] https://bixense.com/clicolors/ [2] https://github.com/jhasse/clicolors/issues/13 [3] https://no-color.org/ (cherry picked from commit ae06a607b3ac6149e168c94cb752d0d69736adf1) --- man/nmcli.xml | 3 ++- src/nmcli/nmcli.c | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/man/nmcli.xml b/man/nmcli.xml index 0404c55e29..9bda9db0af 100644 --- a/man/nmcli.xml +++ b/man/nmcli.xml @@ -130,7 +130,8 @@ Please refer to the section for a list of color names supported by nmcli. If the environment variable NO_COLOR is set (to any non-empty value), - then coloring is disabled with mode "auto". Explicitly enabling coloring overrides + then coloring is disabled with mode "auto". If the environment variable CLICOLOR_FORCE + is set (to any non-empty value), then coloring is enabled with mode "auto". Explicitly enabling coloring overrides the environment variable. diff --git a/src/nmcli/nmcli.c b/src/nmcli/nmcli.c index 82df12ee8d..6de42faa00 100644 --- a/src/nmcli/nmcli.c +++ b/src/nmcli/nmcli.c @@ -489,7 +489,9 @@ check_colors(NmcColorOption color_option, char **out_palette_str) term = g_getenv("TERM"); if (color_option == NMC_USE_COLOR_AUTO) { - if (nm_streq0(term, "dumb") || !isatty(STDOUT_FILENO)) + if (nm_str_not_empty(g_getenv("CLICOLOR_FORCE"))) { + color_option = NMC_USE_COLOR_YES; + } else if (nm_streq0(term, "dumb") || !isatty(STDOUT_FILENO)) return FALSE; }