Open History to see the last commit
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 return numericSuffix ? `Terminal ${numericSuffix}` : terminalId;
7}
8
9/** Prefer server summary label when present; otherwise fall back to `getTerminalLabel`. */
10export function resolveTerminalSessionLabel(
11 terminalId: string,
12 summary: Pick<TerminalSummary, "label"> | null | undefined,
13): string {
14 return summary?.label?.trim() || getTerminalLabel(terminalId);
15}
16
17/**
18 * Allocate the lowest unused `term-N` id, starting at `term-1`.
19 * Clients send the id explicitly on `terminal.open` / `terminal.attach`.
20 */
21export function nextTerminalId(existingTerminalIds: ReadonlyArray<string>): string {
22 const usedIds = new Set(existingTerminalIds);
23 let nextIndex = 1;
24 while (usedIds.has(`term-${nextIndex}`)) {
25 nextIndex += 1;
26 }
27
28 return `term-${nextIndex}`;
29}