@mariozechner/pi-tui package provides a differential rendering system and utilities for terminal output.
Import
TUI Class
Main rendering engine that manages the screen buffer and input.Constructor
Terminal
required
Terminal interface (use
ProcessTerminal for stdio)Methods
render
Render a component to the terminal.showOverlay
Show an overlay component.Component
required
Component to show as overlay
OverlayOptions
'center'- Center of screen'top-left','top-center','top-right''bottom-left','bottom-center','bottom-right''left-center','right-center'
hideOverlay
Hide an overlay.focus
Focus a component (for keyboard input).addInputListener
Add a global input listener.{ consume: true } to prevent other listeners from receiving the input.
close
Clean up and restore terminal.Component Interface
All components must implement this interface:function
required
Render the component to an array of strings (one per line)
function
Handle keyboard input when focused
boolean
Whether component wants key release events. Default:
falsefunction
required
Invalidate cached render state (called on theme changes)
Example: Custom Component
Focusable Interface
Components that accept keyboard input should implementFocusable:
CURSOR_MARKER at the cursor position:
Terminal Interface
TheTerminal interface abstracts terminal operations:
ProcessTerminal
Default implementation for Node.js stdio:Rendering Utilities
visibleWidth
Calculate visible width of text (handles ANSI codes and Unicode).truncateToWidth
Truncate text to fit width.wrapTextWithAnsi
Wrap text preserving ANSI codes.Image Support
renderImage
Render an image using terminal image protocols.detectCapabilities
Detect terminal capabilities.Performance
Differential Rendering
TUI uses differential rendering to minimize terminal writes:- Component renders to string array
- TUI compares with previous frame
- Only changed lines are redrawn
- Cursor positioned efficiently