feat(cli): readline history

This commit is contained in:
Khaïs COLIN 2025-05-10 10:20:26 +02:00
parent fe66326956
commit 9b75cb6144
Signed by: logistic-bot
SSH key fingerprint: SHA256:3zI3/tx0ZpCLHCLPmEaGR4oeYCPMCzQxXhXutBmtOAU
3 changed files with 58 additions and 9 deletions

View file

@ -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
}
}