diff options
author | Daniel <kingdread@gmx.de> | 2020-05-03 17:34:28 +0200 |
---|---|---|
committer | Daniel <kingdread@gmx.de> | 2020-05-03 17:34:28 +0200 |
commit | 18544e32ecd1d3bc3535214f0c2edca38346e3a5 (patch) | |
tree | 3334b84a9bb014dcf5ffc768148ecd295afb78b2 /src/main.rs | |
parent | 913bc8c547e97a785cf1cd9fc20f8377f898da2c (diff) | |
download | raidgrep-18544e32ecd1d3bc3535214f0c2edca38346e3a5.tar.gz raidgrep-18544e32ecd1d3bc3535214f0c2edca38346e3a5.tar.bz2 raidgrep-18544e32ecd1d3bc3535214f0c2edca38346e3a5.zip |
save REPL history to a file
This persists the REPL history across program restarts.
The code should probably be cleaned up a bit more, the error handling in
this one is a bit all over the place. This is because we don't want to
make it a hard error in case the history cannot be saved.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index fd3399b..5b6d57f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ #![feature(trait_alias)] use std::collections::HashMap; use std::fmt; -use std::fs::File; +use std::fs::{self, File}; use std::io::{BufReader, Read, Seek}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::str::FromStr; use std::sync::atomic::{AtomicBool, Ordering}; @@ -25,6 +25,10 @@ use filters::{log::LogFilter, Inclusion}; mod guilds; mod logger; mod output; +mod paths; + +/// Application name, as it should be used in configuration directory paths. +const APP_NAME: &str = "raidgrep"; /// A program that allows you to search through all your evtc logs for specific people. /// @@ -299,9 +303,18 @@ fn repl(opt: &Opt) -> Result<()> { .expect("Could not set interrupt hanlder"); let mut rl = Editor::<()>::new(); + let history_path = paths::history_path(); + if history_path.is_none() { + debug!("Could not determine the history path"); + } + + maybe_load_history(&mut rl, history_path.as_ref().map(|r| r as &Path)); + loop { let line = rl.readline("Query> ")?; rl.add_history_entry(&line); + maybe_save_history(&rl, history_path.as_ref().map(|r| r as &Path)); + let parsed = build_filter(&line); INTERRUPTED.store(false, Ordering::Relaxed); match parsed { @@ -311,6 +324,28 @@ fn repl(opt: &Opt) -> Result<()> { } } +fn maybe_load_history(rl: &mut Editor<()>, path: Option<&Path>) { + if let Some(path) = path { + debug!("Loading history from {:?}", path); + if let Err(e) = rl.load_history(path) { + debug!("Loading the history failed: {}", e); + } + } +} + +fn maybe_save_history(rl: &Editor<()>, path: Option<&Path>) { + if let Some(path) = path { + debug!("Saving history to {:?}", path); + let result: Result<(), Box<dyn std::error::Error>> = + fs::create_dir_all(path.parent().unwrap()) + .map_err(Into::into) + .and_then(|_| rl.save_history(path).map_err(Into::into)); + if let Err(e) = result { + debug!("Saving the history failed: {}", e); + } + } +} + /// Check if the given entry represents a log file, based on the file name. fn is_log_file(entry: &DirEntry) -> bool { entry |