diff --git a/src/cli.rs b/src/cli.rs index fb158b1af6..b410fadf00 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -7,6 +7,7 @@ use crate::commands::plugin::JsonRpc; use crate::commands::plugin::{PluginCommand, PluginSink}; use crate::commands::whole_stream_command; use crate::context::Context; +use crate::data::config; use crate::data::Value; pub(crate) use crate::errors::ShellError; use crate::fuzzysearch::{interactive_fuzzy_search, SelectionResult}; @@ -22,6 +23,7 @@ use std::env; use std::error::Error; use std::io::{BufRead, BufReader, Write}; use std::iter::Iterator; +use std::path::PathBuf; use std::sync::atomic::{AtomicBool, Ordering}; #[derive(Debug)] @@ -210,6 +212,20 @@ fn load_plugins(context: &mut Context) -> Result<(), ShellError> { Ok(()) } +struct History; + +impl History { + pub fn path() -> PathBuf { + const FNAME: &str = "history.txt"; + config::user_data() + .map(|mut p| { + p.push(FNAME); + p + }) + .unwrap_or(PathBuf::from(FNAME)) + } +} + pub async fn cli() -> Result<(), Box> { let mut context = Context::basic()?; @@ -302,7 +318,7 @@ pub async fn cli() -> Result<(), Box> { } // we are ok if history does not exist - let _ = rl.load_history("history.txt"); + let _ = rl.load_history(&History::path()); let ctrl_c = Arc::new(AtomicBool::new(false)); let cc = ctrl_c.clone(); @@ -323,7 +339,7 @@ pub async fn cli() -> Result<(), Box> { context.shell_manager.clone(), ))); - let edit_mode = crate::data::config::config(Tag::unknown())? + let edit_mode = config::config(Tag::unknown())? .get("edit_mode") .map(|s| match s.as_string().unwrap().as_ref() { "vi" => EditMode::Vi, @@ -417,7 +433,7 @@ pub async fn cli() -> Result<(), Box> { } // we are ok if we can not save history - let _ = rl.save_history("history.txt"); + let _ = rl.save_history(&History::path()); Ok(()) } diff --git a/src/data/config.rs b/src/data/config.rs index 6b4d1383f5..1cb4533d8e 100644 --- a/src/data/config.rs +++ b/src/data/config.rs @@ -23,10 +23,7 @@ pub const APP_INFO: AppInfo = AppInfo { }; pub fn config_path() -> Result { - let path = app_root(AppDataType::UserConfig, &APP_INFO) - .map_err(|err| ShellError::string(&format!("Couldn't open config path:\n{}", err)))?; - - Ok(path) + app_path(AppDataType::UserConfig, "config") } pub fn default_path() -> Result { @@ -49,6 +46,17 @@ pub fn default_path_for(file: &Option) -> Result { Ok(filename.clone()) } +pub fn user_data() -> Result { + app_path(AppDataType::UserData, "user data") +} + +pub fn app_path(app_data_type: AppDataType, display: &str) -> Result { + let path = app_root(app_data_type, &APP_INFO) + .map_err(|err| ShellError::string(&format!("Couldn't open {} path:\n{}", display, err)))?; + + Ok(path) +} + pub fn read( tag: impl Into, at: &Option,