refactor(get_command): spawn rustyline instance from main()
This commit is contained in:
parent
3d4ab2e2e4
commit
fe66326956
4 changed files with 19 additions and 16 deletions
10
notes.org
10
notes.org
|
|
@ -196,7 +196,15 @@ i will use rustyline, since it seems like the most feature-complete
|
||||||
|
|
||||||
** DONE do the impl
|
** 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
|
* TODO handle non-interactive input better
|
||||||
|
|
||||||
|
|
|
||||||
12
src/cli.rs
12
src/cli.rs
|
|
@ -1,11 +1,5 @@
|
||||||
use rustyline::DefaultEditor;
|
use rustyline::{Editor, history::FileHistory};
|
||||||
|
|
||||||
pub fn read_input() -> Option<String> {
|
pub fn read_input(rl: &mut Editor<(), FileHistory>) -> Option<String> {
|
||||||
let mut rl = DefaultEditor::new().ok()?;
|
rl.readline("osdb> ").ok()
|
||||||
|
|
||||||
let readline = rl.readline("osdb> ");
|
|
||||||
match readline {
|
|
||||||
Ok(line) => Some(line),
|
|
||||||
Err(_) => None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,13 @@ use osdb::error_display::OSDBError as _;
|
||||||
use osdb::parser::parse;
|
use osdb::parser::parse;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let mut rl = rustyline::DefaultEditor::new().expect("failed to create stdin reader");
|
||||||
|
|
||||||
println!("{}", startup_msg());
|
println!("{}", startup_msg());
|
||||||
'main: while let Some(input) = read_input() {
|
|
||||||
|
'main: while let Some(input) = read_input(&mut rl) {
|
||||||
let file = String::from("<stdin>");
|
let file = String::from("<stdin>");
|
||||||
|
|
||||||
match parse(file.clone(), input.clone()) {
|
match parse(file.clone(), input.clone()) {
|
||||||
Ok(cmds) => {
|
Ok(cmds) => {
|
||||||
for cmd in cmds {
|
for cmd in cmds {
|
||||||
|
|
@ -24,5 +28,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Good-bye");
|
println!("Good-bye");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,11 +22,7 @@ pub fn parse(file: String, input: String) -> Result<Vec<Command>, Vec<CommandPar
|
||||||
crate::tokens::TokenData::EndOfFile => (),
|
crate::tokens::TokenData::EndOfFile => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if errs.is_empty() {
|
if errs.is_empty() { Ok(cmds) } else { Err(errs) }
|
||||||
Ok(cmds)
|
|
||||||
} else {
|
|
||||||
Err(errs)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue