fix(ansi): treat wide characters as breakable in Wrap and Wordwrap#827
Open
tzengyuxio wants to merge 2 commits into
Open
fix(ansi): treat wide characters as breakable in Wrap and Wordwrap#827tzengyuxio wants to merge 2 commits into
tzengyuxio wants to merge 2 commits into
Conversation
Wide characters (CJK ideographs, fullwidth chars) can be broken between any two characters per Unicode line breaking rules (UAX charmbracelet#14). Previously, consecutive wide characters were accumulated into the word buffer and treated as a single long "word", causing suboptimal wrapping: - Mixed CJK/Latin text like "vault 不只是記事本" would push the entire CJK sequence to the next line instead of filling the current line - CJK text only got hard-wrapped when the word exceeded the limit, wasting space at the end of lines Now wide characters (width > 1) are written directly to the output buffer, allowing line breaks between any two wide characters. This produces more natural wrapping for CJK text while preserving word boundaries for narrow (Latin) characters.
This was referenced Apr 9, 2026
…ping Check whether the wide character fits on the current line (including pending space width) before flushing the space buffer. This prevents spaces from being written to the line end when the next character triggers a line break.
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.
Summary
Wrap()andWordwrap()functionsProblem
Previously, consecutive wide characters were accumulated into the word buffer and treated as a single long "word". This caused suboptimal wrapping for CJK text:
Fix
When a wide character (width > 1) is encountered, flush the current word buffer and write the character directly to output. This allows line breaks between any two wide characters, producing more natural wrapping for CJK text while preserving word boundaries for narrow (Latin) characters.
This fix benefits all consumers including
glamour(vialipgloss.Wrap) andglow.Related: charmbracelet/glamour#185
Test plan