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/**
65 * Counts the terminal screen already mounted (`activeRouteTerminalId`) as occupied so
66 * an empty session list on the primary route still advances to `term-2` instead of
67 * `replace`-navigating to the same `default` tab.
68 */
69export function nextOpenTerminalId(input: {
70 readonly listedTerminalIds: ReadonlyArray<string>;
71 readonly activeRouteTerminalId?: string | null;
72 readonly uniqueSuffix?: string;
73}): string {
74 const listed = input.listedTerminalIds.filter((id) => id.trim().length > 0);
75 const routeId = input.activeRouteTerminalId?.trim() ? input.activeRouteTerminalId : null;
76
77 if (!routeId || listed.includes(routeId)) {
78 return nextTerminalId(listed, input.uniqueSuffix);
79 }
80
81 return nextTerminalId([...listed, routeId], input.uniqueSuffix);
82}
83
84export function buildTerminalMenuSessions(input: {
85 readonly knownSessions: ReadonlyArray<KnownTerminalSession>;
86 readonly workspaceRoot: string | null;
87 readonly currentSession?: TerminalMenuSession | null;
88}): ReadonlyArray<TerminalMenuSession> {
89 const sessionsById = new Map<string, TerminalMenuSession>();
90
91 for (const session of input.knownSessions) {
92 if (
93 session.state.status !== "running" &&
94 session.state.status !== "starting" &&
95 session.target.terminalId !== input.currentSession?.terminalId
96 ) {
97 continue;
98 }
99
100 sessionsById.set(session.target.terminalId, {
101 terminalId: session.target.terminalId,
102 cwd: session.state.summary?.cwd ?? input.workspaceRoot,
103 status: session.state.status,
104 hasRunningSubprocess: session.state.hasRunningSubprocess,
105 displayLabel: resolveTerminalSessionLabel(session.target.terminalId, session.state.summary),
106 updatedAt: session.state.updatedAt,
107 });
108 }
109
110 if (input.currentSession && !sessionsById.has(input.currentSession.terminalId)) {
111 sessionsById.set(input.currentSession.terminalId, input.currentSession);
112 }
113
114 return Arr.sort(sessionsById.values(), terminalMenuSessionOrder);
115}
116
117/**
118 * Returns null when no other live session remains and the terminal UI should
119 * be dismissed instead.
120 */
121export function previousLiveTerminalId(input: {
122 readonly sessions: ReadonlyArray<TerminalMenuSession>;
123 readonly exitedTerminalId: string;
124}): string | null {
125 const live = Arr.sort(
126 input.sessions.filter(
127 (session) =>
128 session.terminalId !== input.exitedTerminalId &&
129 (session.status === "running" || session.status === "starting"),
130 ),
131 terminalMenuSessionOrder,
132 );
133 if (live.length === 0) {
134 return null;
135 }
136
137 const below = live.filter(
138 (session) =>
139 session.terminalId.localeCompare(input.exitedTerminalId, undefined, { numeric: true }) < 0,
140 );
141 return (below[below.length - 1] ?? live[0])?.terminalId ?? null;
142}
143
144export function resolveProjectScriptTerminalId(input: {
145 readonly existingTerminalIds: ReadonlyArray<string>;
146 readonly hasRunningTerminal: boolean;
147 readonly uniqueSuffix?: string;
148}): string {
149 if (!input.hasRunningTerminal && input.uniqueSuffix === undefined) {
150 return DEFAULT_TERMINAL_ID;
151 }
152
153 return nextTerminalId(input.existingTerminalIds, input.uniqueSuffix);
154}
155
156export function projectScriptMenuIcon(icon: ProjectScript["icon"]) {
157 if (icon === "test") return "flask";
158 if (icon === "lint") return "checklist";
159 if (icon === "configure") return "wrench.and.screwdriver";
160 if (icon === "build") return "hammer";
161 if (icon === "debug") return "ladybug";
162 return "play";
163}