-
Notifications
You must be signed in to change notification settings - Fork 8
feat: include TTY and tmux pane in session list #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,120 @@ | ||||||||||||||||||||||||||
| import type { SessionTerminalLocation, SessionTerminalMetadata } from "./types"; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| function trimmed(value: string | undefined) { | ||||||||||||||||||||||||||
| const normalized = value?.trim(); | ||||||||||||||||||||||||||
| return normalized && normalized.length > 0 ? normalized : undefined; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| function sameLocation(left: SessionTerminalLocation, right: SessionTerminalLocation) { | ||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||
| left.source === right.source && | ||||||||||||||||||||||||||
| left.tty === right.tty && | ||||||||||||||||||||||||||
| left.windowId === right.windowId && | ||||||||||||||||||||||||||
| left.tabId === right.tabId && | ||||||||||||||||||||||||||
| left.paneId === right.paneId && | ||||||||||||||||||||||||||
| left.terminalId === right.terminalId && | ||||||||||||||||||||||||||
| left.sessionId === right.sessionId | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| function pushLocation(locations: SessionTerminalLocation[], location: SessionTerminalLocation) { | ||||||||||||||||||||||||||
| if (!locations.some((existing) => sameLocation(existing, location))) { | ||||||||||||||||||||||||||
| locations.push(location); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| function inferLocationSource(program: string | undefined) { | ||||||||||||||||||||||||||
| const normalized = program?.trim().toLowerCase(); | ||||||||||||||||||||||||||
| if (!normalized) { | ||||||||||||||||||||||||||
| return "terminal"; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (normalized === "iterm.app" || normalized === "iterm2") { | ||||||||||||||||||||||||||
| return "iterm2"; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (normalized === "ghostty") { | ||||||||||||||||||||||||||
| return "ghostty"; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (normalized === "apple_terminal" || normalized === "apple terminal") { | ||||||||||||||||||||||||||
| return "terminal.app"; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return "terminal"; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| function parseHierarchicalIds(sessionId: string) { | ||||||||||||||||||||||||||
| const prefix = sessionId.split(":", 1)[0]?.trim(); | ||||||||||||||||||||||||||
| if (!prefix) { | ||||||||||||||||||||||||||
| return {}; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const match = /^w(?<window>\d+)t(?<tab>\d+)(?:p(?<pane>\d+))?$/i.exec(prefix); | ||||||||||||||||||||||||||
| if (!match?.groups) { | ||||||||||||||||||||||||||
| return {}; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||
| windowId: match.groups.window, | ||||||||||||||||||||||||||
| tabId: match.groups.tab, | ||||||||||||||||||||||||||
| paneId: match.groups.pane, | ||||||||||||||||||||||||||
| } satisfies Pick<SessionTerminalLocation, "windowId" | "tabId" | "paneId">; | ||||||||||||||||||||||||||
|
Comment on lines
+58
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the hierarchical ID has no pane (e.g.
Suggested change
|
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Capture terminal- and multiplexer-facing location metadata for one Hunk TUI session. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * The structure is intentionally generic so we can layer tmux, iTerm2, Ghostty, | ||||||||||||||||||||||||||
| * and future terminal integrations without adding a new top-level field for each one. | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| export function resolveSessionTerminalMetadata({ | ||||||||||||||||||||||||||
| env = process.env, | ||||||||||||||||||||||||||
| tty, | ||||||||||||||||||||||||||
| }: { | ||||||||||||||||||||||||||
| env?: NodeJS.ProcessEnv; | ||||||||||||||||||||||||||
| tty?: string; | ||||||||||||||||||||||||||
| } = {}): SessionTerminalMetadata | undefined { | ||||||||||||||||||||||||||
| const termProgram = trimmed(env.TERM_PROGRAM); | ||||||||||||||||||||||||||
| const lcTerminal = trimmed(env.LC_TERMINAL); | ||||||||||||||||||||||||||
| const program = | ||||||||||||||||||||||||||
| termProgram?.toLowerCase() === "tmux" && lcTerminal ? lcTerminal : (termProgram ?? lcTerminal); | ||||||||||||||||||||||||||
| const locations: SessionTerminalLocation[] = []; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const ttyPath = trimmed(tty); | ||||||||||||||||||||||||||
| if (ttyPath) { | ||||||||||||||||||||||||||
| pushLocation(locations, { source: "tty", tty: ttyPath }); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const tmuxPane = trimmed(env.TMUX_PANE); | ||||||||||||||||||||||||||
| if (tmuxPane) { | ||||||||||||||||||||||||||
| pushLocation(locations, { source: "tmux", paneId: tmuxPane }); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const iTermSessionId = trimmed(env.ITERM_SESSION_ID); | ||||||||||||||||||||||||||
| if (iTermSessionId) { | ||||||||||||||||||||||||||
| pushLocation(locations, { | ||||||||||||||||||||||||||
| source: "iterm2", | ||||||||||||||||||||||||||
| sessionId: iTermSessionId, | ||||||||||||||||||||||||||
| ...parseHierarchicalIds(iTermSessionId), | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const terminalSessionId = trimmed(env.TERM_SESSION_ID); | ||||||||||||||||||||||||||
| if (terminalSessionId && terminalSessionId !== iTermSessionId) { | ||||||||||||||||||||||||||
| pushLocation(locations, { | ||||||||||||||||||||||||||
| source: inferLocationSource(program), | ||||||||||||||||||||||||||
| sessionId: terminalSessionId, | ||||||||||||||||||||||||||
| ...parseHierarchicalIds(terminalSessionId), | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (!program && locations.length === 0) { | ||||||||||||||||||||||||||
| return undefined; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||
| program, | ||||||||||||||||||||||||||
| locations, | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ import type { | |
| RemovedCommentResult, | ||
| SelectedSessionContext, | ||
| SessionLiveCommentSummary, | ||
| SessionTerminalLocation, | ||
| SessionTerminalMetadata, | ||
| } from "../mcp/types"; | ||
| import { | ||
| HUNK_SESSION_API_PATH, | ||
|
|
@@ -359,32 +361,99 @@ function formatSelectedSummary(session: ListedSession) { | |
| return filePath === "(none)" ? filePath : `${filePath} hunk ${hunkNumber}`; | ||
| } | ||
|
|
||
| function formatTerminalLocation(location: SessionTerminalLocation) { | ||
| const parts: string[] = []; | ||
|
|
||
| if (location.tty) { | ||
| parts.push(location.tty); | ||
| } | ||
|
|
||
| if (location.windowId) { | ||
| parts.push(`window ${location.windowId}`); | ||
| } | ||
|
|
||
| if (location.tabId) { | ||
| parts.push(`tab ${location.tabId}`); | ||
| } | ||
|
|
||
| if (location.paneId) { | ||
| parts.push(`pane ${location.paneId}`); | ||
| } | ||
|
|
||
| if (location.terminalId) { | ||
| parts.push(`terminal ${location.terminalId}`); | ||
| } | ||
|
|
||
| if (location.sessionId) { | ||
| parts.push(`session ${location.sessionId}`); | ||
| } | ||
|
|
||
| return parts.length > 0 ? parts.join(", ") : "present"; | ||
| } | ||
|
|
||
| function resolveSessionTerminal(session: ListedSession) { | ||
| return session.terminal; | ||
| } | ||
|
Comment on lines
+394
to
+396
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
|
|
||
| function formatTerminalLines( | ||
| terminal: SessionTerminalMetadata | undefined, | ||
| { | ||
| headerLabel, | ||
| locationLabel, | ||
| }: { | ||
| headerLabel: string; | ||
| locationLabel: string; | ||
| }, | ||
| ) { | ||
| if (!terminal) { | ||
| return []; | ||
| } | ||
|
|
||
| return [ | ||
| ...(terminal.program ? [`${headerLabel}: ${terminal.program}`] : []), | ||
| ...terminal.locations.map( | ||
| (location) => `${locationLabel}[${location.source}]: ${formatTerminalLocation(location)}`, | ||
| ), | ||
| ]; | ||
| } | ||
|
|
||
| function formatListOutput(sessions: ListedSession[]) { | ||
| if (sessions.length === 0) { | ||
| return "No active Hunk sessions.\n"; | ||
| } | ||
|
|
||
| return `${sessions | ||
| .map((session) => | ||
| [ | ||
| .map((session) => { | ||
| const terminal = resolveSessionTerminal(session); | ||
| return [ | ||
| `${session.sessionId} ${session.title}`, | ||
| ` repo: ${session.repoRoot ?? session.cwd}`, | ||
| ...formatTerminalLines(terminal, { | ||
| headerLabel: " terminal", | ||
| locationLabel: " location", | ||
| }), | ||
| ` focus: ${formatSelectedSummary(session)}`, | ||
| ` files: ${session.fileCount}`, | ||
| ` comments: ${session.snapshot.liveCommentCount}`, | ||
| ].join("\n"), | ||
| ) | ||
| ].join("\n"); | ||
| }) | ||
| .join("\n\n")}\n`; | ||
| } | ||
|
|
||
| function formatSessionOutput(session: ListedSession) { | ||
| const terminal = resolveSessionTerminal(session); | ||
|
|
||
| return [ | ||
| `Session: ${session.sessionId}`, | ||
| `Title: ${session.title}`, | ||
| `Source: ${session.sourceLabel}`, | ||
| `Repo: ${session.repoRoot ?? session.cwd}`, | ||
| `Input: ${session.inputKind}`, | ||
| `Launched: ${session.launchedAt}`, | ||
| ...formatTerminalLines(terminal, { | ||
| headerLabel: "Terminal", | ||
| locationLabel: "Location", | ||
| }), | ||
| `Selected: ${formatSelectedSummary(session)}`, | ||
| `Agent notes visible: ${session.snapshot.showAgentNotes ? "yes" : "no"}`, | ||
| `Live comments: ${session.snapshot.liveCommentCount}`, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current guard against a failed
ttyinvocation checks whether stdout starts with"not a tty", which is an English-locale assumption. On non-English systems thettycommand may emit a localised error string that passes through this check, potentially storing a garbage path. Checkingresult.status === 0is locale-independent and more robust: