| name | opentui-operative |
| description | OpenTUI terminal UI library reference. Use when working with @opentui/core, terminal UIs, renderables, Yoga layouts, or Zig-native rendering. |
| when_to_use | When building or debugging a terminal UI with @opentui/core — renderable composition, Yoga layout issues, or anything touching the Zig-native rendering layer. |
| user-invocable | false |
OpenTUI Operative
Comprehensive reference for building terminal UIs with OpenTUI (@opentui/core).
Source: https://opentui.com/docs/ — all 29 documentation pages.
Trigger
Use when: user mentions "OpenTUI", "TUI", "terminal UI", "@opentui/core", renderables, or works on files importing from @opentui/core. No paths: glob is set — OpenTUI code has no distinctive file extension (it's ordinary .ts/.tsx importing from the package), so keyword/import-based triggering is more reliable than a path pattern here.
Role
You are an expert in OpenTUI — a TypeScript library for building rich terminal interfaces with Yoga-powered flexbox layouts and Zig-native rendering. You know every API surface, every gotcha, and every pattern. You write correct OpenTUI code on the first attempt.
1. Quick Start
Requires Bun.
bun add @opentui/core
import { createCliRenderer, Text } from "@opentui/core"
const renderer = await createCliRenderer({ exitOnCtrlC: true })
renderer.root.add(Text({ content: "Hello, OpenTUI!", fg: "#00FF00" }))
Run with bun index.ts. Press Ctrl+C to exit.
2. Renderer
The CliRenderer drives everything — terminal output, input events, render loop, and context for renderables.
Creation
import { createCliRenderer } from "@opentui/core"
const renderer = await createCliRenderer({
exitOnCtrlC: true,
targetFps: 30,
})
The factory:
- Loads the native Zig rendering library
- Configures terminal (mouse, keyboard protocol, alternate screen)
- Returns an initialised
CliRenderer
Configuration Options
| Option | Type | Default | Description |
|---|
exitOnCtrlC | boolean | true | Destroy renderer on Ctrl+C |
exitSignals | NodeJS.Signals[] | — | Signals that trigger cleanup |
targetFps | number | 30 | Target FPS for render loop |
maxFps | number | 60 | Max FPS for immediate re-renders |
useMouse | boolean | true | Enable mouse input/tracking |
autoFocus | boolean | true | Focus nearest focusable on left click |
enableMouseMovement | boolean | true | Track mouse movement (not just clicks) |
useAlternateScreen | boolean | true | Use terminal alternate screen buffer |
consoleOptions | ConsoleOptions | — | Built-in console overlay options |
openConsoleOnError | boolean | true | Auto-open console on errors (dev only) |
onDestroy | () => void | — | Callback on renderer destruction |
Key Properties
| Property | Type | Description |
|---|
root | RootRenderable | Root of the component tree (fills terminal) |
width | number | Current width in columns |
height | number | Current height in rows |
console | TerminalConsole | Built-in console overlay |
keyInput | KeyHandler | Keyboard input handler |
isRunning | boolean | Whether render loop is active |
isDestroyed | boolean | Whether renderer is destroyed |
currentFocusedRenderable | Renderable | null | Currently focused component |
Render Loop Control
Automatic mode (default) — re-renders only when the component tree changes:
const renderer = await createCliRenderer()
renderer.root.add(Text({ content: "Static content" }))
Continuous mode — runs at targetFps:
renderer.start()
renderer.stop()
Live rendering — for animations:
renderer.requestLive()
renderer.dropLive()
Pause/Suspend:
renderer.pause()
renderer.suspend()
renderer.resume()
Events
renderer.on("resize", (width, height) => { })
renderer.on("destroy", () => { })
renderer.on("selection", (selection) => { })
Cursor Control
renderer.setCursorPosition(10, 5, true)
renderer.setCursorStyle("block", true)
renderer.setCursorColor(RGBA.fromHex("#FF0000"))
Cleanup
renderer.destroy()
CRITICAL: Always call destroy() when finished. This restores terminal state (mouse tracking, raw mode, alternate screen). OpenTUI does NOT automatically clean up on process.exit or unhandled errors.
Debug Overlay
renderer.toggleDebugOverlay()
import { DebugOverlayCorner } from "@opentui/core"
renderer.configureDebugOverlay({ enabled: true, corner: DebugOverlayCorner.topRight })
3. Renderables (Imperative API)
Renderables are the building blocks of the UI. Each represents a visual element using Yoga layout engine for positioning.
Creating Renderables
import { TextRenderable, BoxRenderable } from "@opentui/core"
const greeting = new TextRenderable(renderer, {
id: "greeting",
content: "Hello!",
fg: "#00FF00",
})
renderer.root.add(greeting)
Available Renderables
| Class | Description |
|---|
BoxRenderable | Container with border, background, and layout |
TextRenderable | Read-only styled text display |
InputRenderable | Single-line text input |
TextareaRenderable | Multi-line editable text |
SelectRenderable | Dropdown/list selection |
TabSelectRenderable | Horizontal tab selection |
ScrollBoxRenderable | Scrollable container |
ScrollBarRenderable | Standalone scroll bar control |
CodeRenderable | Syntax-highlighted code display |
LineNumberRenderable | Line number gutter |
DiffRenderable | Unified or split diff viewer |
ASCIIFontRenderable | ASCII art font display |
FrameBufferRenderable | Raw framebuffer for custom graphics |
MarkdownRenderable | Markdown renderer |
SliderRenderable | Numeric slider control |
Full per-component API (options, events, gotchas) is in component-reference.md — load it when working with a specific component beyond basic construction.
The Renderable Tree
const container = new BoxRenderable(renderer, {
id: "container",
flexDirection: "column",
padding: 1,
})
const title = new TextRenderable(renderer, { id: "title", content: "My App" })
const body = new TextRenderable(renderer, { id: "body", content: "Content" })
container.add(title)
container.add(body)
renderer.root.add(container)
container.remove("body")
CRITICAL: remove() API
remove(id: string): void — the ONLY signature. Always pass a string ID.
container.remove("body")
container.remove(child.id)
container.remove(child)
Every renderable gets an auto-generated .id from a static counter. If you set id in options, that becomes the ID. Otherwise it's auto-generated. Access via renderable.id.
Finding Renderables
const title = container.getRenderable("title")
const deep = container.findDescendantById("nested-input")
const children = container.getChildren()
Visibility
panel.visible = false
panel.visible = true
Opacity
panel.opacity = 0.5
Z-Index
const overlay = new BoxRenderable(renderer, {
position: "absolute",
zIndex: 100,
})
Translation (Visual Offset)
renderable.translateX = 10
renderable.translateY = -5
Destroying Renderables
renderable.destroy()
container.destroyRecursively()
Lifecycle Methods (Custom Renderables)
class CustomRenderable extends Renderable {
onUpdate(deltaTime: number) { }
onResize(width: number, height: number) { }
onRemove() { }
renderSelf(buffer: OptimizedBuffer, deltaTime: number) { }
}
Live Rendering
const box = new AnimatedBox(renderer, {
live: true,
})
Buffered Rendering
const complex = new BoxRenderable(renderer, {
buffered: true,
renderAfter: (buffer) => {
buffer.fillRect(0, 0, 10, 5, RGBA.fromHex("#FF0000"))
},
})
4. Constructs (Declarative API)
Factory functions that create VNodes — lightweight descriptions of components. VNodes become actual Renderables when added to the tree.
import { Box, Text, Input } from "@opentui/core"
Box(
{ width: 40, height: 10, borderStyle: "rounded", padding: 1 },
Text({ content: "Welcome!" }),
Input({ placeholder: "Enter your name..." }),
)
Available Constructs
ASCIIFont, Box, Code, FrameBuffer, Input, ScrollBox, Select, TabSelect, Text, SyntaxStyle
NOT yet available as constructs (use Renderable API): Textarea, ScrollBar, Slider, Markdown, LineNumber, Diff
Method Chaining on VNodes
VNodes queue method calls — applied after the component is created:
const input = Input({ placeholder: "Name..." })
input.focus()
Delegation
Routes method/property calls to descendant IDs:
import { delegate } from "@opentui/core"
function LabeledInput(props) {
return delegate(
{ focus: `${props.id}-input` },
Box(
{ flexDirection: "row" },
Text({ content: props.label }),
Input({ id: `${props.id}-input`, placeholder: props.placeholder }),
),
)
}
const field = LabeledInput({ id: "name", label: "Name:", placeholder: "..." })
field.focus()
Mixing Renderables and Constructs
const container = new BoxRenderable(renderer, { id: "root", flexDirection: "column" })
container.add(Text({ content: "Title" }), Input({ placeholder: "Type here..." }))
renderer.root.add(container)
5. Layout (Yoga Flexbox)
Flex Direction
{ flexDirection: "column" }
{ flexDirection: "row" }
{ flexDirection: "row-reverse" }
{ flexDirection: "column-reverse" }
Justify Content (Main Axis)
flex-start | flex-end | center | space-between | space-around | space-evenly
Align Items (Cross Axis)
flex-start | flex-end | center | stretch (default) | baseline
Sizing
{ width: 30, height: 10 }
{ width: "100%", height: "50%" }
{ flexGrow: 1, flexShrink: 0 }
{ minWidth: 20, maxHeight: 30 }
Positioning
{ position: "relative" }
{ position: "absolute", left: 10, top: 5 }
Spacing
{ padding: 2 }
{ paddingTop: 1, paddingX: 4 }
{ margin: 1 }
Gap
{ gap: 1 }
Additional resources
Deep-dive detail lives in supporting files, loaded only when needed:
- component-reference.md — full API for every renderable (BoxRenderable through FrameBufferRenderable): options, events, gotchas
- input-and-styling.md — keyboard input, focus management, colours/RGBA, console overlay, environment variables
- advanced-and-testing.md — tree-sitter integration, Solid.js/React framework bindings, common patterns (screen pattern, lifecycle, dynamic updates), mock-renderer testing, the full gotchas list