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

terminalLabels.ts

40 lines1.3 KB

1import type { TerminalSummary } from "@t3tools/contracts";
2​
3/** Human-readable label for a terminal tab; matches mobile and web sidebars. */
4export function getTerminalLabel(terminalId: string): string {
5 const numericSuffix = /^term(?:inal)?-(\d+)$/i.exec(terminalId)?.[1];
6 if (numericSuffix) {
7 return `Terminal ${numericSuffix}`;
8 }
9​
10 return terminalId;
12​
13/** Prefer server summary label when present; otherwise fall back to `getTerminalLabel`. */
14export function resolveTerminalSessionLabel(
15 terminalId: string,
16 summary: Pick<TerminalSummary, "label"> | null | undefined,
17): string {
18 const trimmed = summary?.label?.trim();
19 if (trimmed && trimmed.length > 0) {
20 return trimmed;
21 }
22 return getTerminalLabel(terminalId);
24​
25/**
26 * Client-side terminal id allocator. Ids are ALWAYS chosen by the client and sent explicitly
27 * on every `terminal.open` / `terminal.attach` call — the server never allocates.
28 *
29 * Returns the lowest unused `term-N` id (starting at `term-1`), skipping any ids already in
30 * `existingTerminalIds`.
31 */
32export function nextTerminalId(existingTerminalIds: ReadonlyArray<string>): string {
33 const usedIds = new Set(existingTerminalIds.filter((id) => id.trim().length > 0));
34 let nextIndex = 1;
35 while (usedIds.has(`term-${nextIndex}`)) {
36 nextIndex += 1;
37 }
38​
39 return `term-${nextIndex}`;