add $nu.data-path and $nu.cache-path

This commit is contained in:
Darren Schroeder 2024-06-10 12:59:17 -05:00
parent af22bb8d52
commit af083a9198
6 changed files with 64 additions and 5 deletions

View File

@ -763,11 +763,13 @@ fn variables_completions() {
// Test completions for $nu
let suggestions = completer.complete("$nu.", 4);
assert_eq!(15, suggestions.len());
assert_eq!(17, suggestions.len());
let expected: Vec<String> = vec![
"cache-path".into(),
"config-path".into(),
"current-exe".into(),
"data-path".into(),
"default-config-dir".into(),
"env-path".into(),
"history-enabled".into(),

View File

@ -6,6 +6,14 @@ pub fn home_dir() -> Option<PathBuf> {
dirs_next::home_dir()
}
pub fn data_dir() -> Option<PathBuf> {
dirs_next::data_dir()
}
pub fn cache_dir() -> Option<PathBuf> {
dirs_next::cache_dir()
}
pub fn config_dir() -> Option<PathBuf> {
match std::env::var("XDG_CONFIG_HOME").map(PathBuf::from) {
Ok(xdg_config) if xdg_config.is_absolute() => {

View File

@ -8,6 +8,6 @@ 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::{config_dir, config_dir_old, home_dir};
pub use helpers::{cache_dir, config_dir, config_dir_old, data_dir, home_dir};
pub use tilde::expand_tilde;
pub use trailing_slash::{has_trailing_slash, strip_trailing_slash};

View File

@ -149,6 +149,36 @@ pub(crate) fn create_nu_constant(engine_state: &EngineState, span: Span) -> Valu
},
);
record.push(
"data-path",
if let Some(path) = nu_path::data_dir() {
let canon_data_path = canonicalize_path(engine_state, &path);
Value::string(canon_data_path.to_string_lossy(), span)
} else {
Value::error(
ShellError::IOError {
msg: "Could not get data path".into(),
},
span,
)
},
);
record.push(
"cache-path",
if let Some(path) = nu_path::cache_dir() {
let canon_cache_path = canonicalize_path(engine_state, &path);
Value::string(canon_cache_path.to_string_lossy(), span)
} else {
Value::error(
ShellError::IOError {
msg: "Could not get cache path".into(),
},
span,
)
},
);
record.push("temp-path", {
let canon_temp_path = canonicalize_path(engine_state, &std::env::temp_dir());
Value::string(canon_temp_path.to_string_lossy(), span)

View File

@ -77,6 +77,8 @@ $env.ENV_CONVERSIONS = {
# The default for this is $nu.default-config-dir/scripts
$env.NU_LIB_DIRS = [
($nu.default-config-dir | path join 'scripts') # add <nushell-config-dir>/scripts
($nu.data-path | path join 'nushell' 'completions') # default home for nushell completions
($nu.cache-path | path join 'nushell') # default home for nushell cache data, not used atm
]
# Directories to search for plugin binaries when calling register

View File

@ -125,13 +125,30 @@ fn main() -> Result<()> {
}
}
let default_nushell_completions_path = if let Some(mut path) = nu_path::data_dir() {
path.push("nushell");
path.push("completions");
path
} else {
std::path::PathBuf::new()
};
let default_nushell_cache_path = if let Some(mut path) = nu_path::cache_dir() {
path.push("nushell");
path
} else {
std::path::PathBuf::new()
};
let mut default_nu_lib_dirs_path = nushell_config_path.clone();
default_nu_lib_dirs_path.push("scripts");
engine_state.add_env_var(
"NU_LIB_DIRS".to_string(),
Value::test_list(vec![Value::test_string(
default_nu_lib_dirs_path.to_string_lossy(),
)]),
Value::test_list(vec![
Value::test_string(default_nu_lib_dirs_path.to_string_lossy()),
Value::test_string(default_nushell_completions_path.to_string_lossy()),
Value::test_string(default_nushell_cache_path.to_string_lossy()),
]),
);
let mut default_nu_plugin_dirs_path = nushell_config_path;