strbuf_add_wrapped_text(): skip over colour codes
Ignore display mode escape sequences (colour codes) for the purpose of text wrapping because they don't have a visible width. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Nov 23, 2009 at 23:40 UTC
8a3c63e01d2b1df471beafff5fb78df4bade9388
2 files changed
+22
-4
Documentation/pretty-formats.txt
+1
-3
@@ -136,9 +136,7 @@ The placeholders are:
136
- '%n': newline
137
- '%x00': print a byte from a hex code
138
- '%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].
140
141
NOTE: Some placeholders may depend on other options given to the
142
revision traversal engine. For example, the `%g*` reflog options will
utf8.c
+21
-1
@@ -314,6 +314,20 @@ static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
314
}
315
}
316
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
+
331
/*
332
* Wrap the text, if necessary. The variable indent is the indent for the
333
* first line, indent2 is the indent for all other lines.
@@ -337,7 +351,13 @@ int strbuf_add_wrapped_text(struct strbuf *buf,
351
}
352
353
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;
361
if (!c || isspace(c)) {
362
if (w < width || !space) {
363
const char *start = bol;