mirror of
https://github.com/nushell/nushell.git
synced 2025-02-16 18:41:44 +01:00
Extract out history parts.
This commit is contained in:
parent
4724b3c570
commit
26cec83b63
@ -64,36 +64,6 @@ pub fn search_paths() -> Vec<std::path::PathBuf> {
|
||||
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<Helper>, IndexMap<String, Value>) {
|
||||
pub fn create_rustyline_configuration() -> (Editor<Helper>, IndexMap<String, Value>) {
|
||||
#[cfg(windows)]
|
||||
const DEFAULT_COMPLETION_MODE: CompletionType = CompletionType::Circular;
|
||||
#[cfg(not(windows))]
|
||||
@ -583,9 +553,6 @@ pub fn set_rustyline_configuration() -> (Editor<Helper>, IndexMap<String, Value>
|
||||
}
|
||||
}
|
||||
|
||||
// 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<dyn Error>> {
|
||||
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(())
|
||||
}
|
||||
|
@ -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<OutputStream, ShellError> {
|
||||
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 {
|
||||
|
4
crates/nu-cli/src/env/environment_syncer.rs
vendored
4
crates/nu-cli/src/env/environment_syncer.rs
vendored
@ -30,6 +30,10 @@ impl EnvironmentSyncer {
|
||||
self.config = Arc::new(config);
|
||||
}
|
||||
|
||||
pub fn get_config(&self) -> Box<dyn Conf> {
|
||||
self.config.clone().clone_box()
|
||||
}
|
||||
|
||||
pub fn load_environment(&mut self) {
|
||||
let config = self.config.clone();
|
||||
|
||||
|
@ -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<String, String>, tag: impl Into<Tag>) -> Result<Value,
|
||||
UntaggedValue::path(keybinding_path).into_value(&tag),
|
||||
);
|
||||
|
||||
let history = History::path();
|
||||
let history = crate::commands::history::history_path(&nu_data::config::NuConfig::new());
|
||||
nu_dict.insert_value(
|
||||
"history-path",
|
||||
UntaggedValue::path(history).into_value(&tag),
|
||||
|
@ -5,6 +5,7 @@ pub trait Conf: Debug + Send {
|
||||
fn env(&self) -> Option<Value>;
|
||||
fn path(&self) -> Option<Value>;
|
||||
fn reload(&self);
|
||||
fn clone_box(&self) -> Box<dyn Conf>;
|
||||
}
|
||||
|
||||
impl Conf for Box<dyn Conf> {
|
||||
@ -19,4 +20,8 @@ impl Conf for Box<dyn Conf> {
|
||||
fn reload(&self) {
|
||||
(**self).reload();
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn Conf> {
|
||||
(**self).clone_box()
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,10 @@ impl Conf for NuConfig {
|
||||
vars.extend(variables);
|
||||
}
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn Conf> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl NuConfig {
|
||||
|
@ -23,6 +23,10 @@ impl Conf for FakeConfig {
|
||||
fn reload(&self) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
fn clone_box(&self) -> Box<dyn Conf> {
|
||||
self.config.clone_box()
|
||||
}
|
||||
}
|
||||
|
||||
impl FakeConfig {
|
||||
|
Loading…
Reference in New Issue
Block a user