From 835d2e9f25311aef5cddcc970589a6d7f7f3dc3a Mon Sep 17 00:00:00 2001 From: perlindgren Date: Sun, 16 Oct 2022 22:13:55 +0200 Subject: [PATCH] examples updated (#493) Puts the minimal examples from the documentation as separate example executables to play around with --- examples/basic.rs | 26 ++++++++++++++++++ examples/completions.rs | 58 +++++++++++++++++++++++++++++++++++++++++ examples/highlighter.rs | 31 ++++++++++++++++++++++ examples/hinter.rs | 31 ++++++++++++++++++++++ examples/history.rs | 33 +++++++++++++++++++++++ src/main.rs | 1 + 6 files changed, 180 insertions(+) create mode 100644 examples/basic.rs create mode 100644 examples/completions.rs create mode 100644 examples/highlighter.rs create mode 100644 examples/hinter.rs create mode 100644 examples/history.rs diff --git a/examples/basic.rs b/examples/basic.rs new file mode 100644 index 0000000..9bbb029 --- /dev/null +++ b/examples/basic.rs @@ -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(()); + } + } + } +} diff --git a/examples/completions.rs b/examples/completions.rs new file mode 100644 index 0000000..a7b37d9 --- /dev/null +++ b/examples/completions.rs @@ -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(()); + } + } + } +} diff --git a/examples/highlighter.rs b/examples/highlighter.rs new file mode 100644 index 0000000..0f1d14f --- /dev/null +++ b/examples/highlighter.rs @@ -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(()); + } + } + } +} diff --git a/examples/hinter.rs b/examples/hinter.rs new file mode 100644 index 0000000..13a01d2 --- /dev/null +++ b/examples/hinter.rs @@ -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(()); + } + } + } +} diff --git a/examples/history.rs b/examples/history.rs new file mode 100644 index 0000000..117fc14 --- /dev/null +++ b/examples/history.rs @@ -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(()); + } + } + } +} diff --git a/src/main.rs b/src/main.rs index b232088..c9bf364 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ use { }; fn main() -> Result<()> { + println!("Ctrl-D to quit"); // quick command like parameter handling let vi_mode = matches!(std::env::args().nth(1), Some(x) if x == "--vi"); let args: Vec = std::env::args().collect();