From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Cc: Mark Lodato <lodatom@gmail.com>, Jeff King <peff@peff.net>
Subject: [PATCH] color: allow multiple attributes
Date: Sat, 27 Feb 2010 18:56:38 -0800 [thread overview]
Message-ID: <1267325798-8280-1-git-send-email-gitster@pobox.com> (raw)
In-Reply-To: <7vfx4mv0h9.fsf@alter.siamese.dyndns.org>
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>
---
Junio C Hamano <gitster@pobox.com> writes:
>>> but wouldn't it be more user friendly for us
>>> to support "red blink bold ul italic"?
>>
>> Yes, I think this should be done whether or not the patch in question
>> is accepted.
This time with a bit of test updates as well for real inclusion.
Also I realized that we can stuff them in an unsigned flag word as
bitfields ("red bold" and "red bold bold bold" would give the same
boldness anyway) to lift the artificial limit of number of attribute
words.
color.c | 47 +++++++++++++++++++++++++++++++++++++++--------
t/t4026-color.sh | 15 +++++++++++----
2 files changed, 50 insertions(+), 12 deletions(-)
diff --git a/color.c b/color.c
index db4dccf..17eb3ec 100644
--- a/color.c
+++ b/color.c
@@ -44,12 +44,23 @@ void color_parse(const char *value, const char *var, char *dst)
color_parse_mem(value, strlen(value), var, dst);
}
+static int count_bits(unsigned flag)
+{
+ int cnt = 0;
+ while (flag) {
+ if (flag & 01)
+ cnt++;
+ flag >>= 1;
+ }
+ return cnt;
+}
+
void color_parse_mem(const char *value, int value_len, const char *var,
char *dst)
{
const char *ptr = value;
int len = value_len;
- int attr = -1;
+ unsigned int attr = 0;
int fg = -2;
int bg = -2;
@@ -58,7 +69,7 @@ void color_parse_mem(const char *value, int value_len, const char *var,
return;
}
- /* [fg [bg]] [attr] */
+ /* [fg [bg]] [attr]... */
while (len > 0) {
const char *word = ptr;
int val, wordlen = 0;
@@ -87,19 +98,39 @@ void color_parse_mem(const char *value, int value_len, const char *var,
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;
+ int num_attrs = count_bits(attr);
+
+ if (COLOR_MAXLEN <=
+ /* Number of bytes to denote colors and attributes */
+ num_attrs
+ + (fg < 0 ? 0 : (fg < 8) ? 2 : 8) /* "3x" or "38;5;xxx" */
+ + (bg < 0 ? 0 : (bg < 8) ? 2 : 8) /* "4x" or "48;5;xxx" */
+ /* Number of semicolons between the above elements */
+ + (num_attrs + (0 <= fg) + (0 <= bg) - 1)
+ /* ESC '[', terminating 'm' and NUL */
+ + 4)
+ goto bad;
*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++)
diff --git a/t/t4026-color.sh b/t/t4026-color.sh
index b61e516..c3af190 100755
--- a/t/t4026-color.sh
+++ b/t/t4026-color.sh
@@ -8,14 +8,13 @@ test_description='Test diff/status color escape codes'
color()
{
- git config diff.color.new "$1" &&
- test "`git config --get-color diff.color.new`" = "^[$2"
+ actual=$(git config --get-color no.such.slot "$1") &&
+ test "$actual" = "^[$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' '
@@ -42,6 +41,14 @@ test_expect_success 'fg bg attr' '
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 'color specification too long' '
+ invalid_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"
'
--
1.7.0.270.g320aa
next prev parent reply other threads:[~2010-02-28 2:56 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-02-27 4:57 [PATCH 0/5] color enhancements, particularly for grep Mark Lodato
2010-02-27 4:57 ` [PATCH 1/5] Allow explicit ANSI codes for colors Mark Lodato
2010-02-27 8:51 ` Jeff King
2010-02-27 18:24 ` Mark Lodato
2010-02-27 21:21 ` Junio C Hamano
2010-02-28 2:56 ` Junio C Hamano [this message]
2010-02-28 12:20 ` [PATCH] color: allow multiple attributes Jeff King
2010-02-28 18:16 ` Junio C Hamano
2010-02-28 18:33 ` Jeff King
2010-02-27 4:57 ` [PATCH 2/5] Add GIT_COLOR_BOLD_* and GIT_COLOR_BG_* Mark Lodato
2010-02-27 4:57 ` [PATCH 3/5] Remove reference to GREP_COLORS from documentation Mark Lodato
2010-02-27 4:57 ` [PATCH 4/5] grep: Colorize filename, line number, and separator Mark Lodato
2010-02-27 11:43 ` René Scharfe
2010-02-28 20:14 ` Mark Lodato
2010-02-28 22:26 ` Michael Witten
2010-03-02 1:49 ` Mark Lodato
2010-03-02 6:43 ` Michael Witten
2010-03-03 4:26 ` Mark Lodato
2010-03-03 4:49 ` Miles Bader
2010-02-27 11:53 ` René Scharfe
2010-02-27 17:08 ` Junio C Hamano
2010-02-28 20:15 ` Mark Lodato
2010-02-28 19:29 ` Junio C Hamano
2010-02-28 20:39 ` Mark Lodato
2010-02-27 4:57 ` [PATCH 5/5] grep: Colorize selected, context, and function lines Mark Lodato
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1267325798-8280-1-git-send-email-gitster@pobox.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=lodatom@gmail.com \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).