git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[GSoC PATCH v2 1/2] diff: handle UTF-8 chars in prefix when calculating diffstat width

From
LorenzoPegorari <lorenzo.pegorari2002@gmail.com>
Date
Feb 27, 2026, 16:04 UTC
Message-ID
<9e8161a7009e270c76a7cc4411b1977837ee7d19.1772136203.git.lorenzo.pegorari2002@gmail.com>
In-Reply-To
<cover.1772136203.git.lorenzo.pegorari2002@gmail.com>

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 UTF-8 characters (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>
---
 diff.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/diff.c b/diff.c
index 35b903a9a0..395cb464f4 100644
--- a/diff.c
+++ b/diff.c
@@ -2749,7 +2749,9 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
 	count = i; /* where we can stop scanning in data->files[] */
 
 	/*
-	 * We have width = stat_width or term_columns() columns total.
+	 * We have width = stat_width or term_columns() columns total minus the
+	 * length of line_prefix skipping UTF-8 chars to get the display width
+	 * (e.g., to skip ANSI-colored strings in "log --graph --stat").
 	 * We want a maximum of min(max_len, stat_name_width) for the name part.
 	 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
 	 * We also need 1 for " " and 4 + decimal_width(max_change)
@@ -2776,14 +2778,8 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
 	 * separators and this message, this message will "overflow"
 	 * making the line longer than the maximum width.
 	 */
-
-	/*
-	 * NEEDSWORK: line_prefix is often used for "log --graph" output
-	 * and contains ANSI-colored string.  utf8_strnwidth() should be
-	 * used to correctly count the display width instead of strlen().
-	 */
 	if (options->stat_width == -1)
-		width = term_columns() - strlen(line_prefix);
+		width = term_columns() - utf8_strnwidth(line_prefix, strlen(line_prefix), 1);
 	else
 		width = options->stat_width ? options->stat_width : 80;
 	number_width = decimal_width(max_change) > number_width ?
-- 
2.43.0
Previous: LorenzoPegorariNext: LorenzoPegorari
Message 8 of 14 in “diff: handle ANSI chars in prefix when calculating diffstat width”
  1. 0/2 diff: handle ANSI chars in prefix when calculating diffstat widthLorenzoPegorari, Feb 24, 2026
  2. 1/2 diff: handle ANSI chars in prefix when calculating diffstat widthLorenzoPegorari, Feb 24, 2026
  3. 2/2 t4074: add test for diffstat width when prefix contains ANSI charsLorenzoPegorari, Feb 24, 2026
  4. Junio C HamanoFeb 24, 2026
  5. Lorenzo PegorariFeb 25, 2026
  6. Junio C HamanoFeb 24, 2026
  7. 0/2 diff: handle UTF-8 chars in prefix when calculating diffstat widthLorenzoPegorari, Feb 27, 2026
  8. 1/2 diff: handle UTF-8 chars in prefix when calculating diffstat widthLorenzoPegorari, Feb 27, 2026
  9. 2/2 t4052: add test for diffstat width when prefix contains UTF-8 charsLorenzoPegorari, Feb 27, 2026
  10. Junio C HamanoFeb 27, 2026
  11. Junio C HamanoFeb 27, 2026
  12. 0/2 diff: handle ANSI escape codes in prefix when calculating diffstat widthLorenzoPegorari, Feb 27, 2026
  13. 1/2 diff: handle ANSI escape codes in prefix when calculating diffstat widthLorenzoPegorari, Feb 27, 2026
  14. 2/2 t4052: test for diffstat width when prefix contains ANSI escape codesLorenzoPegorari, Feb 27, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.