- title-util.ts: folderFromTitle() parses 'user@host:~/path' → last folder segment (home stays ~); pure, dependency-free, 7 unit tests - terminal-session.ts: onTitleChange → folderFromTitle so the tab auto-names to the cwd folder and updates live on cd - Browser-verified: ~ → cd project → 'web-terminal' → cd docs → 'docs' → cd ~ → '~'. 198 tests green, typecheck + build ok.
26 lines
916 B
TypeScript
26 lines
916 B
TypeScript
/**
|
|
* public/title-util.ts — derive a short tab title from a terminal OSC title.
|
|
*
|
|
* Pure, dependency-free (no xterm/DOM) so it's unit-testable in node.
|
|
*/
|
|
|
|
/**
|
|
* Derive the **current folder** from the terminal's OSC title.
|
|
*
|
|
* Most shells emit titles like `user@host:~/path/to/dir` (zsh/bash default).
|
|
* Take the part after the last ':' (the path), then its last segment (the
|
|
* folder name). `~` (home) stays `~`. Returns null for an empty title; falls
|
|
* back to the raw-ish basename when there's no `user@host:` shape (e.g. a
|
|
* running command title).
|
|
*/
|
|
export function folderFromTitle(title: string): string | null {
|
|
let s = title.trim()
|
|
if (!s) return null
|
|
const colon = s.lastIndexOf(':')
|
|
if (colon !== -1) s = s.slice(colon + 1).trim()
|
|
s = s.replace(/\/+$/, '') // strip trailing slash
|
|
const slash = s.lastIndexOf('/')
|
|
if (slash !== -1) s = s.slice(slash + 1)
|
|
return s || null
|
|
}
|