summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: c503467)
raw | patch | inline | side by side (parent: c503467)
author | Junio C Hamano <gitster@pobox.com> | |
Sun, 28 Feb 2010 02:56:38 +0000 (18:56 -0800) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Sun, 7 Mar 2010 20:00:36 +0000 (12:00 -0800) |
In configuration files (and "git config --color" command line), we
supported one and only one attribute after foreground and background
color. Accept combinations of attributes, e.g.
[diff.color]
old = red reverse bold
Signed-off-by: Junio C Hamano <gitster@pobox.com>
supported one and only one attribute after foreground and background
color. Accept combinations of attributes, e.g.
[diff.color]
old = red reverse bold
Signed-off-by: Junio C Hamano <gitster@pobox.com>
color.c | patch | blob | history | |
color.h | patch | blob | history | |
t/t4026-color.sh | patch | blob | history |
index db4dccfb77d80c9bd8981719fe2d0dc17c6772b6..90268fb3c63d340e615f8c54af009e347a654e8b 100644 (file)
--- a/color.c
+++ b/color.c
{
const char *ptr = value;
int len = value_len;
- int attr = -1;
+ unsigned int attr = 0;
int fg = -2;
int bg = -2;
return;
}
- /* [fg [bg]] [attr] */
+ /* [fg [bg]] [attr]... */
while (len > 0) {
const char *word = ptr;
int val, wordlen = 0;
goto bad;
}
val = parse_attr(word, wordlen);
- if (val < 0 || attr != -1)
+ if (0 <= val)
+ attr |= (1 << val);
+ else
goto bad;
- attr = val;
}
- if (attr >= 0 || fg >= 0 || bg >= 0) {
+ if (attr || fg >= 0 || bg >= 0) {
int sep = 0;
+ int i;
*dst++ = '\033';
*dst++ = '[';
- if (attr >= 0) {
- *dst++ = '0' + attr;
- sep++;
+
+ for (i = 0; attr; i++) {
+ unsigned bit = (1 << i);
+ if (!(attr & bit))
+ continue;
+ attr &= ~bit;
+ if (sep++)
+ *dst++ = ';';
+ *dst++ = '0' + i;
}
if (fg >= 0) {
if (sep++)
index 5019df82f79f1888b3aa57b9752a6a55b13f475a..e2988f4e93f95ece2a1b0721fa8a17aa3f030756 100644 (file)
--- a/color.h
+++ b/color.h
#ifndef COLOR_H
#define COLOR_H
-/* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
-#define COLOR_MAXLEN 24
+/* 2 + (2 * num_attrs) + 8 + 1 + 8 + 'm' + NUL */
+/* "\033[1;2;4;5;7;38;5;2xx;48;5;2xxm\0" */
+/*
+ * The maximum length of ANSI color sequence we would generate:
+ * - leading ESC '[' 2
+ * - attr + ';' 2 * 8 (e.g. "1;")
+ * - fg color + ';' 9 (e.g. "38;5;2xx;")
+ * - fg color + ';' 9 (e.g. "48;5;2xx;")
+ * - terminating 'm' NUL 2
+ *
+ * The above overcounts attr (we only use 5 not 8) and one semicolon
+ * but it is close enough.
+ */
+#define COLOR_MAXLEN 40
/*
* This variable stores the value of color.ui
diff --git a/t/t4026-color.sh b/t/t4026-color.sh
index b61e5169f4e9e8d9f87b9ea16e71dfcf1fb9f340..49eced20e7f9346ac6d52891c271ab3ef6d0982b 100755 (executable)
--- a/t/t4026-color.sh
+++ b/t/t4026-color.sh
color()
{
- git config diff.color.new "$1" &&
- test "`git config --get-color diff.color.new`" = "\e$2"
+ actual=$(git config --get-color no.such.slot "$1") &&
+ test "$actual" = "\e$2"
}
invalid_color()
{
- git config diff.color.new "$1" &&
- test -z "`git config --get-color diff.color.new 2>/dev/null`"
+ test_must_fail git config --get-color no.such.slot "$1"
}
test_expect_success 'reset' '
color "blue red ul" "[4;34;41m"
'
+test_expect_success 'fg bg attr...' '
+ color "blue bold dim ul blink reverse" "[1;2;4;5;7;34m"
+'
+
+test_expect_success 'long color specification' '
+ color "254 255 bold dim ul blink reverse" "[1;2;4;5;7;38;5;254;48;5;255m"
+'
+
test_expect_success '256 colors' '
color "254 bold 255" "[1;38;5;254;48;5;255m"
'