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

terminalLabels.ts

28 lines851 Bytes

1export function terminalNumber(terminalId: string): string | undefined {
2 return /^term(?:inal)?-(\d+)(?:-[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12})?$/i.exec(
3 terminalId,
4 )?.[1];
5}
6​
7export function getTerminalLabel(terminalId: string): string {
8 const numericSuffix = terminalNumber(terminalId);
9 if (numericSuffix) {
10 return `Terminal ${numericSuffix}`;
11 }
12​
13 return terminalId;
15​
16/** Append a UUID when metadata is unavailable to avoid reusing unseen host sessions. */
17export function nextTerminalId(
18 existingTerminalIds: ReadonlyArray<string>,
19 uniqueSuffix?: string,
20): string {
21 const usedNumbers = new Set(existingTerminalIds.map(terminalNumber));
22 let nextIndex = 1;
23 while (usedNumbers.has(String(nextIndex))) {
24 nextIndex += 1;
25 }
26​
27 return `term-${nextIndex}${uniqueSuffix === undefined ? "" : `-${uniqueSuffix}`}`;