The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { type KnownTerminalSession } from "@t3tools/client-runtime/state/terminal"; |
| 2 | import { DEFAULT_TERMINAL_ID, type ProjectScript } from "@t3tools/contracts"; |
| 3 | import { nextTerminalId } from "@t3tools/shared/terminalLabels"; |
| 4 | import { resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabelsCopy"; |
| 5 | import * as Arr from "effect/Array"; |
| 6 | import * as Order from "effect/Order"; |
| 7 | import { msg } from "gt-react-native"; |
| 8 | |
| 9 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 10 | |
| 11 | export { resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabelsCopy"; |
| 12 | |
| 13 | export interface TerminalMenuSession { |
| 14 | readonly terminalId: string; |
| 15 | readonly cwd: string | null; |
| 16 | readonly status: "starting" | "running" | "exited" | "error" | "closed"; |
| 17 | readonly hasRunningSubprocess: boolean; |
| 18 | readonly displayLabel: string; |
| 19 | readonly updatedAt: string | null; |
| 20 | } |
| 21 | |
| 22 | const terminalMenuSessionOrder = Order.make<TerminalMenuSession>((left, right) => { |
| 23 | const comparison = left.terminalId.localeCompare(right.terminalId, undefined, { numeric: true }); |
| 24 | if (comparison === 0) { |
| 25 | return 0; |
| 26 | } |
| 27 | return comparison < 0 ? -1 : 1; |
| 28 | }); |
| 29 | |
| 30 | export function basename(input: string | null): string | null { |
| 31 | if (!input) { |
| 32 | return null; |
| 33 | } |
| 34 | |
| 35 | const normalized = input.replace(/\/+$/, ""); |
| 36 | if (normalized.length === 0) { |
| 37 | return "/"; |
| 38 | } |
| 39 | |
| 40 | const segments = normalized.split("/"); |
| 41 | return segments[segments.length - 1] ?? normalized; |
| 42 | } |
| 43 | |
| 44 | export function getTerminalStatusLabel( |
| 45 | input: { |
| 46 | readonly status: TerminalMenuSession["status"]; |
| 47 | readonly hasRunningSubprocess?: boolean; |
| 48 | }, |
| 49 | resolve: MessageResolver = m, |
| 50 | ): string { |
| 51 | if (input.status === "running") { |
| 52 | return input.hasRunningSubprocess |
| 53 | ? resolve(msg("Task running", { $context: "terminal status" })) |
| 54 | : resolve(msg("Ready", { $context: "terminal status" })); |
| 55 | } |
| 56 | if (input.status === "starting") { |
| 57 | return resolve(msg("Starting", { $context: "terminal status" })); |
| 58 | } |
| 59 | if (input.status === "exited") { |
| 60 | return resolve(msg("Exited", { $context: "terminal status" })); |
| 61 | } |
| 62 | if (input.status === "error") { |
| 63 | return resolve(msg("Error", { $context: "terminal status" })); |
| 64 | } |
| 65 | |
| 66 | return resolve(msg("Not started", { $context: "terminal status" })); |
| 67 | } |
| 68 | |
| 69 | /** Count the mounted terminal before sessions load, or new-terminal navigation may replace it with itself. */ |
| 70 | export function nextOpenTerminalId(input: { |
| 71 | readonly listedTerminalIds: ReadonlyArray<string>; |
| 72 | readonly activeRouteTerminalId?: string | null; |
| 73 | readonly uniqueSuffix?: string; |
| 74 | }): string { |
| 75 | const listed = input.listedTerminalIds.filter((id) => id.trim().length > 0); |
| 76 | const routeId = input.activeRouteTerminalId?.trim() ? input.activeRouteTerminalId : null; |
| 77 | |
| 78 | if (!routeId || listed.includes(routeId)) { |
| 79 | return nextTerminalId(listed, input.uniqueSuffix); |
| 80 | } |
| 81 | |
| 82 | return nextTerminalId([...listed, routeId], input.uniqueSuffix); |
| 83 | } |
| 84 | |
| 85 | export function buildTerminalMenuSessions( |
| 86 | input: { |
| 87 | readonly knownSessions: ReadonlyArray<KnownTerminalSession>; |
| 88 | readonly workspaceRoot: string | null; |
| 89 | readonly currentSession?: TerminalMenuSession | null; |
| 90 | }, |
| 91 | resolve: MessageResolver = m, |
| 92 | ): ReadonlyArray<TerminalMenuSession> { |
| 93 | const sessionsById = new Map<string, TerminalMenuSession>(); |
| 94 | |
| 95 | for (const session of input.knownSessions) { |
| 96 | if ( |
| 97 | session.state.status !== "running" && |
| 98 | session.state.status !== "starting" && |
| 99 | session.target.terminalId !== input.currentSession?.terminalId |
| 100 | ) { |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | sessionsById.set(session.target.terminalId, { |
| 105 | terminalId: session.target.terminalId, |
| 106 | cwd: session.state.summary?.cwd ?? input.workspaceRoot, |
| 107 | status: session.state.status, |
| 108 | hasRunningSubprocess: session.state.hasRunningSubprocess, |
| 109 | displayLabel: resolveTerminalSessionLabel( |
| 110 | session.target.terminalId, |
| 111 | session.state.summary, |
| 112 | resolve, |
| 113 | ), |
| 114 | updatedAt: session.state.updatedAt, |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | if (input.currentSession && !sessionsById.has(input.currentSession.terminalId)) { |
| 119 | sessionsById.set(input.currentSession.terminalId, input.currentSession); |
| 120 | } |
| 121 | |
| 122 | return Arr.sort(sessionsById.values(), terminalMenuSessionOrder); |
| 123 | } |
| 124 | |
| 125 | /** Returns null when no other live session remains; dismiss the terminal UI. */ |
| 126 | export function previousLiveTerminalId(input: { |
| 127 | readonly sessions: ReadonlyArray<TerminalMenuSession>; |
| 128 | readonly exitedTerminalId: string; |
| 129 | }): string | null { |
| 130 | const live = Arr.sort( |
| 131 | input.sessions.filter( |
| 132 | (session) => |
| 133 | session.terminalId !== input.exitedTerminalId && |
| 134 | (session.status === "running" || session.status === "starting"), |
| 135 | ), |
| 136 | terminalMenuSessionOrder, |
| 137 | ); |
| 138 | if (live.length === 0) { |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | const below = live.filter( |
| 143 | (session) => |
| 144 | session.terminalId.localeCompare(input.exitedTerminalId, undefined, { numeric: true }) < 0, |
| 145 | ); |
| 146 | return (below[below.length - 1] ?? live[0])?.terminalId ?? null; |
| 147 | } |
| 148 | |
| 149 | export function resolveProjectScriptTerminalId(input: { |
| 150 | readonly existingTerminalIds: ReadonlyArray<string>; |
| 151 | readonly hasRunningTerminal: boolean; |
| 152 | readonly uniqueSuffix?: string; |
| 153 | }): string { |
| 154 | if (!input.hasRunningTerminal && input.uniqueSuffix === undefined) { |
| 155 | return DEFAULT_TERMINAL_ID; |
| 156 | } |
| 157 | |
| 158 | return nextTerminalId(input.existingTerminalIds, input.uniqueSuffix); |
| 159 | } |
| 160 | |
| 161 | export function projectScriptMenuIcon(icon: ProjectScript["icon"]) { |
| 162 | if (icon === "test") return "flask"; |
| 163 | if (icon === "lint") return "checklist"; |
| 164 | if (icon === "configure") return "wrench.and.screwdriver"; |
| 165 | if (icon === "build") return "hammer"; |
| 166 | if (icon === "debug") return "ladybug"; |
| 167 | return "play"; |
| 168 | } |