fix(tui): trim trailing whitespace in wrapped lines to prevent width overflow#457
Closed
robinwander wants to merge 1 commit intobadlogic:mainfrom
Closed
fix(tui): trim trailing whitespace in wrapped lines to prevent width overflow#457robinwander wants to merge 1 commit intobadlogic:mainfrom
robinwander wants to merge 1 commit intobadlogic:mainfrom
Conversation
robinwander
commented
Jan 5, 2026
| assert.ok(wrapped[1].includes("https://")); | ||
| }); | ||
|
|
||
| it("should not have whitespace before underline reset code", () => { |
Contributor
Author
There was a problem hiding this comment.
i can remove this test if you think its too much, but i initially refactored to move all trimming of lines to the end, but this causes a bug due to the underline reset, and thought it could be nice to have this covered with a test
robinwander
commented
Jan 5, 2026
|
|
||
| return wrapped.length > 0 ? wrapped : [""]; | ||
| // Trailing whitespace can cause lines to exceed the requested width | ||
| return wrapped.length > 0 ? wrapped.map((line) => line.trimEnd()) : [""]; |
Contributor
Author
There was a problem hiding this comment.
Opus and I couldn't really think of a scenario where it would be meaningful to try to preserve as much white space as could fit in the requested width, so just moved to trimming all lines but I definitely could be missing something here.
robinwander
commented
Jan 5, 2026
| } | ||
| }); | ||
|
|
||
| it("should truncate trailing whitespace that exceeds width", () => { |
Contributor
Author
There was a problem hiding this comment.
verified this test case breaks without the fix.
Owner
|
Ouch, i'll take a looksy tomorrow! |
Owner
|
Merged into main with changelog entry. Thanks for the fix! |
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.
Bug
When you paste text with content that triggers
Markdown.render(``` ````, >, ``) (bold and italics seem fine though) which also has trailing whitespace that exceeds the width of the terminal, it causes pi to crash with the following error:This happened to me a lot when copy-pasting from another terminal (so it had a lot of trailing white space on every line as well as markdown content).
Root cause / fix
It took me awhile to figure out why I could only repro sometimes even though in this code it seemed liked the bug was clear. But we are actually protected in most scenarios due to
Editor.wordWrapLinetrimming.And for non whitespace characters we are protected by
breakLongWord.So this bug only happens for specific cases when you paste with content that goes through
Markdown.renderand has white space beyond the width of terminal you are pasting into.The wrapping logic in
wrapTextWithAnsiintentionally skips breaking whitespace-only tokens to avoid generating lines of individual spaces. But this means if you paste something like 120 spaces into a 100-character-wide terminal, the line passes through unchanged and exceeds the width limit.It seems like a safe enough fix is to
trimEnd()all wrapped lines before returning, but I could be missing something here. But it looks like trailing whitespace doesn't need to be preserved since padding is added later by the rendering layer. There was already atrimEnd()call at one wrap point in the code, so this just consolidates the behavior to catch all cases.Testing
Added two tests: one for a minimal repro of the original crash case, and one to guard the existing behavior where trailing whitespace must be trimmed before adding ANSI underline reset codes (otherwise the reset code ends up after the whitespace instead of immediately after the content).
I also manually repro'ed with
, >, and '' and confirmed they were fixed with this change by running pi built from this branch's source.