Reduce duplication in history path construction (#13475)

# 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
This commit is contained in:
Piotr Kufel
2024-10-11 05:51:50 -07:00
committed by GitHub
parent 9f714e62cb
commit bcb7ef48b6
12 changed files with 82 additions and 145 deletions

View File

@ -1,3 +1,5 @@
use std::path::PathBuf;
use crate::AbsolutePathBuf;
pub fn home_dir() -> Option<AbsolutePathBuf> {
@ -6,27 +8,29 @@ pub fn home_dir() -> Option<AbsolutePathBuf> {
/// Return the data directory for the current platform or XDG_DATA_HOME if specified.
pub fn data_dir() -> Option<AbsolutePathBuf> {
std::env::var("XDG_DATA_HOME")
.ok()
.and_then(|path| AbsolutePathBuf::try_from(path).ok())
.or_else(|| dirs::data_dir().and_then(|path| AbsolutePathBuf::try_from(path).ok()))
.map(|path| path.canonicalize().map(Into::into).unwrap_or(path))
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> {
std::env::var("XDG_CACHE_HOME")
.ok()
.and_then(|path| AbsolutePathBuf::try_from(path).ok())
.or_else(|| dirs::cache_dir().and_then(|path| AbsolutePathBuf::try_from(path).ok()))
.map(|path| path.canonicalize().map(Into::into).unwrap_or(path))
configurable_dir_path("XDG_CACHE_HOME", dirs::cache_dir)
}
/// Return the config directory for the current platform or XDG_CONFIG_HOME if specified.
pub fn config_dir() -> Option<AbsolutePathBuf> {
std::env::var("XDG_CONFIG_HOME")
/// 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(|| dirs::config_dir().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))
}

View File

@ -11,7 +11,7 @@ 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, home_dir};
pub use helpers::{cache_dir, data_dir, home_dir, nu_config_dir};
pub use path::*;
pub use tilde::expand_tilde;
pub use trailing_slash::{has_trailing_slash, strip_trailing_slash};