[PATCH] display: suppress spotlight yellow and error red when NO_COLOR is set

Benno Schulenberg <[email protected]>
Newsgroups gmane.editors.nano.devel
Message-ID <[email protected]>
This makes nano conform to the https://no-color.org/ idea: suppressing
any color in the output (in the default setup) when NO_COLOR is set in
the environment.
---
 src/color.c      | 6 ++++++
 src/global.c     | 2 ++
 src/nano.c       | 3 +++
 src/prototypes.h | 1 +
 4 files changed, 12 insertions(+)

diff --git a/src/color.c b/src/color.c
index a04982cb..bad60a63 100644
--- a/src/color.c
+++ b/src/color.c
@@ -53,6 +53,7 @@ void set_interface_colorpairs(void)
 			}
 			init_pair(index + 1, combo->fg, combo->bg);
 			interface_color_pair[index] = COLOR_PAIR(index + 1) | combo->attributes;
+			rescind_colors = FALSE;
 		} else {
 			if (index == FUNCTION_TAG || index == SCROLL_BAR)
 				interface_color_pair[index] = A_NORMAL;
@@ -72,6 +73,11 @@ void set_interface_colorpairs(void)
 
 		free(color_combo[index]);
 	}
+
+	if (rescind_colors) {
+		interface_color_pair[SPOTLIGHTED] = A_REVERSE;
+		interface_color_pair[ERROR_MESSAGE] = A_REVERSE;
+	}
 }
 
 /* Assign a pair number to each of the foreground/background color combinations
diff --git a/src/global.c b/src/global.c
index 0a32f139..4bb7535c 100644
--- a/src/global.c
+++ b/src/global.c
@@ -201,6 +201,8 @@ char *syntaxstr = NULL;
 		/* The color syntax name specified on the command line. */
 bool have_palette = FALSE;
 		/* Whether the colors for the current syntax have been initialized. */
+bool rescind_colors = FALSE;
+		/* Becomes TRUE when NO_COLOR is set in the environment. */
 #endif
 
 int currmenu = MMOST;
diff --git a/src/nano.c b/src/nano.c
index 28eb4ef7..5d39b2c3 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2087,6 +2087,9 @@ int main(int argc, char **argv)
 	/* If the terminal can do colors, tell ncurses to switch them on. */
 	if (has_colors())
 		start_color();
+
+	/* When requested, suppress the default spotlight and error colors. */
+	rescind_colors = (getenv("NO_COLOR") != NULL);
 #endif
 
 	/* Set up the function and shortcut lists.  This needs to be done
diff --git a/src/prototypes.h b/src/prototypes.h
index cf79680e..c43981af 100644
--- a/src/prototypes.h
+++ b/src/prototypes.h
@@ -141,6 +141,7 @@ extern char *alt_speller;
 extern syntaxtype *syntaxes;
 extern char *syntaxstr;
 extern bool have_palette;
+extern bool rescind_colors;
 #endif
 
 extern bool refresh_needed;
-- 
2.34.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.