mirror of
https://github.com/nushell/nushell.git
synced 2025-05-02 00:54:25 +02:00
# Description Currently there is a bit of chaos regarding construction of history file paths. Various pieces of code across a number of crates reimplement the same/similar logic: - There is `get_history_path`, but it requires a directory parameter (it really just joins it with a file name). - Some places use a const for the directory parameter, others use a string literal - in all cases the value seems to be `"nushell"`. - Some places assume the `"nushell"` value, other plumb it down from close to the top of the call stack. - Some places use a constant for history file names while others assume it. This PR tries to make it so that the history/config path format is defined in a single places and so dependencies on it are easier to follow: - It removes `get_history_path` and adds a `file_path` method to `HistoryConfig` instead (an extra motivation being, this is a convenient place that can be used from all creates that need a history file path) - Adds a `nu_config_dir` function that returns the nushell configuration directory. - Updates existing code to rely on the above, effectively removing duplicate uses of `"nushell"` and `NUSHELL_FOLDER` and assumptions about file names associated with different history formats # User-Facing Changes None
37 lines
1.1 KiB
Rust
37 lines
1.1 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use crate::AbsolutePathBuf;
|
|
|
|
pub fn home_dir() -> Option<AbsolutePathBuf> {
|
|
dirs::home_dir().and_then(|home| AbsolutePathBuf::try_from(home).ok())
|
|
}
|
|
|
|
/// Return the data directory for the current platform or XDG_DATA_HOME if specified.
|
|
pub fn data_dir() -> Option<AbsolutePathBuf> {
|
|
configurable_dir_path("XDG_DATA_HOME", dirs::data_dir)
|
|
}
|
|
|
|
/// Return the cache directory for the current platform or XDG_CACHE_HOME if specified.
|
|
pub fn cache_dir() -> Option<AbsolutePathBuf> {
|
|
configurable_dir_path("XDG_CACHE_HOME", dirs::cache_dir)
|
|
}
|
|
|
|
/// Return the nushell config directory.
|
|
pub fn nu_config_dir() -> Option<AbsolutePathBuf> {
|
|
configurable_dir_path("XDG_CONFIG_HOME", dirs::config_dir).map(|mut p| {
|
|
p.push("nushell");
|
|
p
|
|
})
|
|
}
|
|
|
|
fn configurable_dir_path(
|
|
name: &'static str,
|
|
dir: impl FnOnce() -> Option<PathBuf>,
|
|
) -> Option<AbsolutePathBuf> {
|
|
std::env::var(name)
|
|
.ok()
|
|
.and_then(|path| AbsolutePathBuf::try_from(path).ok())
|
|
.or_else(|| dir().and_then(|path| AbsolutePathBuf::try_from(path).ok()))
|
|
.map(|path| path.canonicalize().map(Into::into).unwrap_or(path))
|
|
}
|