feat(cli): readline history
This commit is contained in:
parent
fe66326956
commit
9b75cb6144
3 changed files with 58 additions and 9 deletions
45
src/cli.rs
45
src/cli.rs
|
|
@ -1,5 +1,46 @@
|
|||
use rustyline::{Editor, history::FileHistory};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use rustyline::{history::FileHistory, Editor};
|
||||
|
||||
fn xdg_state_dir() -> Option<PathBuf> {
|
||||
if let Ok(dir) = std::env::var("XDG_STATE_DIR") {
|
||||
Some(PathBuf::from(dir))
|
||||
} else if let Ok(home) = std::env::var("HOME") {
|
||||
if home.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(PathBuf::from(home).join(".local/state"))
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn state_dir() -> Option<PathBuf> {
|
||||
if let Some(dir) = xdg_state_dir() {
|
||||
let dir = dir.join("osdb");
|
||||
std::fs::create_dir_all(&dir).ok()?;
|
||||
Some(dir)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn history_file() -> Option<std::path::PathBuf> {
|
||||
if let Some(state) = state_dir() {
|
||||
Some(state.join("cli_history"))
|
||||
} else {
|
||||
eprintln!("Warning: failed to find or create XDG_STATE_DIR for osdb.");
|
||||
eprintln!("Warning: either set XDG_STATE_DIR or HOME, and ensure osdb has write permissions to that directory.");
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_input(rl: &mut Editor<(), FileHistory>) -> Option<String> {
|
||||
rl.readline("osdb> ").ok()
|
||||
if let Ok(result) = rl.readline("osdb> ") {
|
||||
let _ = rl.add_history_entry(&result);
|
||||
Some(result)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue