Skip to content

fix(tui): truncate paths in dialogs by display width - #5118

Open
cpruijsen wants to merge 2 commits into
docker:mainfrom
cpruijsen:fix-working-dir-truncate-width
Open

cpruijsen wants to merge 2 commits into
docker:mainfrom
cpruijsen:fix-working-dir-truncate-width

Conversation

@cpruijsen

@cpruijsen cpruijsen commented Dec 4, 2026 •

Copy link
Copy Markdown

Problem

truncatePath in pkg/tui/dialog/working_dir_picker.go measured and cut paths in bytes. The session browser, the /context dialog and the working directory picker use it. A path with multi-byte characters could be cut in the middle of a rune, so the row rendered invalid UTF-8. For example, the session browser showed "…\xa3目录/子目录/第二层" for a session in /home/user/项目/文档与资料/长期保存的归档目录/子目录/第二层. Paths with accented letters were also cut shorter than the row allowed, because each such letter is two bytes but one column.

Fix

  • truncatePath now calls toolcommon.TruncateTextLeft, the width-aware helper that the file picker has used since fix(tui): truncate the file picker's directory by display width #5026. It keeps the end of the path behind the ellipsis.
  • Before it truncates, it escapes line breaks and tabs with the file picker's pathEscaper, also from fix(tui): truncate the file picker's directory by display width #5026. Lip Gloss expands a tab to four spaces after the width is measured, so an unescaped tab could push the row past its width. lipgloss.Width measures only the widest line, so without the escape a directory name with a line break could split the row. ASCII paths with no line break are cut exactly as before.
  • TestTruncatePathKeepsValidUTF8 checks the exact result for a CJK path at eight widths. TestTruncatePathMeasuresDisplayWidth checks that a path with accented letters that fits is not cut, and that an ASCII path is cut as before. Both tests fail before the fix. TestTruncatePathKeepsOneLine checks that a path with \n or \r stays on one line.

truncatePath measured and cut paths in bytes. A path with multi-byte characters could be cut in the middle of a rune, which rendered invalid UTF-8, and it was cut shorter than the width allowed. It now uses toolcommon.TruncateTextLeft, as the file picker does, and escapes line breaks first with the file picker's pathEscaper so a path stays on one line.

Signed-off-by: Christopher Pruijsen <christopher.pruijsen@gmail.com>
@cpruijsen
cpruijsen requested a review from a team as a code owner December 4, 2026 00:48
@aheritier aheritier added area/tui For features/issues/fixes related to the TUI kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Dec 4, 2026

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One blocking issue inline: paths containing tabs can overflow their column budget and lose the suffix this change is meant to preserve.

return "…" + path[len(path)-(maxLen-1):]
// truncatePath shortens a path to fit within maxWidth display columns, prefixing with "…" when truncated.
func truncatePath(path string, maxWidth int) string {
return toolcommon.TruncateTextLeft(pathEscaper.Replace(path), maxWidth)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[blocking] Escape tabs before measuring the path. With a width of 28, a / followed by a tab and 27 ASCII letters now passes the fit check: ansi.StringWidth doesn't count the tab, but Lip Gloss expands it to four spaces when session_browser.go:638 styles the row. The path renders at 32 columns, so a full row gets clipped from the right (scrollview.go:429), losing the path suffix. The old byte cut dropped the tab and kept all 27 letters. Please add a regression test that checks the styled result fits and retains the tail.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 13ad920. Tabs are escaped before the width check, and the test uses your case.

@aheritier
aheritier marked this pull request as draft December 4, 2026 12:44
@aheritier

Copy link
Copy Markdown
Collaborator

Please mark this PR ready for review once you’ve addressed the requested changes. Thanks!

Signed-off-by: Christopher Pruijsen <christopher.pruijsen@gmail.com>
@cpruijsen
cpruijsen marked this pull request as ready for review December 4, 2026 17:17

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tui For features/issues/fixes related to the TUI kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants