Documentation Index
Fetch the complete documentation index at: https://mintlify.com/jaypopat/cf_ai_duet/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Theterminal package provides a shared terminal session using PTY (pseudo-terminal) with VT10x emulation. Multiple clients can connect to the same terminal and see synchronized output.
Type
Terminal
Wraps a PTY with VT10x terminal emulation and subscriber management.Functions
New
Creates a new terminal instance.Terminal width in columns (defaults to 80 if < 1)
Terminal height in rows (defaults to 24 if < 1)
Working directory for shell (defaults to “/app” if empty)
Start
Starts the shell process and begins reading output.- Creates VT10x terminal emulator
- Spawns shell process (respects
$SHELLenv var, defaults to/bin/sh) - Sets environment:
TERM=xterm-256color - Starts background read loop
Write
Sends input to the terminal (keyboard input, etc.).Raw input data to send to PTY
Render
Renders the current terminal state to a string with ANSI codes.- Caching: Returns cached render if terminal hasn’t changed
- Colors: Full 256-color ANSI support
- Cursor: Visual cursor using reverse video effect
- Optimization: Run-length encoding for color sequences
Resize
Resizes the terminal dimensions.New width in columns
New height in rows
- Updates VT10x emulator size
- Sends
SIGWINCHviapty.Setsize() - Invalidates render cache
Close
Closes the terminal and cleans up resources.- Closes all subscriber channels
- Closes PTY file descriptor
- Kills shell process
Size
Returns current terminal dimensions.Subscription Pattern
The terminal supports multi-client subscriptions for real-time updates.Subscribe
Creates a channel for receiving update notifications.Unsubscribe
Removes a subscription channel.Channel returned by Subscribe()
Implementation Details
Read Loop
The backgroundreadLoop() goroutine:
- Reads from PTY (4KB buffer)
- Writes to VT10x emulator
- Marks terminal as dirty
- Broadcasts to all subscribers
- Exits when shell process terminates
Render Optimization
Color Mapping
- Colors 0-7: Standard ANSI (30-37, 40-47)
- Colors 8-15: Bright ANSI (90-97, 100-107)
- Colors 16-255: Extended palette (38;5;n, 48;5;n)
Thread Safety
All public methods are thread-safe:mu sync.Mutexprotects terminal statesubMu sync.RWMutexprotects subscriber map