| 1 | import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | |
| 3 | export interface TerminalGridSize { |
| 4 | readonly cols: number; |
| 5 | readonly rows: number; |
| 6 | } |
| 7 | |
| 8 | export interface TerminalUiStateTarget { |
| 9 | readonly environmentId: EnvironmentId; |
| 10 | readonly threadId: ThreadId; |
| 11 | readonly terminalId: string; |
| 12 | } |
| 13 | |
| 14 | const terminalGridSizeCache = new Map<string, TerminalGridSize>(); |
| 15 | |
| 16 | function terminalUiStateKey(target: TerminalUiStateTarget): string { |
| 17 | return `${target.environmentId}:${target.threadId}:${target.terminalId}`; |
| 18 | } |
| 19 | |
| 20 | export function getCachedTerminalGridSize(target: TerminalUiStateTarget): TerminalGridSize | null { |
| 21 | return terminalGridSizeCache.get(terminalUiStateKey(target)) ?? null; |
| 22 | } |
| 23 | |
| 24 | export function cacheTerminalGridSize( |
| 25 | target: TerminalUiStateTarget, |
| 26 | size: TerminalGridSize, |
| 27 | ): TerminalGridSize { |
| 28 | const normalized = { |
| 29 | cols: Math.max(1, Math.floor(size.cols)), |
| 30 | rows: Math.max(1, Math.floor(size.rows)), |
| 31 | }; |
| 32 | terminalGridSizeCache.set(terminalUiStateKey(target), normalized); |
| 33 | return normalized; |
| 34 | } |
| 35 | |
| 36 | export function resetTerminalUiStateCaches() { |
| 37 | terminalGridSizeCache.clear(); |
| 38 | } |