summaryrefslogtreecommitdiff
path: root/color.c
diff options
context:
space:
mode:
authorRobert Estelle <robertestelle@gmail.com>2021-10-26 01:03:47 (GMT)
committerJunio C Hamano <gitster@pobox.com>2021-10-28 16:37:18 (GMT)
commitde658515ae1166577441da09fe7624769e263a3e (patch)
treebd019e9daccc7e3f6340bccd4b3788434053ca80 /color.c
parent05f1f41c9b02b916a5f03c5658bec3270ac3684d (diff)
downloadgit-de658515ae1166577441da09fe7624769e263a3e.zip
git-de658515ae1166577441da09fe7624769e263a3e.tar.gz
git-de658515ae1166577441da09fe7624769e263a3e.tar.bz2
color: allow colors to be prefixed with "reset"
"reset" was previously treated as a standalone special color name representing `\e[m`. Now, it can apply to other color properties, allowing exact specifications without implicit attribute inheritance. For example, "reset green" now renders `\e[;32m`, which is interpreted as "reset everything; then set foreground to green". This means the background and other attributes are also reset to their defaults. Previously, this was impossible to represent in a single color: "reset" could be specified alone, or a color with attributes, but some thing like clearing a background color were impossible. There is a separate change that introduces the "default" color name to assist with that, but even then, the above could only to be represented by explicitly disabling each of the attributes: green default no-bold no-dim no-italic no-ul no-blink no-reverse no-strike Signed-off-by: Robert Estelle <robertestelle@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'color.c')
-rw-r--r--color.c18
1 files changed, 11 insertions, 7 deletions
diff --git a/color.c b/color.c
index a5fa9b7..4f884c6 100644
--- a/color.c
+++ b/color.c
@@ -255,6 +255,7 @@ int color_parse_mem(const char *value, int value_len, char *dst)
const char *ptr = value;
int len = value_len;
char *end = dst + COLOR_MAXLEN;
+ unsigned int has_reset = 0;
unsigned int attr = 0;
struct color fg = { COLOR_UNSPECIFIED };
struct color bg = { COLOR_UNSPECIFIED };
@@ -269,12 +270,7 @@ int color_parse_mem(const char *value, int value_len, char *dst)
return 0;
}
- if (!strncasecmp(ptr, "reset", len)) {
- xsnprintf(dst, end - dst, GIT_COLOR_RESET);
- return 0;
- }
-
- /* [fg [bg]] [attr]... */
+ /* [reset] [fg [bg]] [attr]... */
while (len > 0) {
const char *word = ptr;
struct color c = { COLOR_UNSPECIFIED };
@@ -291,6 +287,11 @@ int color_parse_mem(const char *value, int value_len, char *dst)
len--;
}
+ if (match_word(word, wordlen, "reset")) {
+ has_reset = 1;
+ continue;
+ }
+
if (!parse_color(&c, word, wordlen)) {
if (fg.type == COLOR_UNSPECIFIED) {
fg = c;
@@ -316,13 +317,16 @@ int color_parse_mem(const char *value, int value_len, char *dst)
*dst++ = (x); \
} while(0)
- if (attr || !color_empty(&fg) || !color_empty(&bg)) {
+ if (has_reset || attr || !color_empty(&fg) || !color_empty(&bg)) {
int sep = 0;
int i;
OUT('\033');
OUT('[');
+ if (has_reset)
+ sep++;
+
for (i = 0; attr; i++) {
unsigned bit = (1 << i);
if (!(attr & bit))