Skip to main content
The @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.
Uses differential rendering - only redraws changed lines.

showOverlay

Show an overlay component.
Component
required
Component to show as overlay
OverlayOptions
Anchor Positions:
  • '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.
Return { 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: false
function
required
Invalidate cached render state (called on theme changes)

Example: Custom Component

Focusable Interface

Components that accept keyboard input should implement Focusable:
When focused, the component should emit CURSOR_MARKER at the cursor position:

Terminal Interface

The Terminal 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.
Supports iTerm2 and Kitty image protocols with automatic fallback.

detectCapabilities

Detect terminal capabilities.

Performance

Differential Rendering

TUI uses differential rendering to minimize terminal writes:
  1. Component renders to string array
  2. TUI compares with previous frame
  3. Only changed lines are redrawn
  4. Cursor positioned efficiently

Render Caching

Components can cache render output:

Example: Full Application