Expand description
§tui
A lightweight, composable terminal UI library for building full-screen CLI apps.
§Core Primitives
Component— trait for reusable widgets with event handling and renderingEvent— unified input events (key, paste, mouse, tick, resize)Frame— rendered output: lines + cursor positionLayout— vertical section composition into aFrameRenderer— efficient diff-based terminal renderer (feature:runtime)TerminalSession— raw-mode lifecycle management (feature:runtime)
§Quick Start
The library provides composable building blocks — your app owns its event loop and state machine.
use tui::{Component, Event, Frame, KeyCode, Line, ViewContext};
// Define your app state and use Component for child widgets
struct MyWidget { count: i32 }
impl Component for MyWidget {
type Message = ();
async fn on_event(&mut self, event: &Event) -> Option<Vec<()>> {
if let Event::Key(key) = event {
match key.code {
KeyCode::Char('j') => self.count += 1,
KeyCode::Char('k') => self.count -= 1,
_ => return None,
}
}
Some(vec![])
}
fn render(&mut self, _ctx: &ViewContext) -> Frame {
Frame::new(vec![Line::new(format!("Count: {}", self.count))])
}
}Convert crossterm events with Event::try_from(crossterm_event) — it filters key releases and maps resize events automatically.
§Built-in Widgets
Panel— bordered containerForm,TextField,NumberField— form inputsCheckbox,RadioSelect,MultiSelect— selection controlsSelectList— scrollable list with selectionSpinner— animated progress indicatorCombobox— fuzzy-searchable picker (feature:picker)FocusRing— Tab/BackTab focus traversal
§Feature Flags
| Feature | Description | Default |
|---|---|---|
syntax | Syntax highlighting via syntect | yes |
picker | Fuzzy combobox picker | yes |
testing | Test utilities (TestTerminal, render_component) | no |
Disable defaults for lower-level use:
[dependencies]
tui = { version = "0.1", default-features = false }§License
MIT
Modules§
Structs§
- Checkbox
- Boolean toggle rendered as
[x]/[ ]. - Combobox
- Cursor
- Logical cursor position within a component’s rendered output.
- Diff
Line - A single line in a diff, tagged with its change type.
- Diff
Preview - A preview of changed lines for an edit operation.
- Focus
Ring - Tracks which child in a list of focusable items is currently focused.
- Form
- A multi-field form rendered as a tabbed pane.
- Form
Field - A single field within a
Form. - Frame
- Logical component output: lines plus cursor state.
- Fuzzy
Matcher - KeyEvent
- Represents a key event.
- KeyEvent
State - Represents extra state about the key event.
- KeyModifiers
- Represents key modifiers (shift, control, alt, etc.).
- Layout
- Stacks content sections vertically with automatic cursor offset tracking.
- Line
- A single line of styled terminal output, composed of
Spans. - Mouse
Event - Represents a mouse event.
- Multi
Select - Multi-select from a list of options, rendered as checkboxes with a cursor.
- Number
Field - Numeric input field supporting integers or floats.
- Panel
- A bordered panel for wrapping content blocks with title/footer chrome.
- Radio
Select - Single-select from a list of options, rendered as radio buttons.
- Renderer
- Pure TUI renderer with frame diffing and terminal state management.
- Select
List - Select
Option - A single option in a selection list.
- Span
- A contiguous run of text sharing a single
Style. - Spinner
- Split
Diff Cell - One side of a split diff row.
- Split
Diff Row - A row in a side-by-side diff, pairing an old (left) line with a new (right) line.
- Style
- Text styling: foreground/background colors and attributes (bold, italic, etc.).
- Syntax
Highlighter - Unified syntax-highlighting facade.
- Terminal
Session - Text
Field - Single-line text input with cursor tracking and navigation.
- Theme
- Full resolved theme for TUI rendering.
- View
Context - Environment passed to render methods: terminal size, theme.
Enums§
- Color
- Represents a color.
- Crossterm
Event - Represents an event.
- DiffTag
- Tag indicating the kind of change a diff line represents.
- Event
- Events that a [
Widget] can handle. - Focus
Outcome - The result of
FocusRing::handle_key. - Form
Field Kind - The widget type backing a
FormField. - Form
Message - Messages emitted by
Forminput handling. - KeyCode
- Represents a key.
- KeyEvent
Kind - Represents a keyboard event kind.
- Mouse
Capture - Mouse
Event Kind - A mouse event kind.
- Picker
Key - Picker
Message - Generic message type for picker components.
- Renderer
Command - Select
List Message - Theme
Build Error
Constants§
- BORDER_
H_ PAD - Width consumed by left (“│ “) and right (” │“) borders.
- BRAILLE_
FRAMES - GUTTER_
WIDTH - SEPARATOR
- SEPARATOR_
WIDTH
Traits§
- Component
- A component that can process events and emit typed messages.
- Searchable
- Select
Item
Functions§
- classify_
key - display_
width_ text - highlight_
diff - Render a diff preview with syntax-highlighted context/removed/added lines.
- merge
- Merge two event outcomes.
None(ignored) yields to the other. Messages are concatenated in order. - pad_
text_ to_ width - Pads
textwith trailing spaces to reachtarget_widthdisplay columns. Returns the original text unchanged if it already meets or exceeds the target. - render_
diff - Renders a diff preview, choosing split or unified based on terminal width and whether the diff has removals.
- render_
markdown - soft_
wrap_ line - spawn_
terminal_ event_ task - split_
blank_ panel - split_
render_ cell - terminal_
size - truncate_
line - Truncates a styled line to fit within
max_widthdisplay columns. - truncate_
text - Truncates text to fit within
max_widthdisplay columns, appending “…” if truncated. Returns the original string borrowed when no truncation is needed. - wrap_
selection - Wrapping navigation helper for selection indices.
deltaof -1 moves up, +1 moves down, wrapping at boundaries.