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

terminalLabels.ts

44 lines1.2 KB

1import type { TerminalSummary } from "@t3tools/contracts";
2​
3function terminalNumber(terminalId: string): string | undefined {
4 return /^term(?:inal)?-(\d+)(?:-[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12})?$/i.exec(
5 terminalId,
6 )?.[1];
7}
8​
9export function getTerminalLabel(terminalId: string): string {
10 const numericSuffix = terminalNumber(terminalId);
11 if (numericSuffix) {
12 return `Terminal ${numericSuffix}`;
13 }
14​
15 return terminalId;
17​
18export function resolveTerminalSessionLabel(
19 terminalId: string,
20 summary: Pick<TerminalSummary, "label"> | null | undefined,
21): string {
22 const trimmed = summary?.label?.trim();
23 if (trimmed && trimmed.length > 0) {
24 return trimmed;
25 }
26 return getTerminalLabel(terminalId);
28​
29/**
30 * When metadata is unavailable, callers append a UUID so an unseen host session
31 * cannot be reused accidentally.
32 */
33export function nextTerminalId(
34 existingTerminalIds: ReadonlyArray<string>,
35 uniqueSuffix?: string,
36): string {
37 const usedNumbers = new Set(existingTerminalIds.map(terminalNumber));
38 let nextIndex = 1;
39 while (usedNumbers.has(String(nextIndex))) {
40 nextIndex += 1;
41 }
42​
43 return `term-${nextIndex}${uniqueSuffix === undefined ? "" : `-${uniqueSuffix}`}`;