|
| 1 | +import { TerminalRenderer } from '@geometra/renderer-terminal' |
| 2 | +import type { ComputedLayout } from 'textura' |
| 3 | +import type { UIElement } from '@geometra/core' |
| 4 | +import WebSocket from 'ws' |
| 5 | + |
| 6 | +interface ServerFrame { |
| 7 | + type: 'frame' |
| 8 | + layout: ComputedLayout |
| 9 | + tree: UIElement |
| 10 | +} |
| 11 | + |
| 12 | +interface ServerPatch { |
| 13 | + type: 'patch' |
| 14 | + patches: Array<{ path: number[]; x?: number; y?: number; width?: number; height?: number }> |
| 15 | +} |
| 16 | + |
| 17 | +type ServerMessage = ServerFrame | ServerPatch | { type: string } |
| 18 | + |
| 19 | +function applyPatches(layout: ComputedLayout, patches: ServerPatch['patches']): void { |
| 20 | + for (const patch of patches) { |
| 21 | + let node: ComputedLayout = layout |
| 22 | + for (const idx of patch.path) { |
| 23 | + node = node.children[idx]! |
| 24 | + } |
| 25 | + if (patch.x !== undefined) node.x = patch.x |
| 26 | + if (patch.y !== undefined) node.y = patch.y |
| 27 | + if (patch.width !== undefined) node.width = patch.width |
| 28 | + if (patch.height !== undefined) node.height = patch.height |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export interface ViewOptions { |
| 33 | + /** WebSocket URL to connect to. */ |
| 34 | + url: string |
| 35 | + /** Terminal columns (default: stdout columns). */ |
| 36 | + width?: number |
| 37 | + /** Terminal rows (default: stdout rows). */ |
| 38 | + height?: number |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Connect to a Geometra server WebSocket and render in the terminal. |
| 43 | + * Returns a cleanup function to close the connection. |
| 44 | + */ |
| 45 | +export function viewInTerminal(options: ViewOptions): { close: () => void } { |
| 46 | + const renderer = new TerminalRenderer({ |
| 47 | + width: options.width, |
| 48 | + height: options.height, |
| 49 | + }) |
| 50 | + |
| 51 | + let layout: ComputedLayout | null = null |
| 52 | + let tree: UIElement | null = null |
| 53 | + |
| 54 | + const ws = new WebSocket(options.url) |
| 55 | + |
| 56 | + ws.on('message', (data) => { |
| 57 | + try { |
| 58 | + const msg: ServerMessage = JSON.parse(data.toString()) |
| 59 | + |
| 60 | + if (msg.type === 'frame') { |
| 61 | + const frame = msg as ServerFrame |
| 62 | + layout = frame.layout |
| 63 | + tree = frame.tree |
| 64 | + renderer.render(layout, tree) |
| 65 | + } else if (msg.type === 'patch' && layout && tree) { |
| 66 | + applyPatches(layout, (msg as ServerPatch).patches) |
| 67 | + renderer.render(layout, tree) |
| 68 | + } |
| 69 | + } catch { |
| 70 | + // ignore parse errors |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + ws.on('error', (err) => { |
| 75 | + process.stderr.write(`\x1b[31mConnection error: ${err.message}\x1b[0m\n`) |
| 76 | + }) |
| 77 | + |
| 78 | + ws.on('close', () => { |
| 79 | + renderer.destroy() |
| 80 | + process.stderr.write('Disconnected.\n') |
| 81 | + }) |
| 82 | + |
| 83 | + return { |
| 84 | + close() { |
| 85 | + ws.close() |
| 86 | + renderer.destroy() |
| 87 | + }, |
| 88 | + } |
| 89 | +} |
0 commit comments