Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

terminalMenu.ts

152 lines4.9 KB

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