mirror of
https://github.com/nushell/reedline.git
synced 2024-09-11 15:55:50 +03:00
examples updated (#493)
Puts the minimal examples from the documentation as separate example executables to play around with
This commit is contained in:
parent
3042289df4
commit
835d2e9f25
26
examples/basic.rs
Normal file
26
examples/basic.rs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// Create a default reedline object to handle user input
|
||||||
|
// cargo run --example basic
|
||||||
|
//
|
||||||
|
// You can browse the local (non persistent) history using Up/Down or Ctrl n/p.
|
||||||
|
|
||||||
|
use reedline::{DefaultPrompt, Reedline, Signal};
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
// Create a new Reedline engine with a local History that is not synchronized to a file.
|
||||||
|
let mut line_editor = Reedline::create();
|
||||||
|
let prompt = DefaultPrompt::default();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let sig = line_editor.read_line(&prompt)?;
|
||||||
|
match sig {
|
||||||
|
Signal::Success(buffer) => {
|
||||||
|
println!("We processed: {}", buffer);
|
||||||
|
}
|
||||||
|
Signal::CtrlD | Signal::CtrlC => {
|
||||||
|
println!("\nAborted!");
|
||||||
|
break Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
58
examples/completions.rs
Normal file
58
examples/completions.rs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
// Create a reedline object with tab completions support
|
||||||
|
// cargo run --example completions
|
||||||
|
//
|
||||||
|
// "t" [Tab] will allow you to select the completions "test" and "this is the reedline crate"
|
||||||
|
// [Enter] to select the chosen alternative
|
||||||
|
|
||||||
|
use reedline::{
|
||||||
|
default_emacs_keybindings, ColumnarMenu, DefaultCompleter, DefaultPrompt, Emacs, KeyCode,
|
||||||
|
KeyModifiers, Keybindings, Reedline, ReedlineEvent, ReedlineMenu, Signal,
|
||||||
|
};
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
fn add_menu_keybindings(keybindings: &mut Keybindings) {
|
||||||
|
keybindings.add_binding(
|
||||||
|
KeyModifiers::NONE,
|
||||||
|
KeyCode::Tab,
|
||||||
|
ReedlineEvent::UntilFound(vec![
|
||||||
|
ReedlineEvent::Menu("completion_menu".to_string()),
|
||||||
|
ReedlineEvent::MenuNext,
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
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(ColumnarMenu::default().with_name("completion_menu"));
|
||||||
|
|
||||||
|
let mut keybindings = default_emacs_keybindings();
|
||||||
|
add_menu_keybindings(&mut keybindings);
|
||||||
|
|
||||||
|
let edit_mode = Box::new(Emacs::new(keybindings));
|
||||||
|
|
||||||
|
let mut line_editor = Reedline::create()
|
||||||
|
.with_completer(completer)
|
||||||
|
.with_menu(ReedlineMenu::EngineCompleter(completion_menu))
|
||||||
|
.with_edit_mode(edit_mode);
|
||||||
|
|
||||||
|
let prompt = DefaultPrompt::default();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let sig = line_editor.read_line(&prompt)?;
|
||||||
|
match sig {
|
||||||
|
Signal::Success(buffer) => {
|
||||||
|
println!("We processed: {}", buffer);
|
||||||
|
}
|
||||||
|
Signal::CtrlD | Signal::CtrlC => {
|
||||||
|
println!("\nAborted!");
|
||||||
|
break Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
examples/highlighter.rs
Normal file
31
examples/highlighter.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// Create a reedline object with highlighter support.
|
||||||
|
// cargo run --example highlighter
|
||||||
|
//
|
||||||
|
// unmatched input is marked red, matched input is marked green
|
||||||
|
use reedline::{DefaultPrompt, ExampleHighlighter, Reedline, Signal};
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
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)));
|
||||||
|
let prompt = DefaultPrompt::default();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let sig = line_editor.read_line(&prompt)?;
|
||||||
|
match sig {
|
||||||
|
Signal::Success(buffer) => {
|
||||||
|
println!("We processed: {}", buffer);
|
||||||
|
}
|
||||||
|
Signal::CtrlD | Signal::CtrlC => {
|
||||||
|
println!("\nAborted!");
|
||||||
|
break Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
examples/hinter.rs
Normal file
31
examples/hinter.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// Create a reedline object with in-line hint support.
|
||||||
|
// cargo run --example hinter
|
||||||
|
//
|
||||||
|
// Fish-style history based hinting.
|
||||||
|
// assuming history ["abc", "ade"]
|
||||||
|
// pressing "a" hints to abc.
|
||||||
|
// Up/Down or Ctrl p/n, to select next/previous match
|
||||||
|
|
||||||
|
use nu_ansi_term::{Color, Style};
|
||||||
|
use reedline::{DefaultHinter, DefaultPrompt, Reedline, Signal};
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let mut line_editor = Reedline::create().with_hinter(Box::new(
|
||||||
|
DefaultHinter::default().with_style(Style::new().italic().fg(Color::LightGray)),
|
||||||
|
));
|
||||||
|
let prompt = DefaultPrompt::default();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let sig = line_editor.read_line(&prompt)?;
|
||||||
|
match sig {
|
||||||
|
Signal::Success(buffer) => {
|
||||||
|
println!("We processed: {}", buffer);
|
||||||
|
}
|
||||||
|
Signal::CtrlD | Signal::CtrlC => {
|
||||||
|
println!("\nAborted!");
|
||||||
|
break Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
examples/history.rs
Normal file
33
examples/history.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
// Create a reedline object with history support, including history size limits.
|
||||||
|
// cargo run --example history
|
||||||
|
//
|
||||||
|
// A file `history.txt` will be created (or replaced).
|
||||||
|
// Allows for persistent loading of previous session.
|
||||||
|
//
|
||||||
|
// Browse history by Up/Down arrows or Ctrl-n/p
|
||||||
|
|
||||||
|
use reedline::{DefaultPrompt, FileBackedHistory, Reedline, Signal};
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
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);
|
||||||
|
let prompt = DefaultPrompt::default();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let sig = line_editor.read_line(&prompt)?;
|
||||||
|
match sig {
|
||||||
|
Signal::Success(buffer) => {
|
||||||
|
println!("We processed: {}", buffer);
|
||||||
|
}
|
||||||
|
Signal::CtrlD | Signal::CtrlC => {
|
||||||
|
println!("\nAborted!");
|
||||||
|
break Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -22,6 +22,7 @@ use {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
println!("Ctrl-D to quit");
|
||||||
// quick command like parameter handling
|
// quick command like parameter handling
|
||||||
let vi_mode = matches!(std::env::args().nth(1), Some(x) if x == "--vi");
|
let vi_mode = matches!(std::env::args().nth(1), Some(x) if x == "--vi");
|
||||||
let args: Vec<String> = std::env::args().collect();
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
Loading…
Reference in New Issue
Block a user