#cli #console #ratatui #terminal #tui

tui-prompts

A library for building interactive prompts for ratatui

12 releases

0.3.5 Feb 13, 2024
0.3.4 Jan 30, 2024
0.3.2 Dec 15, 2023
0.3.1 Nov 17, 2023
0.1.1 Jul 11, 2023

#467 in GUI

Download history 2/week @ 2023-11-09 54/week @ 2023-11-16 23/week @ 2023-11-23 20/week @ 2023-11-30 5/week @ 2023-12-07 49/week @ 2023-12-14 21/week @ 2023-12-21 1/week @ 2023-12-28 12/week @ 2024-01-04 2/week @ 2024-01-11 3/week @ 2024-01-18 83/week @ 2024-01-25 18/week @ 2024-02-01 43/week @ 2024-02-08 187/week @ 2024-02-15 312/week @ 2024-02-22

566 downloads per month

MIT/Apache

34KB
658 lines

tui-prompts

Crates.io License Docs.rs Dependency Status Codecov Discord

tui-prompts is a Rust crate that provides prompt widgets for the Ratatui crate. It allows for easy creation of interactive command-line interfaces with various types of prompts. Inspired by https://www.npmjs.com/package/prompts and various other prompt libraries.

Examples

Text Prompt

Code
struct App<'a> {
    username_state: TextState<'a>,
    password_state: TextState<'a>,
    invisible_state: TextState<'a>,
}

impl<'a> App<'a> {
    fn draw_ui<B: Backend>(&mut self, frame: &mut Frame<B>) {
        let (username_area, password_area, invisible_area) = split_layout(frame.size())

        TextPrompt::from("Username")
            .draw(frame, username_area, &mut self.username_state);

        TextPrompt::from("Password")
            .with_render_style(TextRenderStyle::Password)
            .draw(frame, password_area, &mut self.password_state);

        TextPrompt::from("Invisible")
            .with_render_style(TextRenderStyle::Invisible)
            .draw(frame, invisible_area, &mut self.invisible_state);
    }
}

Text Prompt

See the text example for more details.

Soft Wrapping

Text is automatically character wrapped to fit in the render area.

Multi-line

See the multi line example for more details.

Features

  • Text prompt
  • Password prompt
  • Invisible prompt
  • Readline / emacs style Key Bindings
  • Crossterm backend
  • Soft wrapping single lines
  • Multi-line input
  • Scrolling
  • More prompt types:
    • Number
    • Confirm
    • List
    • Toggle
    • Select
    • Multi-select
    • Autocomplete
    • Autocomplete multi-select
    • Date
  • Bracketed paste
  • Validation
  • Default initial value
  • Custom style
  • Themes
  • Custom formatting
  • Backend agnostic keyboard event handling (Termion and Termwiz)
  • Customizable key bindings
  • Handle more advanced multi-key bindings e.g. ^[b and ^[f for start / end of line
  • Prompt chaining

Installation

cargo add ratatui tui-prompts crossterm

Or add the following to your Cargo.toml file:

Key Bindings

Key Action
Home, Ctrl+A Move cursor to beginning of line
End, Ctrl+E Move cursor to end of line
Left, Ctrl+B Move cursor one character left
Right, Ctrl+F Move cursor one character right
Backspace (Delete on Mac), Ctrl+H Delete character before cursor
Delete (Fn+Delete on Mac), Ctrl+D Delete character at cursor
Ctrl+K Delete all characters from the cursor to the end of line
Ctrl+U Delete the entire line
Enter Complete the prompt
Escape, Ctrl+C Abort the prompt

License

Dual-licensed under Apache 2.0 or MIT.

Dependencies

~5–12MB
~115K SLoC