A readline replacement written in Rust
Go to file
sholderbach efbbd2f766 Pedantic clippy and code style polish
- Terminate with semicolon, where return type `()`
- Invert some conditionals for readability
- `match` to `if let` if binary
- pass by value vs by reference
- `must_use` attribute where applicable (builder patterns)
- make internal `Mode` enum explicitly `ViMode`
- Docstring fixes
- Format explicit identifiers in docs
2022-03-14 20:56:14 +01:00
.github Add issue templates (#235) 2022-01-02 20:24:47 +01:00
src Pedantic clippy and code style polish 2022-03-14 20:56:14 +01:00
.gitignore vi mode commands (#211) 2021-12-26 18:13:44 +00:00
Cargo.lock Better crossterm fix (#329) 2022-03-01 06:32:14 -05:00
Cargo.toml Better crossterm fix (#329) 2022-03-01 06:32:14 -05:00
LICENSE Update LICENSE (#163) 2021-10-07 06:49:16 +13:00
README.md Small fix to document need to configure completion (#325) 2022-02-24 20:41:45 +01:00
TODO.txt Bump version (#152) 2021-09-24 07:40:32 +12:00
UX_TESTING.md Manual checklist to test the UX (#105) 2021-07-17 23:51:58 +02:00

A readline replacement written in Rust

GitHub Crates.io docs.rs CI status Discord Twitch Status

Introduction

Reedline is a project to create a readline-style crate for Rust that supports many of the modern conveniences of CLIs, including syntax highlighting, completions, multiline support, Unicode support, and more.

Examples

Basic example

// Create a default reedline object to handle user input

use reedline::{DefaultPrompt, Reedline, Signal};

fn main() {
    let mut line_editor = Reedline::create()?;
    let prompt = DefaultPrompt::default();

    loop {
        let sig = line_editor.read_line(&prompt).unwrap();
        match sig {
            Signal::Success(buffer) => {
                println!("We processed: {}", buffer);
            }
            Signal::CtrlD | Signal::CtrlC => {
                println!("\nAborted!");
                break;
            }
            Signal::CtrlL => {
                line_editor.clear_screen().unwrap();
            }
        }
    }
}

Integrate with custom Keybindings

// Configure reedline with custom keybindings

//Cargo.toml
//  [dependencies]
//  crossterm = "*"

use {
  crossterm::event::{KeyCode, KeyModifiers},
  reedline::{default_emacs_keybindings, EditCommand, Reedline},
};

let mut keybindings = default_emacs_keybindings();
keybindings.add_binding(
  KeyModifiers::ALT,
  KeyCode::Char('m'),
  vec![EditCommand::BackspaceWord],
);

let mut line_editor = Reedline::create()?.with_keybindings(keybindings);

Integrate with custom History

// Create a reedline object with history support, including history size limits

use reedline::{FileBackedHistory, Reedline};

let history = Box::new(
  FileBackedHistory::with_file(5, "history.txt".into())
    .expect("Error configuring history with file"),
);
let mut line_editor = Reedline::create()?
  .with_history(history)
  .expect("Error configuring reedline with history");

Integrate with custom Highlighter

// Create a reedline object with highlighter support

use reedline::{ExampleHighlighter, Reedline};

let commands = vec![
  "test".into(),
  "hello world".into(),
  "hello world reedline".into(),
  "this is the reedline crate".into(),
];
let mut line_editor =
Reedline::create()?.with_highlighter(Box::new(ExampleHighlighter::new(commands)));

Integrate with custom tab completion

// Create a reedline object with tab completions support

use reedline::{DefaultCompleter, Reedline, CompletionMenu};

let commands = vec![
  "test".into(),
  "hello world".into(),
  "hello world reedline".into(),
  "this is the reedline crate".into(),
];
let completer = Box::new(DefaultCompleter::new_with_wordlen(commands.clone(), 2));
// Use the interactive menu to select options from the completer
let completion_menu = Box::new(CompletionMenu::default());

let mut line_editor = Reedline::create()?.with_completer(completer).with_menu(completion_menu);

Integrate with custom Hinter

// Create a reedline object with in-line hint support

//Cargo.toml
//  [dependencies]
//  nu-ansi-term = "*"

use {
  nu_ansi_term::{Color, Style},
  reedline::{DefaultHinter, Reedline},
};

let mut line_editor = Reedline::create()?.with_hinter(Box::new(
  DefaultHinter::default()
  .with_style(Style::new().italic().fg(Color::LightGray)),
));

Integrate with custom line completion Validator

// Create a reedline object with line completion validation support

use reedline::{DefaultValidator, Reedline};

let validator = Box::new(DefaultValidator);

let mut line_editor = Reedline::create()?.with_validator(validator);

Integrate with custom Edit Mode

// Create a reedline object with custom edit mode

use reedline::{EditMode, Reedline};

let mut line_editor = Reedline::create()?.with_edit_mode(
  EditMode::ViNormal, // or EditMode::Emacs or EditMode::ViInsert
);

Are we prompt yet? (Development status)

This crate is currently under active development in JT's live-coding streams. If you want to see a feature, jump by the streams, file an issue or contribute a PR!

  • Basic unicode grapheme aware cursor editing.
  • Configurable prompt
  • Basic EMACS-style editing shortcuts.
  • Configurable keybindings.
  • Basic system integration with clipboard or optional stored history file.
  • Content aware highlighting.
  • Autocompletion.
  • Undo support.
  • Multiline aware editing with line completion validation.

For a more detailed roadmap check out TODO.txt.

Join the vision discussion in the vision milestone list by contributing suggestions or voting.

Alternatives

For currently more mature Rust line editing check out: