hermes-agent/ui-tui/src/domain/paths.ts
Ben Barclay d33965396e
feat(tui): include session name in the terminal titlebar (#43188)
The terminal/console titlebar was composed from status marker + model +
cwd only; the session's (auto-)title never appeared, even though the TUI
already knows it.

Change the format to `<marker> <session name> · <model> · <cwd>`, with the
session name and cwd each omitted when absent so single-segment titles stay
clean. The current session's live title is pulled from the existing
session.active_list poll (which already carries each session's current flag
and title), so there's no extra round-trip; UiState gains a sessionTitle
field updated only when it actually changes, preserving the existing
idle-flicker guard.

Extract the join logic into a pure composeTabTitle() helper in domain/paths
and cover its edge cases (name omitted, cwd omitted, whitespace-only name,
marker-only fallback, truncation, boundary length) in paths.test.ts.
2026-06-10 11:24:01 +10:00

40 lines
1.3 KiB
TypeScript

export const shortCwd = (cwd: string, max = 28) => {
const h = process.env.HOME
const p = h && cwd.startsWith(h) ? `~${cwd.slice(h.length)}` : cwd
return p.length <= max ? p : `${p.slice(-(max - 1))}`
}
export const fmtCwdBranch = (cwd: string, branch: null | string, max = 40) => {
if (!branch) {
return shortCwd(cwd, max)
}
const tag = ` (${branch.length > 16 ? `${branch.slice(-15)}` : branch})`
return `${shortCwd(cwd, Math.max(8, max - tag.length))}${tag}`
}
/**
* Compose the terminal titlebar string:
* `<marker> <session name> · <model> · <cwd>`
*
* The session name and cwd are each omitted when empty, and a long session
* name is truncated. The marker is always glued to the first present segment
* with a plain space (not a ` · ` separator). When no model is known yet the
* caller should fall back to a plain brand string instead of calling this.
*/
export const composeTabTitle = (
marker: string,
sessionName: string,
model: string,
cwd: string,
maxName = 28
): string => {
const name = sessionName.trim()
const shortName = name.length > maxName ? `${name.slice(0, maxName - 1)}` : name
const segments = [shortName, model, cwd].filter(Boolean)
return segments.length ? `${marker} ${segments.join(' · ')}` : marker
}