input_handler
A tiny, reusable input editor for Rust CLI apps. It provides readline-like terminal editing with in-memory and optional on-disk history, arrow-key navigation, backspace/delete handling, and sane defaults, without external C deps beyond libc/termios.
Features
- Line editing: insert, backspace, left/right cursor movement
- History navigation: up/down to traverse previous inputs
- Persistent history (optional): load/save to a file you choose
- Minimal API:
Editorand ergonomic wrapperInputHandler - Raw mode management: enables raw mode and restores it on drop
Installation
Add to your Cargo.toml:
[]
= "0.1"
Quick start
use InputHandler;
With persistent history
use InputHandler;
use PathBuf;
Key bindings
- Enter: submit current line
- Backspace: delete character before cursor
- Left/Right: move cursor
- Up/Down: previous/next history entry
- Ctrl-C: returns an empty string (line is cleared), prints
^C - Ctrl-D: when the buffer is empty, returns the literal string
"exit"
API overview
Exports from the crate root:
Editornew() -> io::Result<Editor>with_history_file(path: PathBuf) -> io::Result<Editor>readline(prompt: &str) -> io::Result<String>
Historynew(max_size: usize) -> Historywith_file(max_size: usize, path: PathBuf) -> Historyadd(command: String)previous() -> Option<&String>/next_command() -> Option<&String>
InputHandler(light wrapper aroundEditor)new() -> io::Result<InputHandler>with_history_file(path: PathBuf) -> io::Result<InputHandler>readline(prompt: &str) -> io::Result<String>
Notes:
- History de-duplicates consecutive identical inputs and ignores empty lines.
- When backed by a file, history is loaded on start and rewritten on each add.
- Raw mode is enabled before reading and restored automatically on drop.
Platform support
This crate uses termios and libc to enter raw mode and read keys. It targets Unix-like systems (Linux, macOS). Windows is not supported out of the box.
Example behavior
- Pressing Up/Down cycles through history. When you reach the newest entry and press Down again, the buffer clears.
- Pressing Ctrl-D with an empty buffer returns
"exit"so you can exit loops easily; otherwise, it is ignored.
License
MIT © Contributors
Repository
https://github.com/Arshdeep54/input_handler