Commits

Anonymous committed f5b571f

diff: Support 256 colors

Add support for more than 8 colors. Colors can be specified as numbers
-1..255. -1 is same as "normal".

Signed-off-by: Timo Hirvonen <tihirvon@gmail.com>
Signed-off-by: Junio C Hamano <junkio@cox.net>

  • Participants
  • Parent commits f37399e

Comments (0)

Files changed (1)

 	DIFF_FILE_NEW = 5,
 };
 
-/* "\033[1;30;47m\0" is 11 bytes */
-static char diff_colors[][16] = {
+/* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
+static char diff_colors[][24] = {
 	"\033[m",	/* reset */
 	"",		/* normal */
 	"\033[1m",	/* bold */
 		"normal", "black", "red", "green", "yellow",
 		"blue", "magenta", "cyan", "white"
 	};
+	char *end;
 	int i;
 	for (i = 0; i < ARRAY_SIZE(color_names); i++) {
 		const char *str = color_names[i];
 		if (!strncasecmp(name, str, len) && !str[len])
 			return i - 1;
 	}
+	i = strtol(name, &end, 10);
+	if (*name && !*end && i >= -1 && i <= 255)
+		return i;
 	return -2;
 }
 
 		if (fg >= 0) {
 			if (sep++)
 				*dst++ = ';';
-			*dst++ = '3';
-			*dst++ = '0' + fg;
+			if (fg < 8) {
+				*dst++ = '3';
+				*dst++ = '0' + fg;
+			} else {
+				dst += sprintf(dst, "38;5;%d", fg);
+			}
 		}
 		if (bg >= 0) {
 			if (sep++)
 				*dst++ = ';';
-			*dst++ = '4';
-			*dst++ = '0' + bg;
+			if (bg < 8) {
+				*dst++ = '4';
+				*dst++ = '0' + bg;
+			} else {
+				dst += sprintf(dst, "48;5;%d", bg);
+			}
 		}
 		*dst++ = 'm';
 	}