decorate: color each token separately

Wrap "tag:" prefixes and the arrows in "HEAD -> branch" decorations in
their own color sequences. Otherwise, if --graph is used, tag names or
arrows can end up uncolored when %w width formatting breaks a line just
before them. This is because --graph resets the color after doing its
drawing at the start of a line.

Amend test t4207-log-decoration-colors.sh accordingly.

Signed-off-by: Andy Koppe <andy.koppe@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Andy Koppe
2023-08-20 19:50:06 +01:00
committed by Junio C Hamano
parent b87a9a2c1e
commit dcb347f837
2 changed files with 36 additions and 22 deletions

View File

@ -342,26 +342,34 @@ void format_decorations(struct strbuf *sb,
* appeared, skipping the entry for current.
*/
if (decoration != current_and_HEAD) {
const char *color =
decorate_get_color(use_color, decoration->type);
if (*prefix) {
strbuf_addstr(sb, color_commit);
strbuf_addstr(sb, prefix);
strbuf_addstr(sb, color_reset);
}
strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
if (decoration->type == DECORATION_REF_TAG)
if (decoration->type == DECORATION_REF_TAG) {
strbuf_addstr(sb, color);
strbuf_addstr(sb, "tag: ");
strbuf_addstr(sb, color_reset);
}
strbuf_addstr(sb, color);
show_name(sb, decoration);
strbuf_addstr(sb, color_reset);
if (current_and_HEAD &&
decoration->type == DECORATION_REF_HEAD) {
strbuf_addstr(sb, color);
strbuf_addstr(sb, " -> ");
strbuf_addstr(sb, color_reset);
strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
show_name(sb, current_and_HEAD);
strbuf_addstr(sb, color_reset);
}
strbuf_addstr(sb, color_reset);
prefix = separator;
}