Files
t3code/packages/shared/src/terminalLabels.ts
b3e8c0334b T3 Code Mobile [WIP] (#2013)
Co-authored-by: codex <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
2026-05-29 17:00:54 -07:00

41 lines
1.3 KiB
TypeScript

import type { TerminalSummary } from "@t3tools/contracts";
/** Human-readable label for a terminal tab; matches mobile and web sidebars. */
export function getTerminalLabel(terminalId: string): string {
const numericSuffix = /^term(?:inal)?-(\d+)$/i.exec(terminalId)?.[1];
if (numericSuffix) {
return `Terminal ${numericSuffix}`;
}
return terminalId;
}
/** Prefer server summary label when present; otherwise fall back to `getTerminalLabel`. */
export function resolveTerminalSessionLabel(
terminalId: string,
summary: Pick<TerminalSummary, "label"> | null | undefined,
): string {
const trimmed = summary?.label?.trim();
if (trimmed && trimmed.length > 0) {
return trimmed;
}
return getTerminalLabel(terminalId);
}
/**
* Client-side terminal id allocator. Ids are ALWAYS chosen by the client and sent explicitly
* on every `terminal.open` / `terminal.attach` call — the server never allocates.
*
* Returns the lowest unused `term-N` id (starting at `term-1`), skipping any ids already in
* `existingTerminalIds`.
*/
export function nextTerminalId(existingTerminalIds: ReadonlyArray<string>): string {
const usedIds = new Set(existingTerminalIds.filter((id) => id.trim().length > 0));
let nextIndex = 1;
while (usedIds.has(`term-${nextIndex}`)) {
nextIndex += 1;
}
return `term-${nextIndex}`;
}