Overview
The@mariozechner/pi-tui package is a minimal terminal UI framework designed for building flicker-free interactive CLI applications. It features differential rendering, synchronized output, and a component-based architecture.
Differential Rendering
Three-strategy rendering that only updates what changed
Synchronized Output
Uses CSI 2026 for atomic screen updates with zero flicker
Component-Based
Simple Component interface with render() method
Rich Components
Text, Input, Editor, Markdown, SelectList, Image, and more
Installation
Quick Start
Key Features
Differential Rendering
The TUI intelligently updates only what changed:- First Render: Output all lines without clearing scrollback
- Width Changed: Clear screen and full re-render
- Normal Update: Move cursor to first changed line, clear to end, render changes
\x1b[?2026h … \x1b[?2026l) for flicker-free rendering.
Component Interface
All components implement a simple interface:render() must not exceed the width parameter. Use truncateToWidth() or manual wrapping.
Built-in Components
Layout:Container- Groups child componentsBox- Container with padding and backgroundSpacer- Empty vertical spacing
Text- Multi-line text with word wrappingTruncatedText- Single-line text with truncationMarkdown- Markdown rendering with syntax highlighting
Input- Single-line text inputEditor- Multi-line editor with autocomplete and paste handling
SelectList- Interactive list with keyboard navigationSettingsList- Settings panel with value cycling
Loader- Animated loading spinnerCancellableLoader- Loader with Escape key abortImage- Inline images (Kitty/iTerm2 protocols)
Overlay System
Render components on top of existing content:center, top-left, top-right, bottom-left, bottom-right, top-center, bottom-center, left-center, right-center
Keyboard Input
UsematchesKey() with the Key helper:
- Basic:
Key.enter,Key.escape,Key.tab,Key.space,Key.backspace,Key.delete - Arrows:
Key.up,Key.down,Key.left,Key.right - Modifiers:
Key.ctrl("c"),Key.shift("tab"),Key.alt("left"),Key.ctrlShift("p")
IME Support (CJK Input)
Components with text cursors should implementFocusable for IME support:
CURSOR_MARKER location for CJK IME candidate windows.
Autocomplete
Combine slash commands and file path completion:- Type
/for slash commands - Press
Tabfor file paths - Supports
~/,./,../, and@prefix
Component Examples
Text with Background
Multi-line Editor
Enter- Submit (if not disabled)Shift+Enter,Ctrl+Enter,Alt+Enter- New lineTab- AutocompleteCtrl+K- Delete to end of lineCtrl+W- Delete word backwards
Markdown Rendering
Interactive List
Inline Images
Loading Spinner
Creating Custom Components
Basic Component
Component with Caching
Utilities
Text Width and Truncation
ANSI Code Handling
BothvisibleWidth() and truncateToWidth() correctly handle ANSI escape codes:
Terminal Abstraction
The TUI works with any object implementing theTerminal interface:
ProcessTerminal- Usesprocess.stdin/stdoutVirtualTerminal- For testing (uses@xterm/headless)
Example: Chat Interface
Seetest/chat-simple.ts in the package for a complete example with:
- Markdown messages with custom backgrounds
- Loading spinner during responses
- Editor with autocomplete and slash commands
- Spacers between messages