Merge pull request #690 from pka/history-path

History path
This commit is contained in:
Jonathan Turner 2019-09-20 09:50:03 +12:00 committed by GitHub
commit db3b2595e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 9 deletions

View File

@ -7,6 +7,7 @@ use crate::commands::plugin::JsonRpc;
use crate::commands::plugin::{PluginCommand, PluginSink}; use crate::commands::plugin::{PluginCommand, PluginSink};
use crate::commands::whole_stream_command; use crate::commands::whole_stream_command;
use crate::context::Context; use crate::context::Context;
use crate::data::config;
use crate::data::Value; use crate::data::Value;
pub(crate) use crate::errors::ShellError; pub(crate) use crate::errors::ShellError;
use crate::fuzzysearch::{interactive_fuzzy_search, SelectionResult}; use crate::fuzzysearch::{interactive_fuzzy_search, SelectionResult};
@ -22,6 +23,7 @@ use std::env;
use std::error::Error; use std::error::Error;
use std::io::{BufRead, BufReader, Write}; use std::io::{BufRead, BufReader, Write};
use std::iter::Iterator; use std::iter::Iterator;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
#[derive(Debug)] #[derive(Debug)]
@ -210,6 +212,20 @@ fn load_plugins(context: &mut Context) -> Result<(), ShellError> {
Ok(()) 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<dyn Error>> { pub async fn cli() -> Result<(), Box<dyn Error>> {
let mut context = Context::basic()?; let mut context = Context::basic()?;
@ -302,7 +318,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
} }
// we are ok if history does not exist // 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 ctrl_c = Arc::new(AtomicBool::new(false));
let cc = ctrl_c.clone(); let cc = ctrl_c.clone();
@ -323,7 +339,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
context.shell_manager.clone(), context.shell_manager.clone(),
))); )));
let edit_mode = crate::data::config::config(Tag::unknown())? let edit_mode = config::config(Tag::unknown())?
.get("edit_mode") .get("edit_mode")
.map(|s| match s.as_string().unwrap().as_ref() { .map(|s| match s.as_string().unwrap().as_ref() {
"vi" => EditMode::Vi, "vi" => EditMode::Vi,
@ -383,6 +399,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
LineResult::CtrlC => { LineResult::CtrlC => {
if ctrlcbreak { if ctrlcbreak {
let _ = rl.save_history(&History::path());
std::process::exit(0); std::process::exit(0);
} else { } else {
context.with_host(|host| host.stdout("CTRL-C pressed (again to quit)")); context.with_host(|host| host.stdout("CTRL-C pressed (again to quit)"));
@ -417,7 +434,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
} }
// we are ok if we can not save history // we are ok if we can not save history
let _ = rl.save_history("history.txt"); let _ = rl.save_history(&History::path());
Ok(()) Ok(())
} }

View File

@ -130,7 +130,7 @@ impl InternalCommand {
CommandAction::AddSpanSource(uuid, span_source) => { CommandAction::AddSpanSource(uuid, span_source) => {
context.add_span_source(uuid, span_source); context.add_span_source(uuid, span_source);
} }
CommandAction::Exit => std::process::exit(0), CommandAction::Exit => std::process::exit(0), // TODO: save history.txt
CommandAction::EnterHelpShell(value) => { CommandAction::EnterHelpShell(value) => {
match value { match value {
Tagged { Tagged {
@ -170,7 +170,7 @@ impl InternalCommand {
CommandAction::LeaveShell => { CommandAction::LeaveShell => {
context.shell_manager.remove_at_current(); context.shell_manager.remove_at_current();
if context.shell_manager.is_empty() { if context.shell_manager.is_empty() {
std::process::exit(0); std::process::exit(0); // TODO: save history.txt
} }
} }
}, },

View File

@ -23,10 +23,7 @@ pub const APP_INFO: AppInfo = AppInfo {
}; };
pub fn config_path() -> Result<PathBuf, ShellError> { pub fn config_path() -> Result<PathBuf, ShellError> {
let path = app_root(AppDataType::UserConfig, &APP_INFO) app_path(AppDataType::UserConfig, "config")
.map_err(|err| ShellError::string(&format!("Couldn't open config path:\n{}", err)))?;
Ok(path)
} }
pub fn default_path() -> Result<PathBuf, ShellError> { pub fn default_path() -> Result<PathBuf, ShellError> {
@ -49,6 +46,17 @@ pub fn default_path_for(file: &Option<PathBuf>) -> Result<PathBuf, ShellError> {
Ok(filename.clone()) Ok(filename.clone())
} }
pub fn user_data() -> Result<PathBuf, ShellError> {
app_path(AppDataType::UserData, "user data")
}
pub fn app_path(app_data_type: AppDataType, display: &str) -> Result<PathBuf, ShellError> {
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( pub fn read(
tag: impl Into<Tag>, tag: impl Into<Tag>,
at: &Option<PathBuf>, at: &Option<PathBuf>,