fix: exclude wide characters that would exceed endColumn#44
Closed
RyogaK wants to merge 1 commit intochalk:mainfrom
Closed
fix: exclude wide characters that would exceed endColumn#44RyogaK wants to merge 1 commit intochalk:mainfrom
RyogaK wants to merge 1 commit intochalk:mainfrom
Conversation
When endColumn falls in the middle of a wide character (CJK or emoji), the character is now excluded so that the result never exceeds the requested column range. Fixes #43
Author
|
Another idea: we could add an option to control the behavior when the boundary falls inside a wide character, e.g.: sliceAnsi('あいう', 0, 3); // 'あい' (default, current behavior)
sliceAnsi('あいう', 0, 3, { wideCharBoundary: 'exclude' }); // 'あ'This would provide a solution for downstream projects that have strict column-width constraints (like Happy to implement this approach if you prefer it. |
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
Fixes #43
When
endColumnfalls in the middle of a wide character (CJK or emoji),sliceAnsicurrently includes the character in full, causing the result to exceed the requested column range. This change excludes such characters so thatstringWidth(result) <= endColumn - startColumnis always guaranteed.Before
After
Implementation
A single condition added to the
isPastEndcheck in the main loop: a non-continuation character token whoseposition + visibleWidthexceedsendis treated as past end. The rest of the existing logic (closing active styles, etc.) works as-is.Discussion: wide emoji graphemes
This change also applies the same rule to wide emoji graphemes (e.g. regional-indicator flags
🇮🇱), updating the existing tests that previously expected "round up" behavior. I believe this is consistent — callers that specify a column range should be able to rely on the result not exceeding it — but I'd like to get your thoughts on whether wide emoji graphemes should be treated differently from CJK wide characters.Test plan
'あいう')