Skip to content

Commit 1a9df8d

Browse files
LorenzoPegorarigitster
authored andcommitted
diff: handle ANSI escape codes in prefix when calculating diffstat width
The diffstat width is calculated by taking the terminal width and incorrectly subtracting the `strlen()` of `line_prefix`, instead of the actual display width of `line_prefix`, which may contain ANSI escape codes (e.g., ANSI-colored strings in `log --graph --stat`). Utilize the display width instead, obtained via `utf8_strnwidth()` with the flag `skip_ansi`. Signed-off-by: LorenzoPegorari <lorenzo.pegorari2002@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 9a2fb14 commit 1a9df8d

1 file changed

Lines changed: 4 additions & 8 deletions

File tree

diff.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,9 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
27132713
count = i; /* where we can stop scanning in data->files[] */
27142714

27152715
/*
2716-
* We have width = stat_width or term_columns() columns total.
2716+
* We have width = stat_width or term_columns() columns total minus the
2717+
* length of line_prefix skipping ANSI escape codes to get the display
2718+
* width (e.g., skip ANSI-colored strings in "log --graph --stat").
27172719
* We want a maximum of min(max_len, stat_name_width) for the name part.
27182720
* We want a maximum of min(max_change, stat_graph_width) for the +- part.
27192721
* We also need 1 for " " and 4 + decimal_width(max_change)
@@ -2740,14 +2742,8 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
27402742
* separators and this message, this message will "overflow"
27412743
* making the line longer than the maximum width.
27422744
*/
2743-
2744-
/*
2745-
* NEEDSWORK: line_prefix is often used for "log --graph" output
2746-
* and contains ANSI-colored string. utf8_strnwidth() should be
2747-
* used to correctly count the display width instead of strlen().
2748-
*/
27492745
if (options->stat_width == -1)
2750-
width = term_columns() - strlen(line_prefix);
2746+
width = term_columns() - utf8_strnwidth(line_prefix, strlen(line_prefix), 1);
27512747
else
27522748
width = options->stat_width ? options->stat_width : 80;
27532749
number_width = decimal_width(max_change) > number_width ?

0 commit comments

Comments
 (0)