Skip to content

Commit 1fdffa6

Browse files
committed
Merge branch 'rs/color-escape-has-zero-width'
* rs/color-escape-has-zero-width: strbuf_add_wrapped_text(): skip over colour codes
2 parents d2cd665 + 8a3c63e commit 1fdffa6

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

Documentation/pretty-formats.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ The placeholders are:
136136
- '%n': newline
137137
- '%x00': print a byte from a hex code
138138
- '%w([<w>[,<i1>[,<i2>]]])': switch line wrapping, like the -w option of
139-
linkgit:git-shortlog[1]. NOTE: Color placeholders (`%C*`) are not
140-
recognized as having no width, so they should not be put into wrapped
141-
sections.
139+
linkgit:git-shortlog[1].
142140

143141
NOTE: Some placeholders may depend on other options given to the
144142
revision traversal engine. For example, the `%g*` reflog options will

utf8.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,20 @@ static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
314314
}
315315
}
316316

317+
static size_t display_mode_esc_sequence_len(const char *s)
318+
{
319+
const char *p = s;
320+
if (*p++ != '\033')
321+
return 0;
322+
if (*p++ != '[')
323+
return 0;
324+
while (isdigit(*p) || *p == ';')
325+
p++;
326+
if (*p++ != 'm')
327+
return 0;
328+
return p - s;
329+
}
330+
317331
/*
318332
* Wrap the text, if necessary. The variable indent is the indent for the
319333
* first line, indent2 is the indent for all other lines.
@@ -337,7 +351,13 @@ int strbuf_add_wrapped_text(struct strbuf *buf,
337351
}
338352

339353
for (;;) {
340-
char c = *text;
354+
char c;
355+
size_t skip;
356+
357+
while ((skip = display_mode_esc_sequence_len(text)))
358+
text += skip;
359+
360+
c = *text;
341361
if (!c || isspace(c)) {
342362
if (w < width || !space) {
343363
const char *start = bol;

0 commit comments

Comments
 (0)