Files
web-terminal/public/title-util.ts
Yaojia Wang 87e11734d8 feat(v0.3): M6 — new tab opens in the active tab's directory
- title-util.cwdFromOsc7: parse cwd from OSC 7 (file:// URL); 4 unit tests
- terminal-session: register OSC 7 handler → cwd getter; opts.cwd → sent in the
  attach frame on a fresh session
- protocol/types: ClientMessage attach.cwd (absolute path); validated
- session/manager/server: thread cwd → createSession spawn cwd (defaults homeDir)
- tabs: '+' (newTab) opens in the active tab's reported cwd, falls back to home
- Verified: server attach.cwd spawns there; cd /private/tmp → + → new tab pwd is
  /private/tmp. 212 tests green.
2026-06-18 07:14:52 +02:00

42 lines
1.3 KiB
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
}
/**
* Parse the full cwd path from an OSC 7 sequence payload (M6).
* data = "file://host/Users/me/dir" → "/Users/me/dir"
* Returns null if it isn't a file:// URL.
*/
export function cwdFromOsc7(data: string): string | null {
const m = /^file:\/\/[^/]*(\/.*)$/.exec(data.trim())
if (m === null) return null
const path = m[1] as string
try {
return decodeURIComponent(path)
} catch {
return path
}
}