From 36b7b433f9344fe79d42006460223277316d2f8e Mon Sep 17 00:00:00 2001
From: Arun Prakash Jana <engineerarun@gmail.com>
Date: Mon, 3 Dec 2018 21:44:33 +0530
Subject: [PATCH] Per-context directory color

---
 README.md | 12 +++++++++--
 nnn.1     | 14 +++++++++----
 src/nnn.c | 62 ++++++++++++++++++++++++++++++++++++++++---------------
 3 files changed, 65 insertions(+), 23 deletions(-)

diff --git a/README.md b/README.md
index 110430e1..ec70a83f 100644
--- a/README.md
+++ b/README.md
@@ -50,6 +50,7 @@ It runs on Linux, macOS, Raspberry Pi, Cygwin, Linux subsystem for Windows and T
   - [Keyboard shortcuts](#keyboard-shortcuts)
   - [Leader key](#leader-key)
   - [Contexts](#contexts)
+  - [Directory color](#directory-color)
   - [Filters](#filters)
   - [Navigate-as-you-type mode](#navigate-as-you-type-mode)
   - [File indicators](#file-indicators)
@@ -183,7 +184,7 @@ Search keyword and option completion scripts for Bash, Fish and Zsh can be found
 #### Cmdline options
 
 ```
-usage: nnn [-b key] [-c N] [-e] [-i] [-l]
+usage: nnn [-b key] [-C] [-e] [-i] [-l]
            [-p file] [-S] [-v] [-h] [PATH]
 
 The missing terminal file manager for X.
@@ -193,7 +194,7 @@ positional args:
 
 optional args:
  -b key  bookmark key to open
- -c N    dir color, disables if N>7
+ -C      disable directory color
  -e      use exiftool instead of mediainfo
  -i      start in navigate-as-you-type mode
  -l      start in light mode
@@ -273,6 +274,13 @@ The first time a context is entered, it copies the state of the last visited con
 
 When a context is quit, the next active context is selected. If the last active context is quit, the program quits.
 
+#### Directory color
+
+Each context can have its own color for directories specified:
+
+    export NNN_CONTEXT_COLORS="1234"
+colors: 0-black, 1-red, 2-green, 3-yellow, 4-blue (default), 5-magenta, 6-cyan, 7-white
+
 #### Filters
 
 Filters support regexes to instantly (search-as-you-type) list the matching entries in the current directory.
diff --git a/nnn.1 b/nnn.1
index 98f9c6e3..51ce9e40 100644
--- a/nnn.1
+++ b/nnn.1
@@ -7,7 +7,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Ar -b key
-.Op Ar -c N
+.Op Ar -C
 .Op Ar -e
 .Op Ar -i
 .Op Ar -l
@@ -166,9 +166,8 @@ supports the following options:
 .Fl "b key"
         specify bookmark key to open
 .Pp
-.Fl "c N"
-        specify dir color (default blue), disables if N>7
-        0-black, 1-red, 2-green, 3-yellow, 4-blue, 5-magenta, 6-cyan, 7-white
+.Fl C
+        disable directory color
 .Pp
 .Fl e
         use exiftool instead of mediainfo
@@ -282,6 +281,13 @@ files.
     export NNN_USE_EDITOR=1
 .Ed
 .Pp
+\fBNNN_CONTEXT_COLORS:\fR string of color codes for each context, e.g.:
+.Bd -literal
+    export NNN_CONTEXT_COLORS="1234"
+
+    codes: 0-black, 1-red, 2-green, 3-yellow, 4-blue (default), 5-magenta, 6-cyan, 7-white
+.Ed
+.Pp
 \fBNNN_IDLE_TIMEOUT:\fR set idle timeout (in seconds) to invoke terminal locker.
 .Pp
 \fBNNN_COPIER:\fR set to a clipboard copier script.
diff --git a/src/nnn.c b/src/nnn.c
index 6c360d8d..429c1588 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -180,7 +180,7 @@ disabledbg()
 #define SYMLINK_TO_DIR 0x1
 #define MAX_HOME_LEN 64
 #define MAX_CTX 4
-#define DOT_FILTER_LEN 8
+#define DOT_FILTER_LEN 7
 
 /* Macros to define process spawn behaviour as flags */
 #define F_NONE     0x00  /* no flag set */
@@ -258,9 +258,8 @@ typedef struct {
 	uint showcolor  : 1;  /* Set to show dirs in blue */
 	uint dircolor   : 1;  /* Current status of dir color */
 	uint metaviewer : 1;  /* Index of metadata viewer in utils[] */
-	uint color      : 3;  /* Color code for directories */
 	uint ctxactive  : 1;  /* Context active or not */
-	uint reserved   : 10;
+	uint reserved   : 13;
 	/* The following settings are global */
 	uint curctx     : 2;  /* Current context number */
 	uint picker     : 1;  /* Write selection to user-specified file */
@@ -277,12 +276,13 @@ typedef struct {
 	char c_name[NAME_MAX + 1]; /* Current file name */
 	settings c_cfg; /* Current configuration */
 	char c_fltr[DOT_FILTER_LEN]; /* Hidden filter */
+	char color; /* Color code for directories */
 } context;
 
 /* GLOBALS */
 
 /* Configuration, contexts */
-static settings cfg = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 4, 1, 0, 0, 0, 0, 0, 0};
+static settings cfg = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0};
 static context g_ctx[MAX_CTX] __attribute__ ((aligned));
 
 static struct entry *dents;
@@ -798,8 +798,12 @@ static void initcurses(void)
 	curs_set(FALSE); /* Hide cursor */
 	start_color();
 	use_default_colors();
-	if (cfg.showcolor)
-		init_pair(1, cfg.color, -1);
+	if (cfg.showcolor) {
+		init_pair(1, g_ctx[0].color, -1);
+		init_pair(2, g_ctx[1].color, -1);
+		init_pair(3, g_ctx[2].color, -1);
+		init_pair(4, g_ctx[3].color, -1);
+	}
 	settimeout(); /* One second */
 }
 
@@ -1518,7 +1522,7 @@ static char *get_bm_loc(int key, char *buf)
 static void resetdircolor(mode_t mode)
 {
 	if (cfg.dircolor && !S_ISDIR(mode)) {
-		attroff(COLOR_PAIR(1) | A_BOLD);
+		attroff(COLOR_PAIR(cfg.curctx + 1) | A_BOLD);
 		cfg.dircolor = 0;
 	}
 }
@@ -2103,6 +2107,8 @@ static int show_help(char *path)
 
 	if (cfg.useeditor)
 		dprintf(fd, "NNN_USE_EDITOR: 1\n");
+	if (getenv("NNN_CONTEXT_COLORS"))
+		dprintf(fd, "NNN_CONTEXT_COLORS: %s\n", getenv("NNN_CONTEXT_COLORS"));
 	if (idletimeout)
 		dprintf(fd, "NNN_IDLE_TIMEOUT: %d secs\n", idletimeout);
 	if (copier)
@@ -2478,7 +2484,7 @@ static void redraw(char *path)
 		ncols -= 5;
 
 	if (cfg.showcolor) {
-		attron(COLOR_PAIR(1) | A_BOLD);
+		attron(COLOR_PAIR(cfg.curctx + 1) | A_BOLD);
 		cfg.dircolor = 1;
 	}
 
@@ -2500,7 +2506,7 @@ static void redraw(char *path)
 
 	/* Must reset e.g. no files in dir */
 	if (cfg.dircolor) {
-		attroff(COLOR_PAIR(1) | A_BOLD);
+		attroff(COLOR_PAIR(cfg.curctx + 1) | A_BOLD);
 		cfg.dircolor = 0;
 	}
 
@@ -3511,14 +3517,14 @@ nochange:
 static void usage(void)
 {
 	fprintf(stdout,
-		"usage: nnn [-b key] [-c N] [-e] [-i] [-l]\n"
+		"usage: nnn [-b key] [-C] [-e] [-i] [-l]\n"
 		"           [-p file] [-S] [-v] [-h] [PATH]\n\n"
 		"The missing terminal file manager for X.\n\n"
 		"positional args:\n"
 		"  PATH   start dir [default: current dir]\n\n"
 		"optional args:\n"
 		" -b key  bookmark key to open\n"
-		" -c N    dir color, disables if N>7\n"
+		" -C      disable directory color\n"
 		" -e      use exiftool instead of mediainfo\n"
 		" -i      start in navigate-as-you-type mode\n"
 		" -l      start in light mode\n"
@@ -3541,7 +3547,7 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
-	while ((opt = getopt(argc, argv, "Slib:c:ep:vh")) != -1) {
+	while ((opt = getopt(argc, argv, "Slib:Cep:vh")) != -1) {
 		switch (opt) {
 		case 'S':
 			cfg.blkorder = 1;
@@ -3558,11 +3564,8 @@ int main(int argc, char *argv[])
 		case 'b':
 			ipath = optarg;
 			break;
-		case 'c':
-			if (atoi(optarg) > 7)
-				cfg.showcolor = 0;
-			else
-				cfg.color = (uchar)atoi(optarg);
+		case 'C':
+			cfg.showcolor = 0;
 			break;
 		case 'e':
 			cfg.metaviewer = EXIFTOOL;
@@ -3592,6 +3595,31 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	/* Get the context colors; copier used as tmp var */
+	if (cfg.showcolor) {
+		copier = getenv("NNN_CONTEXT_COLORS");
+		if (copier) {
+			opt = 0;
+			while (*copier && opt < MAX_CTX) {
+				if (*copier < '0' || *copier > '7') {
+					fprintf(stderr, "invalid color code\n");
+					exit(1);
+				} else
+					g_ctx[opt].color = *copier - '0';
+
+				++copier;
+				++opt;
+			}
+
+			while (opt != MAX_CTX) {
+				g_ctx[opt].color = 4;
+				++opt;
+			}
+		} else
+			for (opt = 0; opt < MAX_CTX; ++opt)
+				g_ctx[opt].color = 4; /* Default color is blue */
+	}
+
 	/* Parse bookmarks string */
 	 if (parsebmstr() < 0) {
 		fprintf(stderr, "ERROR parsing NNN_BMS: set single-char bookmark keys only\n");
-- 
2.51.0