From 18544e32ecd1d3bc3535214f0c2edca38346e3a5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 3 May 2020 17:34:28 +0200 Subject: 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. --- src/main.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'src/main.rs') 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> = + 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 -- cgit v1.2.3