support xdg for cache and data

This commit is contained in:
Darren Schroeder 2024-06-11 07:31:46 -05:00
parent 6d598667b8
commit 8ae40af674
5 changed files with 21 additions and 9 deletions

View File

@ -195,6 +195,7 @@ reedline = { workspace = true, features = ["bashisms", "sqlite"] }
crossterm = { workspace = true }
ctrlc = { workspace = true }
dirs-next = { workspace = true }
log = { workspace = true }
miette = { workspace = true, features = ["fancy-no-backtrace", "fancy"] }
mimalloc = { version = "0.1.42", default-features = false, optional = true }

View File

@ -6,26 +6,36 @@ pub fn home_dir() -> Option<PathBuf> {
dirs_next::home_dir()
}
/// Return the data directory for the current platform or XDG_DATA_HOME if specified.
pub fn data_dir() -> Option<PathBuf> {
dirs_next::data_dir()
match std::env::var("XDG_DATA_HOME").map(PathBuf::from) {
Ok(xdg_data) if xdg_data.is_absolute() => Some(canonicalize(&xdg_data).unwrap_or(xdg_data)),
_ => get_canonicalized_path(dirs_next::data_dir()),
}
}
/// Return the cache directory for the current platform or XDG_CACHE_HOME if specified.
pub fn cache_dir() -> Option<PathBuf> {
dirs_next::cache_dir()
match std::env::var("XDG_CACHE_HOME").map(PathBuf::from) {
Ok(xdg_cache) if xdg_cache.is_absolute() => {
Some(canonicalize(&xdg_cache).unwrap_or(xdg_cache))
}
_ => get_canonicalized_path(dirs_next::cache_dir()),
}
}
/// Return the config directory for the current platform or XDG_CONFIG_HOME if specified.
pub fn config_dir() -> Option<PathBuf> {
match std::env::var("XDG_CONFIG_HOME").map(PathBuf::from) {
Ok(xdg_config) if xdg_config.is_absolute() => {
Some(canonicalize(&xdg_config).unwrap_or(xdg_config))
}
_ => config_dir_old(),
_ => get_canonicalized_path(dirs_next::config_dir()),
}
}
/// Get the old default config directory. Outside of Linux, this will ignore `XDG_CONFIG_HOME`
pub fn config_dir_old() -> Option<PathBuf> {
let path = dirs_next::config_dir()?;
pub fn get_canonicalized_path(path: Option<PathBuf>) -> Option<PathBuf> {
let path = path?;
Some(canonicalize(&path).unwrap_or(path))
}

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

View File

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

View File

@ -104,7 +104,9 @@ fn main() -> Result<()> {
default: nushell_config_path.display().to_string(),
},
);
} else if let Some(old_config) = nu_path::config_dir_old().map(|p| p.join("nushell")) {
} else if let Some(old_config) =
nu_path::get_canonicalized_path(dirs_next::config_dir()).map(|p| p.join("nushell"))
{
let xdg_config_empty = nushell_config_path
.read_dir()
.map_or(true, |mut dir| dir.next().is_none());