{"thread":{"id":"4872","subject":"[PATCH] diff: Support 256 colors","startedAt":"2006-07-13T16:08:06Z","lastAt":"2006-07-13T16:08:06Z","messageCount":1,"participants":["Timo Hirvonen"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"23767","messageId":"20060713190806.4700b24c.tihirvon@gmail.com","threadId":"4872","inReplyTo":null,"subject":"[PATCH] diff: Support 256 colors","fromName":"Timo Hirvonen","fromEmail":"tihirvon@gmail.com","sentAt":"2006-07-13T16:08:06Z","receivedAt":"2006-07-13T16:08:06Z","isPatch":true,"sender":{"key":"tihirvon@gmail.com","avatar":null},"body":"Add support for more than 8 colors.  Colors can be specified as numbers\n-1..255.  -1 is same as \"normal\".\n\nSigned-off-by: Timo Hirvonen <tihirvon@gmail.com>\n---\n diff.c |   24 ++++++++++++++++++------\n 1 files changed, 18 insertions(+), 6 deletions(-)\n\ndiff --git a/diff.c b/diff.c\nindex a9118a9..351cd07 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -26,8 +26,8 @@ enum color_diff {\n \tDIFF_FILE_NEW = 5,\n };\n \n-/* \"\\033[1;30;47m\\0\" is 11 bytes */\n-static char diff_colors[][16] = {\n+/* \"\\033[1;38;5;2xx;48;5;2xxm\\0\" is 23 bytes */\n+static char diff_colors[][24] = {\n \t\"\\033[m\",\t/* reset */\n \t\"\",\t\t/* normal */\n \t\"\\033[1m\",\t/* bold */\n@@ -57,12 +57,16 @@ static int parse_color(const char *name,\n \t\t\"normal\", \"black\", \"red\", \"green\", \"yellow\",\n \t\t\"blue\", \"magenta\", \"cyan\", \"white\"\n \t};\n+\tchar *end;\n \tint i;\n \tfor (i = 0; i < ARRAY_SIZE(color_names); i++) {\n \t\tconst char *str = color_names[i];\n \t\tif (!strncasecmp(name, str, len) && !str[len])\n \t\t\treturn i - 1;\n \t}\n+\ti = strtol(name, &end, 10);\n+\tif (*name && !*end && i >= -1 && i <= 255)\n+\t\treturn i;\n \treturn -2;\n }\n \n@@ -135,14 +139,22 @@ static void parse_diff_color_value(const\n \t\tif (fg >= 0) {\n \t\t\tif (sep++)\n \t\t\t\t*dst++ = ';';\n-\t\t\t*dst++ = '3';\n-\t\t\t*dst++ = '0' + fg;\n+\t\t\tif (fg < 8) {\n+\t\t\t\t*dst++ = '3';\n+\t\t\t\t*dst++ = '0' + fg;\n+\t\t\t} else {\n+\t\t\t\tdst += sprintf(dst, \"38;5;%d\", fg);\n+\t\t\t}\n \t\t}\n \t\tif (bg >= 0) {\n \t\t\tif (sep++)\n \t\t\t\t*dst++ = ';';\n-\t\t\t*dst++ = '4';\n-\t\t\t*dst++ = '0' + bg;\n+\t\t\tif (bg < 8) {\n+\t\t\t\t*dst++ = '4';\n+\t\t\t\t*dst++ = '0' + bg;\n+\t\t\t} else {\n+\t\t\t\tdst += sprintf(dst, \"48;5;%d\", bg);\n+\t\t\t}\n \t\t}\n \t\t*dst++ = 'm';\n \t}\n-- \n1.4.1.gd0c9d-dirty\n"}]}