Skip to content

fix(current-working-dir): don't count ~ as a segment, and preview every segment count - #685

Open
eric-engberg wants to merge 1 commit into
sirmalloc:mainfrom
eric-engberg:fix/cwd-segments
Open

eric-engberg wants to merge 1 commit into
sirmalloc:mainfrom
eric-engberg:fix/cwd-segments

Conversation

@eric-engberg

Copy link
Copy Markdown
Contributor

What

  • Home abbreviation with segments: Current Working Dir no longer counts the leading ~ as one of the kept segments. With abbreviate-home on and segments set to 2, ~/Projects/my-project now renders as ~/Projects/my-project instead of ~/.../Projects/my-project, which elided nothing.
  • Preview: the TUI preview now shows the segment count that's set. With segments set to 3 it previews .../Documents/Projects/my-project instead of .../example/project.

Why

  • ~ counted as a segment: the widget splits the path and elides it when there are more parts than segments. After home abbreviation, ~ is one of those parts:

    cwd settings before after
    ~/a/b home, segments 2 ~/.../a/b ~/a/b
    ~/a home, segments 1 ~/.../a ~/a
    ~/a/b/c home, segments 2 ~/.../b/c ~/.../b/c
    /a/b segments 2 /a/b /a/b

    The ~ is always kept in front of the ..., so counting it meant one segment fewer was ever shown in full, and the ... stood for nothing.

  • Preview: the preview was a set of fixed strings for one segment and "more than one". Every count from 2 up previewed .../example/project (or ~/.../Projects/my-project with home on), while the status line shows that many segments. The strings also weren't cut from the sample path the preview otherwise shows (/Users/example/Documents/Projects/my-project): one segment previewed .../project.

How

The segment logic moves into a keepLastSegments helper that both the status line and the preview call. It skips a leading ~ part when counting and keeps it in front as before. Everything else works as before: separator detection (/ or \), UNC and leading separators, and paths outside home. Fish style ignores segments, as before. The preview runs the helper over its sample path (/Users/example/Documents/Projects/my-project, or ~/Documents/Projects/my-project with home on), so it shows exactly what the setting does to that path. Previews for one and two segments change too, to segments of the sample path: .../my-project and .../Projects/my-project.

Demo

The TUI preview is its built-in sample path. The status line is piped with sample payloads under a scratch home.

Setting segments to 3 in the TUI:

Powerline: before

Segments set to 3, preview before the fix, Powerline mode

Powerline: after

Segments set to 3, preview after the fix, Powerline mode

Plain: before

Segments set to 3, preview before the fix, plain mode

Plain: after

Segments set to 3, preview after the fix, plain mode

Home abbreviation with segments set to 2, for a directory two levels below home and one three levels below (unchanged):

Powerline: before

Home abbreviation with 2 segments before the fix, Powerline mode

Powerline: after

Home abbreviation with 2 segments after the fix, Powerline mode

Plain: before

Home abbreviation with 2 segments before the fix, plain mode

Plain: after

Home abbreviation with 2 segments after the fix, plain mode

Testing

  • New tests, each failing on main first: ~ isn't counted (2 of ~/Projects/my-project, 1 of ~/Projects); the preview at 1 to 5 segments, and at 3 and 4 with home on.
  • bun test: 2791 pass, 0 fail. bun run lint passes.
  • The changed test file under Node (Vitest) fails as a whole, as it does on main (24 of 24 there, 33 of 33 here). Its beforeEach spies on os.homedir, which Node doesn't allow. So I checked the built CLI instead: under Bun 1.4.2 and Node 26.10.0 with a scratch HOME, ~/Projects/my-project (2) and ~/Projects (1) render without .... ~/Documents/Projects/my-project (2), /a/b and /a/b/c render as on main. Both runtimes agree.
  • Built CLI with a Model and Current Working Dir line (home on, 2 segments) and the baseline line, under Bun 1.4.2 and Node 26.10.0: all runtimes agree in plain and Powerline modes, and the output matches main (the payload's cwd isn't under home). The TUI opens and exits cleanly under both.

…ry segment count

With abbreviate-home on, the leading "~" counted as one of the kept
segments. ~/a/b with segments=2 had three parts, so it rendered
"~/.../a/b" though nothing was left out; /a/b with segments=2 renders
"/a/b". The "~" is always kept in front, so it no longer counts.

The preview was a set of fixed strings for one or two segments, so
segments=3 or 4 still previewed two (".../example/project"), and those
strings didn't come from the sample path itself. The preview now runs
the same segment logic over the sample path that a real path goes
through, so it shows what the setting produces.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant