Files
openchamber/packages/ui/src/lib/terminalInput.ts
T
Bohdan Triapitsyn d4a8c4d2e1 feat(terminal): refactor runtime and add mobile workspace (#2280)
Replace the legacy terminal flow with a shared authenticated WebSocket
runtime used across web, desktop, relay, and mobile surfaces.

- introduce the v3 terminal protocol with scoped attachments, snapshots,
  ordered output, bounded replay history, reconnects, and explicit lifecycle
- harden PTY creation, restart, resize, close, force-kill, idle cleanup,
  shell selection, login mode, environment sanitization, and appearance sync
- add runtime-aware terminal APIs with relay authentication and Electron parity
- add a fullscreen mobile terminal workspace with touch scrolling,
  long-press selection, safe-area controls, quick keys, and Ctrl/Alt input
- add terminal selection attachments, preview detection, project actions,
  shell settings, and localized UI
- harden Ghostty rendering, resize recovery, Unicode handling, block
  characters, line height, and stale-row behavior
- remove the obsolete terminal SSE path and update reverse-proxy guidance
- expand terminal runtime, transport, input, selection, and store coverage
- avoid duplicate web builds when preparing mobile assets in root CI builds
2026-07-17 13:17:21 +03:00

29 lines
1.3 KiB
TypeScript

export type TerminalModifier = 'ctrl' | 'alt';
export type TerminalQuickKey = 'esc' | 'tab' | 'enter' | 'arrow-up' | 'arrow-down' | 'arrow-left' | 'arrow-right';
const sequences: Record<TerminalQuickKey, string> = {
esc: '\u001b', tab: '\t', enter: '\r',
'arrow-up': '\u001b[A', 'arrow-down': '\u001b[B', 'arrow-left': '\u001b[D', 'arrow-right': '\u001b[C',
};
export const terminalSequenceForKey = (key: TerminalQuickKey, modifier: TerminalModifier | null): string => {
if (modifier && key.startsWith('arrow-')) {
const suffix = modifier === 'ctrl' ? '5' : '3';
const direction = { 'arrow-up': 'A', 'arrow-down': 'B', 'arrow-right': 'C', 'arrow-left': 'D' }[key as 'arrow-up' | 'arrow-down' | 'arrow-right' | 'arrow-left'];
return `\u001b[1;${suffix}${direction}`;
}
return sequences[key];
};
export const terminalControlCharacter = (value: string): string | null => {
const character = value[0]?.toUpperCase();
if (!character || character < 'A' || character > 'Z') return null;
return String.fromCharCode(character.charCodeAt(0) & 0b11111);
};
export const applyTerminalModifier = (value: string, modifier: TerminalModifier): string => {
if (!value) return value;
if (modifier === 'ctrl') return terminalControlCharacter(value) ?? value;
return value.length === 1 && value !== '\u001b' ? `\u001b${value}` : value;
};