Components
The Pi-TUI component system provides a declarative API for building terminal user interfaces with differential rendering.Component Interface
All TUI components must implement the Component interface.Methods
(width: number) => string[]
required
Renders the component to an array of strings (one per line) for the given viewport width. Must respect the width constraint.
(data: string) => void
Optional handler for keyboard input when component has focus. Receives raw terminal input data.
() => void
required
Invalidates any cached rendering state. Called when theme changes or when component needs to re-render from scratch.
Properties
boolean
default:"false"
If true, component receives key release events (Kitty protocol). Default is false - release events are filtered out.
Focusable Interface
Components that can receive focus and display a hardware cursor implement the Focusable interface.boolean
required
Set by TUI when focus changes. When true, component should emit
CURSOR_MARKER at the cursor position in its render output.Type Guard
Built-in Components
Container
Container component that renders children sequentially.Example
Text
Displays multi-line text with word wrapping and padding.Parameters
string
default:"''"
Text content to display
number
default:"1"
Left/right padding in columns
number
default:"1"
Top/bottom padding in rows
(text: string) => string
Optional function to apply background color
Example
Box
Container that applies padding and background to all children.Example
Input
Single-line text input with horizontal scrolling.Example
Features
- Emacs-style keybindings (configurable)
- Bracketed paste support
- Kill ring for cut/yank operations
- Undo/redo support
- Word navigation
- Horizontal scrolling for long input
Loader
Animated spinner with customizable message.Example
SelectList
Scrollable list with keyboard navigation.Example
Cursor Marker
Focusable components should emit theCURSOR_MARKER constant at the cursor position when focused is true. The TUI will position the hardware cursor there for proper IME candidate window positioning.
Example
Creating Custom Components
Implement the Component interface and optionally Focusable:Best Practices
- Always respect the width parameter - Lines exceeding terminal width will crash the TUI
- Use
visibleWidth()to measure text - Accounts for ANSI codes and wide characters - Implement efficient caching - Store rendered output and only regenerate when needed
- Call
invalidate()on theme changes - Clear cached output with styled colors - Emit
CURSOR_MARKERwhen focused - Enables proper IME positioning