Termwright
A Playwright-like automation framework for terminal TUI applications.
Termwright enables AI agents and integration tests to interact with and observe terminal user interfaces by wrapping applications in a pseudo-terminal (PTY).
Features
- PTY Wrapping: Spawn and control any terminal application
- Screen Reading: Access text, colors, cursor position, and cell attributes
- Wait Conditions: Wait for text, regex patterns, screen stability, or process exit
- Input Simulation: Send keystrokes, special keys, and control sequences
- Multiple Output Formats: Plain text, JSON (for AI agents), and PNG screenshots
- Box Detection: Detect UI boundaries using box-drawing characters
- Framework Agnostic: Works with any TUI framework (ratatui, crossterm, ncurses, etc.)
Installation
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["rt-multi-thread", "macros"] }
Or install the CLI:
Quick Start
Library Usage
use *;
async
CLI Usage
Capture terminal output as text:
Take a screenshot of a TUI application:
Get JSON output for AI processing:
CLI Reference
termwright run
Run a command and capture its output.
termwright run [OPTIONS] -- <COMMAND> [ARGS]...
Options:
--cols <COLS> Terminal width [default: 80]
--rows <ROWS> Terminal height [default: 24]
--wait-for <TEXT> Wait for this text to appear before capturing
--delay <MS> Delay in milliseconds before capturing [default: 500]
--format <FORMAT> Output format: text, json, json-compact [default: text]
--timeout <SECS> Timeout for wait conditions [default: 30]
termwright screenshot
Take a PNG screenshot of a terminal application.
termwright screenshot [OPTIONS] -- <COMMAND> [ARGS]...
Options:
--cols <COLS> Terminal width [default: 80]
--rows <ROWS> Terminal height [default: 24]
--wait-for <TEXT> Wait for this text to appear before capturing
--delay <MS> Delay in milliseconds before capturing [default: 500]
-o, --output <PATH> Output file path (defaults to stdout)
--font <NAME> Font name for rendering
--font-size <SIZE> Font size in pixels [default: 14]
--timeout <SECS> Timeout for wait conditions [default: 30]
API Overview
Terminal
The main entry point for controlling terminal applications:
let term = builder
.size
.spawn
.await?;
// Input
term.type_str.await?;
term.send_key.await?;
term.enter.await?; // Shorthand for Enter key
// Screen access
let screen = term.screen.await;
// Wait conditions
term.expect.timeout.await?;
term.wait_exit.await?;
// Screenshots
term.screenshot.await.save?;
Screen
Query the terminal screen state:
let screen = term.screen.await;
// Text access
let text = screen.text;
let line = screen.line;
assert!;
// Cell-level access
let cell = screen.cell;
println!;
// Cursor position
let cursor = screen.cursor;
println!;
// Region extraction
let region = screen.region;
// Pattern matching
if let Some = screen.find_text
// Box detection (UI boundaries)
let boxes = screen.detect_boxes;
// Output formats
println!; // Pretty JSON
println!; // Compact JSON
Keys
Available key types for input:
Char // Regular characters
Enter // Enter/Return
Tab // Tab
Escape // Escape
Backspace // Backspace
Up, Down, Left, Right // Arrow keys
Home, End
PageUp, PageDown
Insert, Delete
F..F // Function keys
Ctrl // Ctrl combinations
Alt // Alt combinations
Requirements
- Rust 1.85.0 or later (Edition 2024)
- macOS or Linux (Windows not supported)
- For screenshots: A monospace font (uses system fonts via font-kit)
Use Cases
- AI Agents: Enable LLMs to observe and interact with terminal UIs via JSON output
- Integration Testing: Automated testing of TUI applications
- Documentation: Generate screenshots for documentation
- Accessibility: Extract text content from visual terminal applications
License
MIT License - see LICENSE for details.