From ff8d2f2d6f0efc363d3681c3908923ed8a1daf4b Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 12 Jul 2019 02:40:06 -0400 Subject: adjust the readline api a bit --- src/lib.rs | 1 + src/readline.rs | 22 +++++++++++++++++----- src/repl.rs | 2 +- src/tui.rs | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4f3e3df..cfcc4a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ #![allow(clippy::if_not_else)] // match_same_arms is buggy, doesn't notice differences due to match arm order #![allow(clippy::match_same_arms)] +#![allow(clippy::missing_const_for_fn)] #![allow(clippy::module_name_repetitions)] #![allow(clippy::multiple_crate_versions)] #![allow(clippy::similar_names)] diff --git a/src/readline.rs b/src/readline.rs index a8c1526..f878d50 100644 --- a/src/readline.rs +++ b/src/readline.rs @@ -24,8 +24,8 @@ pub enum Error { pub type Result = std::result::Result; -pub fn readline(prompt: &str, echo: bool) -> Result { - Readline::new(prompt, echo) +pub fn readline() -> Result { + Readline::new() } pub struct Readline { @@ -44,15 +44,15 @@ struct ReadlineState { } impl Readline { - fn new(prompt: &str, echo: bool) -> Result { + pub fn new() -> Result { let screen = crossterm::RawScreen::into_raw_mode().context(IntoRawMode)?; Ok(Self { reader: None, state: ReadlineState { - prompt: prompt.to_string(), - echo, + prompt: String::from("$ "), + echo: true, buffer: String::new(), cursor: 0, wrote_prompt: false, @@ -61,6 +61,18 @@ impl Readline { }) } + #[allow(dead_code)] + pub fn prompt(mut self, prompt: &str) -> Self { + self.state.prompt = prompt.to_string(); + self + } + + #[allow(dead_code)] + pub fn echo(mut self, echo: bool) -> Self { + self.state.echo = echo; + self + } + fn with_reader(&mut self, f: F) -> Result where F: FnOnce(&KeyReader, &mut ReadlineState) -> Result, diff --git a/src/repl.rs b/src/repl.rs index c370fcf..7f4c10d 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -51,7 +51,7 @@ pub fn repl() { } fn read() -> impl futures::future::Future { - crate::readline::readline("$ ", true) + crate::readline::readline() .into_future() .flatten() .context(Read) diff --git a/src/tui.rs b/src/tui.rs index 82733c8..49f7d6f 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -40,7 +40,7 @@ impl Tui { } fn read() -> Result { - crate::readline::readline("$ ", true).context(Read) + crate::readline::readline().context(Read) } fn eval( -- cgit v1.2.3-54-g00ecf