From f9005692a1e2dba89274f39df762fdcfa59b739b Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 28 Apr 2024 12:49:50 +0200 Subject: rebel-lang: repl: switch from rustyline to reedline reedline has a nicer API than rustyline. --- crates/rebel-lang/Cargo.toml | 2 +- crates/rebel-lang/examples/repl.rs | 43 ++++++++++++++------------------------ 2 files changed, 17 insertions(+), 28 deletions(-) (limited to 'crates') diff --git a/crates/rebel-lang/Cargo.toml b/crates/rebel-lang/Cargo.toml index 957d31d..6601c8e 100644 --- a/crates/rebel-lang/Cargo.toml +++ b/crates/rebel-lang/Cargo.toml @@ -15,4 +15,4 @@ enum-kinds = "0.5.1" [dev-dependencies] clap = { version = "4.0.0", features = ["derive"] } -rustyline = { version = "14.0.0", features = ["derive"] } +reedline = "0.31.0" diff --git a/crates/rebel-lang/examples/repl.rs b/crates/rebel-lang/examples/repl.rs index 185155b..4a7b755 100644 --- a/crates/rebel-lang/examples/repl.rs +++ b/crates/rebel-lang/examples/repl.rs @@ -5,7 +5,7 @@ use rebel_lang::{ value::{EvalError, Result, Value}, }; use rebel_parse::{recipe, tokenize}; -use rustyline::{error::ReadlineError, validate::ValidationResult}; +use reedline::{DefaultPrompt, DefaultPromptSegment, Reedline, Signal, ValidationResult}; fn intrinsic_array_len(params: &[Value]) -> Result { assert!(params.len() == 1); @@ -24,25 +24,19 @@ fn intrinsic_string_len(params: &[Value]) -> Result { )) } -#[derive(rustyline::Completer, rustyline::Hinter, rustyline::Highlighter, rustyline::Helper)] -struct Helper; +struct Validator; -impl rustyline::validate::Validator for Helper { - fn validate( - &self, - ctx: &mut rustyline::validate::ValidationContext, - ) -> rustyline::Result { - let input = ctx.input(); - let result = match tokenize::token_stream(input) { - Ok(_) => ValidationResult::Valid(None), - Err(_) => ValidationResult::Incomplete, - }; - - Ok(result) +impl reedline::Validator for Validator { + fn validate(&self, line: &str) -> ValidationResult { + if tokenize::token_stream(line).is_ok() { + ValidationResult::Complete + } else { + ValidationResult::Incomplete + } } } -fn main() -> rustyline::Result<()> { +fn main() { let mut ctx = Context::default(); ctx.methods.entry(TypeFamily::Array).or_default().insert( @@ -66,18 +60,15 @@ fn main() -> rustyline::Result<()> { }, ); - let mut rl = rustyline::Editor::::new()?; - rl.set_helper(Some(Helper)); + let mut rl = Reedline::create().with_validator(Box::new(Validator)); + let prompt = DefaultPrompt::new(DefaultPromptSegment::Empty, DefaultPromptSegment::Empty); loop { - let input = match rl.readline("> ") { - Ok(input) => input, - Err(ReadlineError::Interrupted) => continue, - Err(ReadlineError::WindowResized) => continue, - Err(ReadlineError::Eof) => break, - Err(err) => panic!("{err}"), + let input = match rl.read_line(&prompt).unwrap() { + Signal::Success(input) => input, + Signal::CtrlC => continue, + Signal::CtrlD => break, }; - rl.add_history_entry(input.as_str())?; let tokens = match tokenize::token_stream(&input) { Ok(value) => value, @@ -146,6 +137,4 @@ fn main() -> rustyline::Result<()> { }; println!("{value}: {typ}"); } - - Ok(()) } -- cgit v1.2.3