Use get_history_path where appropriate
This commit is contained in:
parent
53fbf62493
commit
8eaf2c175d
|
@ -5,6 +5,8 @@ use reedline::{
|
|||
SqliteBackedHistory,
|
||||
};
|
||||
|
||||
use crate::config_files::get_history_path;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct History;
|
||||
|
||||
|
@ -44,89 +46,77 @@ impl Command for History {
|
|||
};
|
||||
|
||||
// todo for sqlite history this command should be an alias to `open ~/.config/nushell/history.sqlite3 | get history`
|
||||
if let Some(config_path) = nu_path::config_dir() {
|
||||
let clear = call.has_flag(engine_state, stack, "clear")?;
|
||||
let long = call.has_flag(engine_state, stack, "long")?;
|
||||
let signals = engine_state.signals().clone();
|
||||
let Some(history_path) = get_history_path(history.file_format) else {
|
||||
return Err(ShellError::ConfigDirNotFound { span: Some(head) });
|
||||
};
|
||||
let clear = call.has_flag(engine_state, stack, "clear")?;
|
||||
let long = call.has_flag(engine_state, stack, "long")?;
|
||||
let signals = engine_state.signals().clone();
|
||||
|
||||
let mut history_path = config_path;
|
||||
history_path.push("nushell");
|
||||
match history.file_format {
|
||||
HistoryFileFormat::Sqlite => {
|
||||
history_path.push("history.sqlite3");
|
||||
}
|
||||
HistoryFileFormat::PlainText => {
|
||||
history_path.push("history.txt");
|
||||
}
|
||||
}
|
||||
|
||||
if clear {
|
||||
let _ = std::fs::remove_file(history_path);
|
||||
// TODO: FIXME also clear the auxiliary files when using sqlite
|
||||
Ok(PipelineData::empty())
|
||||
} else {
|
||||
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
|
||||
HistoryFileFormat::Sqlite => {
|
||||
SqliteBackedHistory::with_file(history_path.clone(), None, None)
|
||||
.map(|inner| {
|
||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||
boxed
|
||||
})
|
||||
.ok()
|
||||
}
|
||||
|
||||
HistoryFileFormat::PlainText => FileBackedHistory::with_file(
|
||||
history.max_size as usize,
|
||||
history_path.clone(),
|
||||
)
|
||||
.map(|inner| {
|
||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||
boxed
|
||||
})
|
||||
.ok(),
|
||||
};
|
||||
|
||||
match history.file_format {
|
||||
HistoryFileFormat::PlainText => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries.into_iter().enumerate().map(move |(idx, entry)| {
|
||||
Value::record(
|
||||
record! {
|
||||
"command" => Value::string(entry.command_line, head),
|
||||
"index" => Value::int(idx as i64, head),
|
||||
},
|
||||
head,
|
||||
)
|
||||
})
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
HistoryFileFormat::Sqlite => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries.into_iter().enumerate().map(move |(idx, entry)| {
|
||||
create_history_record(idx, entry, long, head)
|
||||
})
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
}
|
||||
}
|
||||
if clear {
|
||||
let _ = std::fs::remove_file(history_path);
|
||||
// TODO: FIXME also clear the auxiliary files when using sqlite
|
||||
Ok(PipelineData::empty())
|
||||
} else {
|
||||
Err(ShellError::ConfigDirNotFound { span: Some(head) })
|
||||
let history_reader: Option<Box<dyn ReedlineHistory>> = match history.file_format {
|
||||
HistoryFileFormat::Sqlite => {
|
||||
SqliteBackedHistory::with_file(history_path.clone(), None, None)
|
||||
.map(|inner| {
|
||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||
boxed
|
||||
})
|
||||
.ok()
|
||||
}
|
||||
|
||||
HistoryFileFormat::PlainText => {
|
||||
FileBackedHistory::with_file(history.max_size as usize, history_path.clone())
|
||||
.map(|inner| {
|
||||
let boxed: Box<dyn ReedlineHistory> = Box::new(inner);
|
||||
boxed
|
||||
})
|
||||
.ok()
|
||||
}
|
||||
};
|
||||
|
||||
match history.file_format {
|
||||
HistoryFileFormat::PlainText => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries.into_iter().enumerate().map(move |(idx, entry)| {
|
||||
Value::record(
|
||||
record! {
|
||||
"command" => Value::string(entry.command_line, head),
|
||||
"index" => Value::int(idx as i64, head),
|
||||
},
|
||||
head,
|
||||
)
|
||||
})
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
HistoryFileFormat::Sqlite => Ok(history_reader
|
||||
.and_then(|h| {
|
||||
h.search(SearchQuery::everything(SearchDirection::Forward, None))
|
||||
.ok()
|
||||
})
|
||||
.map(move |entries| {
|
||||
entries
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(move |(idx, entry)| create_history_record(idx, entry, long, head))
|
||||
})
|
||||
.ok_or(ShellError::FileNotFound {
|
||||
file: history_path.display().to_string(),
|
||||
span: head,
|
||||
})?
|
||||
.into_pipeline_data(head, signals)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -240,9 +240,8 @@ pub fn eval_config_contents(
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_history_path(storage_path: &str, mode: HistoryFileFormat) -> Option<PathBuf> {
|
||||
nu_path::config_dir().map(|mut history_path| {
|
||||
history_path.push(storage_path);
|
||||
pub(crate) fn get_history_path(mode: HistoryFileFormat) -> Option<PathBuf> {
|
||||
nu_path::nu_config_dir().map(|mut history_path| {
|
||||
history_path.push(match mode {
|
||||
HistoryFileFormat::PlainText => HISTORY_FILE_TXT,
|
||||
HistoryFileFormat::Sqlite => HISTORY_FILE_SQLITE,
|
||||
|
|
|
@ -52,7 +52,6 @@ use sysinfo::System;
|
|||
pub fn evaluate_repl(
|
||||
engine_state: &mut EngineState,
|
||||
stack: Stack,
|
||||
nushell_path: &str,
|
||||
prerun_command: Option<Spanned<String>>,
|
||||
load_std_lib: Option<Spanned<String>>,
|
||||
entire_start_time: Instant,
|
||||
|
@ -99,7 +98,7 @@ pub fn evaluate_repl(
|
|||
|
||||
unique_stack.add_env_var("LAST_EXIT_CODE".into(), Value::int(0, Span::unknown()));
|
||||
|
||||
let mut line_editor = get_line_editor(engine_state, nushell_path, use_color)?;
|
||||
let mut line_editor = get_line_editor(engine_state, use_color)?;
|
||||
let temp_file = temp_dir().join(format!("{}.nu", uuid::Uuid::new_v4()));
|
||||
|
||||
if let Some(s) = prerun_command {
|
||||
|
@ -200,7 +199,7 @@ pub fn evaluate_repl(
|
|||
}
|
||||
Err(_) => {
|
||||
// line_editor is lost in the error case so reconstruct a new one
|
||||
line_editor = get_line_editor(engine_state, nushell_path, use_color)?;
|
||||
line_editor = get_line_editor(engine_state, use_color)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -208,11 +207,7 @@ pub fn evaluate_repl(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn get_line_editor(
|
||||
engine_state: &mut EngineState,
|
||||
nushell_path: &str,
|
||||
use_color: bool,
|
||||
) -> Result<Reedline> {
|
||||
fn get_line_editor(engine_state: &mut EngineState, use_color: bool) -> Result<Reedline> {
|
||||
let mut start_time = std::time::Instant::now();
|
||||
let mut line_editor = Reedline::create();
|
||||
|
||||
|
@ -223,7 +218,7 @@ fn get_line_editor(
|
|||
if let Some(history) = engine_state.history_config() {
|
||||
start_time = std::time::Instant::now();
|
||||
|
||||
line_editor = setup_history(nushell_path, engine_state, line_editor, history)?;
|
||||
line_editor = setup_history(engine_state, line_editor, history)?;
|
||||
|
||||
perf!("setup history", start_time, use_color);
|
||||
}
|
||||
|
@ -1037,7 +1032,6 @@ fn flush_engine_state_repl_buffer(engine_state: &mut EngineState, line_editor: &
|
|||
/// Setup history management for Reedline
|
||||
///
|
||||
fn setup_history(
|
||||
nushell_path: &str,
|
||||
engine_state: &mut EngineState,
|
||||
line_editor: Reedline,
|
||||
history: HistoryConfig,
|
||||
|
@ -1049,7 +1043,7 @@ fn setup_history(
|
|||
None
|
||||
};
|
||||
|
||||
if let Some(path) = crate::config_files::get_history_path(nushell_path, history.file_format) {
|
||||
if let Some(path) = crate::config_files::get_history_path(history.file_format) {
|
||||
return update_line_editor_history(
|
||||
engine_state,
|
||||
path,
|
||||
|
@ -1317,8 +1311,7 @@ fn trailing_slash_looks_like_path() {
|
|||
fn are_session_ids_in_sync() {
|
||||
let engine_state = &mut EngineState::new();
|
||||
let history = engine_state.history_config().unwrap();
|
||||
let history_path =
|
||||
crate::config_files::get_history_path("nushell", history.file_format).unwrap();
|
||||
let history_path = crate::config_files::get_history_path(history.file_format).unwrap();
|
||||
let line_editor = reedline::Reedline::create();
|
||||
let history_session_id = reedline::Reedline::create_history_session_id();
|
||||
let line_editor = update_line_editor_history(
|
||||
|
|
|
@ -2,6 +2,11 @@
|
|||
use omnipath::WinPathExt;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[deprecated(
|
||||
note = "prefer using nu_config_dir() instead of config_dir() joined with NUSHELL_FOLDER"
|
||||
)]
|
||||
pub const NUSHELL_FOLDER: &str = "nushell";
|
||||
|
||||
pub fn home_dir() -> Option<PathBuf> {
|
||||
dirs::home_dir()
|
||||
}
|
||||
|
@ -34,6 +39,15 @@ pub fn config_dir() -> Option<PathBuf> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Return the nushell config directory.
|
||||
pub fn nu_config_dir() -> Option<PathBuf> {
|
||||
config_dir().map(|mut p| {
|
||||
#[allow(deprecated)]
|
||||
p.push(NUSHELL_FOLDER);
|
||||
p
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_canonicalized_path(path: Option<PathBuf>) -> Option<PathBuf> {
|
||||
let path = path?;
|
||||
Some(canonicalize(&path).unwrap_or(path))
|
||||
|
|
|
@ -11,7 +11,12 @@ mod trailing_slash;
|
|||
|
||||
pub use components::components;
|
||||
pub use expansions::{canonicalize_with, expand_path_with, expand_to_real_path, locate_in_dirs};
|
||||
pub use helpers::{cache_dir, config_dir, data_dir, get_canonicalized_path, home_dir};
|
||||
pub use helpers::{
|
||||
cache_dir, config_dir, data_dir, get_canonicalized_path, home_dir, nu_config_dir,
|
||||
};
|
||||
pub use path::*;
|
||||
pub use tilde::expand_tilde;
|
||||
pub use trailing_slash::{has_trailing_slash, strip_trailing_slash};
|
||||
|
||||
#[allow(deprecated)]
|
||||
pub use helpers::NUSHELL_FOLDER;
|
||||
|
|
|
@ -3,6 +3,8 @@ use log::warn;
|
|||
use nu_cli::read_plugin_file;
|
||||
use nu_cli::{eval_config_contents, eval_source};
|
||||
use nu_path::canonicalize_with;
|
||||
#[allow(deprecated)]
|
||||
use nu_path::NUSHELL_FOLDER;
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack, StateWorkingSet},
|
||||
report_error, report_error_new, Config, ParseError, PipelineData, Spanned,
|
||||
|
@ -17,7 +19,6 @@ use std::{
|
|||
sync::Arc,
|
||||
};
|
||||
|
||||
pub(crate) const NUSHELL_FOLDER: &str = "nushell";
|
||||
const CONFIG_FILE: &str = "config.nu";
|
||||
const ENV_FILE: &str = "env.nu";
|
||||
const LOGINSHELL_FILE: &str = "login.nu";
|
||||
|
@ -50,6 +51,7 @@ pub(crate) fn read_config_file(
|
|||
}
|
||||
}
|
||||
} else if let Some(mut config_path) = nu_path::config_dir() {
|
||||
#[allow(deprecated)]
|
||||
config_path.push(NUSHELL_FOLDER);
|
||||
|
||||
// Create config directory if it does not exist
|
||||
|
@ -135,6 +137,7 @@ pub(crate) fn read_loginshell_file(engine_state: &mut EngineState, stack: &mut S
|
|||
|
||||
// read and execute loginshell file if exists
|
||||
if let Some(mut config_path) = nu_path::config_dir() {
|
||||
#[allow(deprecated)]
|
||||
config_path.push(NUSHELL_FOLDER);
|
||||
config_path.push(LOGINSHELL_FILE);
|
||||
|
||||
|
@ -268,6 +271,7 @@ pub(crate) fn setup_config(
|
|||
);
|
||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||
#[cfg(feature = "plugin")]
|
||||
#[allow(deprecated)]
|
||||
read_plugin_file(engine_state, plugin_file, NUSHELL_FOLDER);
|
||||
|
||||
read_config_file(engine_state, stack, env_file, true);
|
||||
|
@ -301,6 +305,7 @@ pub(crate) fn set_config_path(
|
|||
let config_path = match config_file {
|
||||
Some(s) => canonicalize_with(&s.item, cwd).ok(),
|
||||
None => nu_path::config_dir().map(|mut p| {
|
||||
#[allow(deprecated)]
|
||||
p.push(NUSHELL_FOLDER);
|
||||
let mut p = canonicalize_with(&p, cwd).unwrap_or(p);
|
||||
p.push(default_config_name);
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
#[cfg(feature = "plugin")]
|
||||
use crate::config_files::NUSHELL_FOLDER;
|
||||
use crate::{
|
||||
command,
|
||||
config_files::{self, setup_config},
|
||||
|
@ -8,6 +7,8 @@ use log::trace;
|
|||
#[cfg(feature = "plugin")]
|
||||
use nu_cli::read_plugin_file;
|
||||
use nu_cli::{evaluate_commands, evaluate_file, evaluate_repl, EvaluateCommandsOpts};
|
||||
#[allow(deprecated)]
|
||||
use nu_path::NUSHELL_FOLDER;
|
||||
use nu_protocol::{
|
||||
engine::{EngineState, Stack},
|
||||
report_error_new, PipelineData, Spanned,
|
||||
|
@ -37,6 +38,7 @@ pub(crate) fn run_commands(
|
|||
// if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files
|
||||
if parsed_nu_cli_args.no_config_file.is_none() {
|
||||
#[cfg(feature = "plugin")]
|
||||
#[allow(deprecated)]
|
||||
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER);
|
||||
|
||||
perf!("read plugins", start_time, use_color);
|
||||
|
@ -125,6 +127,7 @@ pub(crate) fn run_file(
|
|||
if parsed_nu_cli_args.no_config_file.is_none() {
|
||||
let start_time = std::time::Instant::now();
|
||||
#[cfg(feature = "plugin")]
|
||||
#[allow(deprecated)]
|
||||
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER);
|
||||
perf!("read plugins", start_time, use_color);
|
||||
|
||||
|
@ -216,7 +219,6 @@ pub(crate) fn run_repl(
|
|||
let ret_val = evaluate_repl(
|
||||
engine_state,
|
||||
stack,
|
||||
config_files::NUSHELL_FOLDER,
|
||||
parsed_nu_cli_args.execute,
|
||||
parsed_nu_cli_args.no_std_lib,
|
||||
entire_start_time,
|
||||
|
|
Loading…
Reference in New Issue
Block a user