Crate reedline[][src]

Expand description

reedline \|/

A readline replacement written in Rust

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.

Basic example

// Create a default reedline object to handle user input

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

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

 loop {
     let sig = line_editor.read_line(&prompt);
     match sig {
         Ok(Signal::Success(buffer)) => {
             println!("We processed: {}", buffer);
         }
         Ok(Signal::CtrlD) | Ok(Signal::CtrlC) => {
             let _ = line_editor.print_crlf();
             break;
         }
         Ok(Signal::CtrlL) => {
             line_editor.clear_screen();
         }
         x => {
             println!("Event: {:?}", x);
         }
     }
 }

Integrate with custom Keybindings

// Configure reedline with custom keybindings

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

use std::io;
use {
  crossterm::event::{KeyCode, KeyModifiers},
  reedline::{default_emacs_keybindings, EditCommand, Reedline, Emacs, ReedlineEvent},
};

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

let mut line_editor = Reedline::create()?.with_edit_mode(edit_mode);

Integrate with custom History

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

use std::io;
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 std::io;
use reedline::{DefaultHighlighter, 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(DefaultHighlighter::new(commands)));

Integrate with custom Tab-Handler

// Create a reedline object with tab completions support

use std::io;
use reedline::{DefaultCompleter, DefaultCompletionActionHandler, Reedline};

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));

let mut line_editor = Reedline::create()?.with_completion_action_handler(Box::new(
  DefaultCompletionActionHandler::default().with_completer(completer),
));

Integrate with custom Hinter

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

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

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

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));

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

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:

Structs

A default completer that can detect keywords

A simple handler that will do a cycle-based rotation through the options given by the Completer

A simple, example highlighter that shows how to highlight keywords

A default example hinter that use the completions or the history to show a hint to the user

Simple two-line Prompt displaying the current working directory and the time above the entry line.

A default validator which checks for mismatched quotes

This parses the incoming Events like a emacs style-editor

Stateful history that allows up/down-arrow browsing with an internal cursor.

A representation of the history search

Line editor engine

A span of source code, with positions in bytes

A representation of a buffer with styling, used for doing syntax highlighting

This parses incomming input Events like a Vi-Style editor

Enums

Editing actions which can be mapped to key bindings.

Modes that the prompt can be in

The current success/failure of the history search

The vi-specific modes that the prompt can be in

Reedline supported actions.

Valid ways how [Reedline::read_line()] can return

Whether or not the validation shows the input was complete

Constants

Default size of the FileBackedHistory used when calling FileBackedHistory::default()

Statics

The default color for the prompt

The default prompt indicator

Traits

The handler for when the user begins a completion action, often using the tab key This handler will then present the options to the user, allowing them to navigate the options and pick the completion they want

A trait that defines how to convert a line and position to a list of potential completions in that position.

Define the style of parsing for the edit events Available default options:

The syntax highlighting trait. Implementers of this trait will take in the current string and then return a StyledText object, which represents the contents of the original line as styled strings

A trait that’s responsible for returning the hint for the current line and position Hints are often shown in-line as part of the buffer, showing the user text they can accept or ignore

The trait that handles history activity, which includes the history buffer and navigating the history

API to provide a custom prompt.

The syntax validation trait. Implementers of this trait will check to see if the current input is incomplete and spans multiple lines

Functions

Returns the current default emacs keybindings