forked from extern/nushell
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
|
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(
|
pub fn create_default_context(
|
||||||
syncer: &mut crate::EnvironmentSyncer,
|
syncer: &mut crate::EnvironmentSyncer,
|
||||||
interactive: bool,
|
interactive: bool,
|
||||||
@ -421,7 +391,7 @@ pub async fn run_pipeline_standalone(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_rustyline_configuration() -> (Editor<Helper>, IndexMap<String, Value>) {
|
pub fn create_rustyline_configuration() -> (Editor<Helper>, IndexMap<String, Value>) {
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
const DEFAULT_COMPLETION_MODE: CompletionType = CompletionType::Circular;
|
const DEFAULT_COMPLETION_MODE: CompletionType = CompletionType::Circular;
|
||||||
#[cfg(not(windows))]
|
#[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)
|
(rl, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -594,9 +561,15 @@ pub async fn cli(
|
|||||||
mut syncer: EnvironmentSyncer,
|
mut syncer: EnvironmentSyncer,
|
||||||
mut context: Context,
|
mut context: Context,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> 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 _ = 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
|
let skip_welcome_message = config
|
||||||
.get("skip_welcome_message")
|
.get("skip_welcome_message")
|
||||||
@ -761,13 +734,13 @@ pub async fn cli(
|
|||||||
match line {
|
match line {
|
||||||
LineResult::Success(line) => {
|
LineResult::Success(line) => {
|
||||||
rl.add_history_entry(&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));
|
context.maybe_print_errors(Text::from(line));
|
||||||
}
|
}
|
||||||
|
|
||||||
LineResult::Error(line, err) => {
|
LineResult::Error(line, err) => {
|
||||||
rl.add_history_entry(&line);
|
rl.add_history_entry(&line);
|
||||||
let _ = rl.save_history(&History::path());
|
let _ = rl.save_history(&history_path);
|
||||||
|
|
||||||
context.with_host(|_host| {
|
context.with_host(|_host| {
|
||||||
print_err(err, &Text::from(line.clone()));
|
print_err(err, &Text::from(line.clone()));
|
||||||
@ -787,7 +760,7 @@ pub async fn cli(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ctrlcbreak {
|
if ctrlcbreak {
|
||||||
let _ = rl.save_history(&History::path());
|
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)"));
|
||||||
@ -804,7 +777,7 @@ pub async fn cli(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// we are ok if we can not save history
|
// we are ok if we can not save history
|
||||||
let _ = rl.save_history(&History::path());
|
let _ = rl.save_history(&history_path);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,32 @@
|
|||||||
use crate::cli::History as HistoryFile;
|
|
||||||
use crate::commands::WholeStreamCommand;
|
use crate::commands::WholeStreamCommand;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use nu_data::config::NuConfig;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
|
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufRead, BufReader};
|
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;
|
pub struct History;
|
||||||
|
|
||||||
@ -32,9 +54,10 @@ impl WholeStreamCommand for History {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn history(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
fn history(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||||
|
let config = NuConfig::new();
|
||||||
let tag = args.call_info.name_tag;
|
let tag = args.call_info.name_tag;
|
||||||
let history_path = HistoryFile::path();
|
let path = history_path(&config);
|
||||||
let file = File::open(history_path);
|
let file = File::open(path);
|
||||||
if let Ok(file) = file {
|
if let Ok(file) = file {
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
let output = reader.lines().filter_map(move |line| match line {
|
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);
|
self.config = Arc::new(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_config(&self) -> Box<dyn Conf> {
|
||||||
|
self.config.clone().clone_box()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_environment(&mut self) {
|
pub fn load_environment(&mut self) {
|
||||||
let config = self.config.clone();
|
let config = self.config.clone();
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use crate::cli::History;
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value};
|
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),
|
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(
|
nu_dict.insert_value(
|
||||||
"history-path",
|
"history-path",
|
||||||
UntaggedValue::path(history).into_value(&tag),
|
UntaggedValue::path(history).into_value(&tag),
|
||||||
|
@ -5,6 +5,7 @@ pub trait Conf: Debug + Send {
|
|||||||
fn env(&self) -> Option<Value>;
|
fn env(&self) -> Option<Value>;
|
||||||
fn path(&self) -> Option<Value>;
|
fn path(&self) -> Option<Value>;
|
||||||
fn reload(&self);
|
fn reload(&self);
|
||||||
|
fn clone_box(&self) -> Box<dyn Conf>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Conf for Box<dyn Conf> {
|
impl Conf for Box<dyn Conf> {
|
||||||
@ -19,4 +20,8 @@ impl Conf for Box<dyn Conf> {
|
|||||||
fn reload(&self) {
|
fn reload(&self) {
|
||||||
(**self).reload();
|
(**self).reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn clone_box(&self) -> Box<dyn Conf> {
|
||||||
|
(**self).clone_box()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,10 @@ impl Conf for NuConfig {
|
|||||||
vars.extend(variables);
|
vars.extend(variables);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn clone_box(&self) -> Box<dyn Conf> {
|
||||||
|
Box::new(self.clone())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NuConfig {
|
impl NuConfig {
|
||||||
|
@ -23,6 +23,10 @@ impl Conf for FakeConfig {
|
|||||||
fn reload(&self) {
|
fn reload(&self) {
|
||||||
// no-op
|
// no-op
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn clone_box(&self) -> Box<dyn Conf> {
|
||||||
|
self.config.clone_box()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FakeConfig {
|
impl FakeConfig {
|
||||||
|
Loading…
Reference in New Issue
Block a user