Open History to see the last commit
1import { type KnownTerminalSession } from "@t3tools/client-runtime/state/terminal";
2import { DEFAULT_TERMINAL_ID, type ProjectScript } from "@t3tools/contracts";
3import { nextTerminalId, resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabels";
4import * as Arr from "effect/Array";
5import * as Order from "effect/Order";
6
7export {
8 getTerminalLabel,
9 nextTerminalId,
10 resolveTerminalSessionLabel,
11} from "@t3tools/shared/terminalLabels";
12
13export 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
22const 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
30export 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
44export function getTerminalStatusLabel(input: {
45 readonly status: TerminalMenuSession["status"];
46 readonly hasRunningSubprocess?: boolean;
47}): string {
48 if (input.status === "running") {
49 return input.hasRunningSubprocess ? "Task running" : "Ready";
50 }
51 if (input.status === "starting") {
52 return "Starting";
53 }
54 if (input.status === "exited") {
55 return "Exited";
56 }
57 if (input.status === "error") {
58 return "Error";
59 }
60
61 return "Not started";
62}
63
64/** Count the mounted terminal before sessions load, or new-terminal navigation may replace it with itself. */
65export function nextOpenTerminalId(input: {
66 readonly listedTerminalIds: ReadonlyArray<string>;
67 readonly activeRouteTerminalId?: string | null;
68 readonly uniqueSuffix?: string;
69}): string {
70 const listed = input.listedTerminalIds.filter((id) => id.trim().length > 0);
71 const routeId = input.activeRouteTerminalId?.trim() ? input.activeRouteTerminalId : null;
72
73 if (!routeId || listed.includes(routeId)) {
74 return nextTerminalId(listed, input.uniqueSuffix);
75 }
76
77 return nextTerminalId([...listed, routeId], input.uniqueSuffix);
78}
79
80export function buildTerminalMenuSessions(input: {
81 readonly knownSessions: ReadonlyArray<KnownTerminalSession>;
82 readonly workspaceRoot: string | null;
83 readonly currentSession?: TerminalMenuSession | null;
84}): ReadonlyArray<TerminalMenuSession> {
85 const sessionsById = new Map<string, TerminalMenuSession>();
86
87 for (const session of input.knownSessions) {
88 if (
89 session.state.status !== "running" &&
90 session.state.status !== "starting" &&
91 session.target.terminalId !== input.currentSession?.terminalId
92 ) {
93 continue;
94 }
95
96 sessionsById.set(session.target.terminalId, {
97 terminalId: session.target.terminalId,
98 cwd: session.state.summary?.cwd ?? input.workspaceRoot,
99 status: session.state.status,
100 hasRunningSubprocess: session.state.hasRunningSubprocess,
101 displayLabel: resolveTerminalSessionLabel(session.target.terminalId, session.state.summary),
102 updatedAt: session.state.updatedAt,
103 });
104 }
105
106 if (input.currentSession && !sessionsById.has(input.currentSession.terminalId)) {
107 sessionsById.set(input.currentSession.terminalId, input.currentSession);
108 }
109
110 return Arr.sort(sessionsById.values(), terminalMenuSessionOrder);
111}
112
113/** Returns null when no other live session remains; dismiss the terminal UI. */
114export function previousLiveTerminalId(input: {
115 readonly sessions: ReadonlyArray<TerminalMenuSession>;
116 readonly exitedTerminalId: string;
117}): string | null {
118 const live = Arr.sort(
119 input.sessions.filter(
120 (session) =>
121 session.terminalId !== input.exitedTerminalId &&
122 (session.status === "running" || session.status === "starting"),
123 ),
124 terminalMenuSessionOrder,
125 );
126 if (live.length === 0) {
127 return null;
128 }
129
130 const below = live.filter(
131 (session) =>
132 session.terminalId.localeCompare(input.exitedTerminalId, undefined, { numeric: true }) < 0,
133 );
134 return (below[below.length - 1] ?? live[0])?.terminalId ?? null;
135}
136
137export function resolveProjectScriptTerminalId(input: {
138 readonly existingTerminalIds: ReadonlyArray<string>;
139 readonly hasRunningTerminal: boolean;
140 readonly uniqueSuffix?: string;
141}): string {
142 if (!input.hasRunningTerminal && input.uniqueSuffix === undefined) {
143 return DEFAULT_TERMINAL_ID;
144 }
145
146 return nextTerminalId(input.existingTerminalIds, input.uniqueSuffix);
147}
148
149export function projectScriptMenuIcon(icon: ProjectScript["icon"]) {
150 if (icon === "test") return "flask";
151 if (icon === "lint") return "checklist";
152 if (icon === "configure") return "wrench.and.screwdriver";
153 if (icon === "build") return "hammer";
154 if (icon === "debug") return "ladybug";
155 return "play";
156}