fix(tui): truncate helpers can return a longer string than they got - #41060
Open
Mute-404 wants to merge 1 commit into
Open
fix(tui): truncate helpers can return a longer string than they got#41060Mute-404 wants to merge 1 commit into
Mute-404 wants to merge 1 commit into
Conversation
Both helpers compute a slice length that goes to zero or negative on
small budgets, and slice(-0) hands back the whole string. So they grow
the text right when the terminal has the least room for it:
truncateMiddle("abcdefghij", 0) === "…bcdefghij"
truncateLeft("abcdefghij", 1) === "…abcdefghij"
At 10 columns dialog-select.tsx:702 asks for 1 char and gets 40 back.
Same for dialog-move-session.tsx:151. The Math.max(1, ...) at those call
sites reads like a guard but clamps straight into the broken range.
Clamped inside the helpers so callers don't each need their own guard.
Adds tests for util/locale, which had none.
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
This was referenced Aug 7, 2026
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.
Issue for this PR
Closes #37458
(I opened #41054 before finding #37458. Closing mine as a duplicate — this PR targets the existing one. Same bug was also reported in #20880 and #34395, and a fix in #34482 was closed by the automated PR cleanup in July rather than reviewed.)
Type of change
What does this PR do?
truncateLeftandtruncateMiddleinpackages/tui/src/util/locale.tscompute a slice length that reaches zero or goes negative on small budgets.String.prototype.slice(-0)isslice(0), which returns the whole string, so both helpers hand back something longer than the input:They grow the text exactly when the terminal has the least room for it.
Clamped inside both helpers so callers don't each need their own guard.
truncateMiddlealso got restructured slightly:keepEndis now checked before slicing, which is what removes theslice(-0)path.Worth noting for review: the
Math.max(1, ...)atdialog-select.tsx:702anddialog-move-session.tsx:151looks like a guard against this, but 1 is inside the broken range, so it clamps straight into it.files.tsx:35usesMath.max(2, ...)and happens to land just outside. After this change those clamps are redundant but harmless, so I left them alone to keep the diff focused.How did you verify your code works?
Ran the actual call site expressions from the TUI against the real module, on
devand on this branch:9 call sites returned more than their budget before, 0 after. Output stays correct for normal widths (
truncateMiddle("...", 5)gives"ab…ij").util/locale.tshad no tests, which is presumably how this survived three reports. The tests assert the budget is never exceeded across the whole small range rather than checking specific strings.Two things I deliberately left out:
packages/core/src/util/path.ts:31has a byte-identical copy of the same bug. Different package, seemed like it should be its own PR — happy to include it here if you'd prefer.str.length(UTF-16 code units) while callers are budgeting terminal columns, so CJK and emoji still under-truncate.packages/tui/src/prompt/display.ts:7shows the house approach withBun.stringWidth+Intl.Segmenter. That's a behavior change and needs its own discussion.Screenshots / recordings
This only shows up below roughly 16 columns, where the layout is already degraded. The table above is the measurable version.
Checklist