Clone this repo:
  1. 3b94c07 Update build rules to rely on new dependency target by Benjamin Brittain · 4 weeks ago master
  2. 47a8e51 add gn rules for rust build changes by Benjamin Brittain · 5 weeks ago
  3. 8e82f6a Add support for Fuchsia by Benjamin Brittain · 6 weeks ago upstream/master
  4. 15de348 Merge pull request #110 from gwenn/cargo-categories by Katsu Kawakami · 10 months ago
  5. 44a43bd Merge pull request #108 from rgardner/add-get-history-const by Katsu Kawakami · 10 months ago

RustyLine

Build Status Build status Clippy Linting Result

Readline implementation in Rust that is based on Antirez' Linenoise

Documentation (Releases)

Documentation (Master)

Supported Platforms

  • Linux
  • Windows
    • cmd.exe
    • Powershell

Note: Powershell ISE is not supported, check issue #56

Build

This project uses Cargo and Rust stable

cargo build --release

Example

extern crate rustyline;

use rustyline::error::ReadlineError;
use rustyline::Editor;

fn main() {
    // `()` can be used when no completer is required
    let mut rl = Editor::<()>::new();
    if let Err(_) = rl.load_history("history.txt") {
        println!("No previous history.");
    }
    loop {
        let readline = rl.readline(">> ");
        match readline {
            Ok(line) => {
                rl.add_history_entry(&line);
                println!("Line: {}", line);
            },
            Err(ReadlineError::Interrupted) => {
                println!("CTRL-C");
                break
            },
            Err(ReadlineError::Eof) => {
                println!("CTRL-D");
                break
            },
            Err(err) => {
                println!("Error: {:?}", err);
                break
            }
        }
    }
    rl.save_history("history.txt").unwrap();
}

crates.io

You can use this package in your project by adding the following to your Cargo.toml:

[dependencies]
rustyline = "1.0.0"

Features

Actions

For all modes:

KeystrokeAction
HomeMove cursor to the beginning of line
EndMove cursor to end of line
LeftMove cursor one character left
RightMove cursor one character right
Ctrl-CInterrupt/Cancel edition
Ctrl-D, Del(if line is not empty) Delete character under cursor
Ctrl-D(if line is empty) End of File
Ctrl-J, Ctrl-M, EnterFinish the line entry
Ctrl-RReverse Search history (Ctrl-S forward, Ctrl-G cancel)
Ctrl-TTranspose previous character with current character
Ctrl-UDelete from start of line to cursor
Ctrl-VInsert any special character without perfoming its associated action (#65)
Ctrl-WDelete word leading up to cursor (using white space as a word boundary)
Ctrl-YPaste from Yank buffer
Ctrl-ZSuspend (unix only)

Emacs mode (default mode)

KeystrokeAction
Ctrl-A, HomeMove cursor to the beginning of line
Ctrl-B, LeftMove cursor one character left
Ctrl-E, EndMove cursor to end of line
Ctrl-F, RightMove cursor one character right
Ctrl-H, BackSpaceDelete character before cursor
Ctrl-I, TabNext completion
Ctrl-KDelete from cursor to end of line
Ctrl-LClear screen
Ctrl-N, DownNext match from history
Ctrl-P, UpPrevious match from history
Ctrl-YPaste from Yank buffer (Meta-Y to paste next yank instead)
Meta-<Move to first entry in history
Meta->Move to last entry in history
Meta-B, Alt-LeftMove cursor to previous word
Meta-CCapitalize the current word
Meta-DDelete forwards one word
Meta-F, Alt-RightMove cursor to next word
Meta-LLower-case the next word
Meta-TTranspose words
Meta-UUpper-case the next word
Meta-YSee Ctrl-Y
Meta-BackSpaceKill from the start of the current word, or, if between words, to the start of the previous word
Meta-0, 1, ..., -Specify the digit to the argument. starts a negative argument.

Readline Emacs Editing Mode Cheat Sheet

Vi command mode

KeystrokeAction
$, EndMove cursor to end of line
.Redo the last text modification
;Redo the last character finding command
,Redo the last character finding command in opposite direction
0, HomeMove cursor to the beginning of line
^Move to the first non-blank character of line
aInsert after cursor
AInsert at the end of line
bMove one word or token left
BMove one non-blank word left
cChange text of a movement command
CChange text to the end of line (equivalent to c$)
dDelete text of a movement command
D, Ctrl-KDelete to the end of the line
eMove to the end of the current word
EMove to the end of the current non-blank word
fMove right to the next occurance of char
FMove left to the previous occurance of char
h, Ctrl-H, BackSpaceMove one character left
l, SpaceMove one character right
Ctrl-LClear screen
iInsert before cursor
IInsert at the beginning of line
+, j, Ctrl-NMove forward one command in history
-, k, Ctrl-PMove backward one command in history
pInsert the yanked text at the cursor (paste)
PInsert the yanked text before the cursor
rReplaces a single character under the cursor (without leaving command mode)
sDelete a single character under the cursor and enter input mode
SChange current line (equivalent to 0c$)
tMove right to the next occurance of char, then one char backward
TMove left to the previous occurance of char, then one char forward
wMove one word or token right
WMove one non-blank word right
xDelete a single character under the cursor
XDelete a character before the cursor
yYank a movement into buffer (copy)

Vi insert mode

KeystrokeAction
Ctrl-H, BackSpaceDelete character before cursor
Ctrl-I, TabNext completion
EscSwitch to command mode

Readline VI Editing Mode Cheat Sheet

Terminal codes (ANSI/VT100)

ToDo

  • Undos
  • Read input with timeout to properly handle single ESC key
  • expose an API callable from C

Wine

$ cargo run --example example --target 'x86_64-pc-windows-gnu'
...
Error: Io(Error { repr: Os { code: 6, message: "Invalid handle." } })
$ wineconsole --backend=curses target/x86_64-pc-windows-gnu/debug/examples/example.exe
...

Terminal checks

$ # current settings of all terminal attributes:
$ stty -a
$ # key bindings:
$ bind -p

Similar projects