fix(diff): use stringWidth for CJK-aware truncation in SplitDiff#1683
Closed
Bernardxu123 wants to merge 1 commit into
Closed
fix(diff): use stringWidth for CJK-aware truncation in SplitDiff#1683Bernardxu123 wants to merge 1 commit into
Bernardxu123 wants to merge 1 commit into
Conversation
The diff panel was using raw.length to calculate visual width for truncation, which undercounts CJK characters (each takes 2 terminal cells but raw.length counts it as 1). This caused incorrect border positions when diff lines contained Chinese characters. Fix by using stringWidth() from text-width.js which correctly handles CJK, emoji, and other wide characters. Fixes esengine#1671
3 tasks
esengine
added a commit
that referenced
this pull request
May 24, 2026
#1686) Two bugs in the same `Cell` body (#1671): - truncation used `raw.length > inner`, which undercounts wide chars — a CJK row that was visually too wide for the column slipped past the check and overflowed into the gutter - padding used `padEnd(inner)`, which counts string length not cells — short CJK content got padded as if each wide char were 1 cell, so the background-color stripe ran past the column edge Repo already has a cell-aware clipper (`clipToCells`) for the cut and a private `padToCells` in `markdown.tsx` for the pad. Promote `padToCells` to `text-width.ts` so SplitDiff can use both — collapses the original four lines (slice + ternary + padEnd + ascii comment) to a single `padToCells(clipToCells(raw, inner), inner)` and drops the duplicate definition from `markdown.tsx`. Supersedes #1683, which fixed the detection half but left the slice + padEnd in place — for CJK content the truncated string ended up wider than `inner` and short rows still over-padded. Co-authored-by: reasonix <reasonix@deepseek.com> Co-authored-by: Bernardxu123 <Bernardxu123@users.noreply.github.com>
Owner
|
Thanks for spotting the bug. Shipped the full fix as #1686 (merged) — used Closing in favor of #1686 because the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In review mode, the diff panel calculates wrong character width when CJK (Chinese/Japanese/Korean) characters are present. This causes incorrect border positions and visual misalignment.
Root Cause
In
src/cli/ui/SplitDiff.tsx, theCellfunction usesraw.lengthto calculate visual width for truncation:The problem is that
raw.lengthcounts JavaScript characters, not visual terminal cells. For CJK characters:raw.lengthcounts each character as 1This mismatch causes the truncation calculation to be wrong, resulting in incorrect border positions when diff lines contain Chinese characters.
Example
A diff line with Chinese characters like
函数返回值has:raw.length= 5 (5 JavaScript characters)When truncating to fit a column of width 8, the code thinks 5 < 8 so no truncation is needed, but the actual visual width is 10, which overflows.
Solution
Replace
raw.lengthwithstringWidth(raw)from the existingtext-width.jsutility:The
stringWidthfunction (from thestring-widthlibrary) correctly handles:Files Changed
src/cli/ui/SplitDiff.tsx— ImportstringWidthand use it for truncation calculationTesting
Impact
This fix ensures the diff panel renders correctly regardless of the characters in the diff content. Users working with CJK characters (Chinese, Japanese, Korean) will now see correct border positions and proper text truncation.
Fixes #1671