Skip to main content

elide_middle

Function elide_middle 

Source
pub fn elide_middle(text: &str, width: usize) -> String
Expand description

Shorten text to width columns, preferring / boundaries (#8164).

Why: a splash or status line that names an absolute path routinely overflows its column, and clipping the tail removes the leaf — the one component a reader is checking. Eliding whole path components instead keeps the deepest directories intact, which is where a repository name and a worktree name live.

What it GUARANTEES, and nothing more: text that already fits width is returned unchanged, and the result never exceeds width — except at a width of 0 or 1, where the one-column … is kept rather than an empty string. For a path, the deepest components arrive WHOLE behind a leading …/, and the repository directory name survives alongside them: when the deepest contiguous run that fits does not reach back to the repository — a linked worktree under <repo>/.claude/worktrees/<name> is the motivating case (#8205) — that run is replaced by …/<repo>/…/<tail>, so the name a reader identifies the checkout by stays on the row. The two columns each …/ costs are part of the budget.

Order of surrender, repository name LAST (owner ruling 2026-09-17): tail components go one at a time, then the tail entirely (…/<repo>/…), and only a repository name too wide for width itself is given up — that path falls back to the gapless …/<tail> run, keeping whole components even though the repository name is no longer among them. Character elision, which keeps both ends and replaces the middle with …, is the last resort: it takes a string with no /, and any path where not even the last two components fit. Widths count chars, never bytes. Test: tests::elide_middle_keeps_whole_trailing_path_components, tests::elide_middle_keeps_the_repo_name_at_both_render_budgets, tests::elide_middle_keeps_a_repo_name_that_is_the_leaf, tests::elide_middle_drops_the_tail_before_the_repo_name, tests::elide_middle_keeps_a_fitting_path_unchanged, tests::elide_middle_falls_back_to_character_elision, tests::elide_middle_handles_degenerate_widths.