From fe66326956c84a45c81987ff3f1a58f457e0ce86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 9 May 2025 17:16:05 +0200 Subject: [PATCH] refactor(get_command): spawn rustyline instance from main() --- notes.org | 10 +++++++++- src/cli.rs | 12 +++--------- src/main.rs | 7 ++++++- src/parser.rs | 6 +----- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/notes.org b/notes.org index 7420bf0..6db9d88 100644 --- a/notes.org +++ b/notes.org @@ -196,7 +196,15 @@ i will use rustyline, since it seems like the most feature-complete ** DONE do the impl -** TODO tweak it to make history work +** TODO make history work + +*** DONE have the rl instance be spawned from main + +*** TODO figure out how to locate the app data directory on linux + +*** TODO create our own app data directory + +*** TODO load and save the history from a file in this directory * TODO handle non-interactive input better diff --git a/src/cli.rs b/src/cli.rs index 1323240..e44b26f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,11 +1,5 @@ -use rustyline::DefaultEditor; +use rustyline::{Editor, history::FileHistory}; -pub fn read_input() -> Option { - let mut rl = DefaultEditor::new().ok()?; - - let readline = rl.readline("osdb> "); - match readline { - Ok(line) => Some(line), - Err(_) => None, - } +pub fn read_input(rl: &mut Editor<(), FileHistory>) -> Option { + rl.readline("osdb> ").ok() } diff --git a/src/main.rs b/src/main.rs index bac5cd6..6691c8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,13 @@ use osdb::error_display::OSDBError as _; use osdb::parser::parse; fn main() { + let mut rl = rustyline::DefaultEditor::new().expect("failed to create stdin reader"); + println!("{}", startup_msg()); - 'main: while let Some(input) = read_input() { + + 'main: while let Some(input) = read_input(&mut rl) { let file = String::from(""); + match parse(file.clone(), input.clone()) { Ok(cmds) => { for cmd in cmds { @@ -24,5 +28,6 @@ fn main() { } } } + println!("Good-bye"); } diff --git a/src/parser.rs b/src/parser.rs index 949a1bc..98325d8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -22,11 +22,7 @@ pub fn parse(file: String, input: String) -> Result, Vec (), } } - if errs.is_empty() { - Ok(cmds) - } else { - Err(errs) - } + if errs.is_empty() { Ok(cmds) } else { Err(errs) } } #[cfg(test)]