From 26cec83b635757bfb0e8556fd2465bcaabb47638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Thu, 27 Aug 2020 06:06:25 -0500 Subject: [PATCH] Extract out history parts. --- crates/nu-cli/src/cli.rs | 51 +++++---------------- crates/nu-cli/src/commands/history.rs | 29 ++++++++++-- crates/nu-cli/src/env/environment_syncer.rs | 4 ++ crates/nu-cli/src/evaluate/variables.rs | 3 +- crates/nu-data/src/config/conf.rs | 5 ++ crates/nu-data/src/config/nuconfig.rs | 4 ++ crates/nu-data/src/config/tests.rs | 4 ++ 7 files changed, 56 insertions(+), 44 deletions(-) diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index b2561aa122..a3ce8e7f7d 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -64,36 +64,6 @@ pub fn search_paths() -> Vec { search_paths } -pub struct History; - -impl History { - pub fn path() -> PathBuf { - const FNAME: &str = "history.txt"; - let default = config::user_data() - .map(|mut p| { - p.push(FNAME); - p - }) - .unwrap_or_else(|_| PathBuf::from(FNAME)); - - let cfg = nu_data::config::config(Tag::unknown()); - if let Ok(c) = cfg { - match &c.get("history-path") { - Some(Value { - value: UntaggedValue::Primitive(p), - .. - }) => match p { - Primitive::String(path) => PathBuf::from(path), - _ => default, - }, - _ => default, - } - } else { - default - } - } -} - pub fn create_default_context( syncer: &mut crate::EnvironmentSyncer, interactive: bool, @@ -421,7 +391,7 @@ pub async fn run_pipeline_standalone( Ok(()) } -pub fn set_rustyline_configuration() -> (Editor, IndexMap) { +pub fn create_rustyline_configuration() -> (Editor, IndexMap) { #[cfg(windows)] const DEFAULT_COMPLETION_MODE: CompletionType = CompletionType::Circular; #[cfg(not(windows))] @@ -583,9 +553,6 @@ pub fn set_rustyline_configuration() -> (Editor, IndexMap } } - // we are ok if history does not exist - let _ = rl.load_history(&History::path()); - (rl, config) } @@ -594,9 +561,15 @@ pub async fn cli( mut syncer: EnvironmentSyncer, mut context: Context, ) -> Result<(), Box> { + let configuration = nu_data::config::NuConfig::new(); + let history_path = crate::commands::history::history_path(&configuration); + let _ = register_plugins(&mut context); - let (mut rl, config) = set_rustyline_configuration(); + let (mut rl, config) = create_rustyline_configuration(); + + // we are ok if history does not exist + let _ = rl.load_history(&history_path); let skip_welcome_message = config .get("skip_welcome_message") @@ -761,13 +734,13 @@ pub async fn cli( match line { LineResult::Success(line) => { rl.add_history_entry(&line); - let _ = rl.save_history(&History::path()); + let _ = rl.save_history(&history_path); context.maybe_print_errors(Text::from(line)); } LineResult::Error(line, err) => { rl.add_history_entry(&line); - let _ = rl.save_history(&History::path()); + let _ = rl.save_history(&history_path); context.with_host(|_host| { print_err(err, &Text::from(line.clone())); @@ -787,7 +760,7 @@ pub async fn cli( } if ctrlcbreak { - let _ = rl.save_history(&History::path()); + let _ = rl.save_history(&history_path); std::process::exit(0); } else { context.with_host(|host| host.stdout("CTRL-C pressed (again to quit)")); @@ -804,7 +777,7 @@ pub async fn cli( } // we are ok if we can not save history - let _ = rl.save_history(&History::path()); + let _ = rl.save_history(&history_path); Ok(()) } diff --git a/crates/nu-cli/src/commands/history.rs b/crates/nu-cli/src/commands/history.rs index 213583fb34..32e2ceb84c 100644 --- a/crates/nu-cli/src/commands/history.rs +++ b/crates/nu-cli/src/commands/history.rs @@ -1,10 +1,32 @@ -use crate::cli::History as HistoryFile; use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_data::config::NuConfig; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; use std::fs::File; use std::io::{BufRead, BufReader}; +use std::path::PathBuf; + +const DEFAULT_LOCATION: &str = "history.txt"; + +pub fn history_path(config: &NuConfig) -> PathBuf { + let vars = config.vars.lock(); + + let default_path = nu_data::config::user_data() + .map(|mut p| { + p.push(DEFAULT_LOCATION); + p + }) + .unwrap_or_else(|_| PathBuf::from(DEFAULT_LOCATION)); + + vars.get("history-path") + .map_or(default_path.clone(), |custom_path| { + match custom_path.as_string() { + Ok(path) => PathBuf::from(path), + Err(_) => default_path, + } + }) +} pub struct History; @@ -32,9 +54,10 @@ impl WholeStreamCommand for History { } fn history(args: CommandArgs, _registry: &CommandRegistry) -> Result { + let config = NuConfig::new(); let tag = args.call_info.name_tag; - let history_path = HistoryFile::path(); - let file = File::open(history_path); + let path = history_path(&config); + let file = File::open(path); if let Ok(file) = file { let reader = BufReader::new(file); let output = reader.lines().filter_map(move |line| match line { diff --git a/crates/nu-cli/src/env/environment_syncer.rs b/crates/nu-cli/src/env/environment_syncer.rs index c7b847a30e..7ca8174476 100644 --- a/crates/nu-cli/src/env/environment_syncer.rs +++ b/crates/nu-cli/src/env/environment_syncer.rs @@ -30,6 +30,10 @@ impl EnvironmentSyncer { self.config = Arc::new(config); } + pub fn get_config(&self) -> Box { + self.config.clone().clone_box() + } + pub fn load_environment(&mut self) { let config = self.config.clone(); diff --git a/crates/nu-cli/src/evaluate/variables.rs b/crates/nu-cli/src/evaluate/variables.rs index 94f77fcda0..7f4dd7d8db 100644 --- a/crates/nu-cli/src/evaluate/variables.rs +++ b/crates/nu-cli/src/evaluate/variables.rs @@ -1,4 +1,3 @@ -use crate::cli::History; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value}; @@ -48,7 +47,7 @@ pub fn nu(env: &IndexMap, tag: impl Into) -> Result Option; fn path(&self) -> Option; fn reload(&self); + fn clone_box(&self) -> Box; } impl Conf for Box { @@ -19,4 +20,8 @@ impl Conf for Box { fn reload(&self) { (**self).reload(); } + + fn clone_box(&self) -> Box { + (**self).clone_box() + } } diff --git a/crates/nu-data/src/config/nuconfig.rs b/crates/nu-data/src/config/nuconfig.rs index 76e08a9a8e..c9c148cc33 100644 --- a/crates/nu-data/src/config/nuconfig.rs +++ b/crates/nu-data/src/config/nuconfig.rs @@ -27,6 +27,10 @@ impl Conf for NuConfig { vars.extend(variables); } } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } } impl NuConfig { diff --git a/crates/nu-data/src/config/tests.rs b/crates/nu-data/src/config/tests.rs index 9039707b76..dcad4a69c1 100644 --- a/crates/nu-data/src/config/tests.rs +++ b/crates/nu-data/src/config/tests.rs @@ -23,6 +23,10 @@ impl Conf for FakeConfig { fn reload(&self) { // no-op } + + fn clone_box(&self) -> Box { + self.config.clone_box() + } } impl FakeConfig {