From a721cc31f3932e6933adb86b2b02361e310af498 Mon Sep 17 00:00:00 2001 From: Utkarsh Gupta Date: Sat, 22 Apr 2023 05:41:57 +0530 Subject: [PATCH] Replace `dirs` crate with `etcetera` crate --- Cargo.lock | 71 ++++++++++++-------------------------- Cargo.toml | 6 ++-- src/assets.rs | 2 +- src/bin/bat/directories.rs | 59 ++++++++++--------------------- 4 files changed, 45 insertions(+), 93 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fa5b7e44..cc17cefc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,13 +129,14 @@ dependencies = [ "clircle", "console", "content_inspector", - "dirs", "encoding_rs", + "etcetera", "expect-test", "flate2", "git2", "globset", "grep-cli", + "home", "nix", "nu-ansi-term", "once_cell", @@ -338,26 +339,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" -[[package]] -name = "dirs" -version = "5.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dece029acd3353e3a58ac2e3eb3c8d6c35827a892edc6cc4138ef9c33df46ecd" -dependencies = [ - "dirs-sys", -] - -[[package]] -name = "dirs-sys" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04414300db88f70d74c5ff54e50f9e1d1737d9a5b90f53fcf2e95ca2a9ab554b" -dependencies = [ - "libc", - "redox_users", - "windows-sys 0.45.0", -] - [[package]] name = "dissimilar" version = "1.0.5" @@ -412,6 +393,17 @@ dependencies = [ "libc", ] +[[package]] +name = "etcetera" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "136d1b5283a1ab77bd9257427ffd09d8667ced0570b6f938942bc7568ed5b943" +dependencies = [ + "cfg-if", + "home", + "windows-sys 0.48.0", +] + [[package]] name = "expect-test" version = "1.4.1" @@ -472,17 +464,6 @@ dependencies = [ "percent-encoding", ] -[[package]] -name = "getrandom" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - [[package]] name = "git-version" version = "0.3.5" @@ -559,6 +540,15 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "home" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" +dependencies = [ + "windows-sys 0.48.0", +] + [[package]] name = "idna" version = "0.3.0" @@ -923,17 +913,6 @@ dependencies = [ "bitflags 1.3.2", ] -[[package]] -name = "redox_users" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" -dependencies = [ - "getrandom", - "redox_syscall 0.2.16", - "thiserror", -] - [[package]] name = "regex" version = "1.8.3" @@ -1343,12 +1322,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "wasi" -version = "0.11.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" - [[package]] name = "wild" version = "2.1.0" diff --git a/Cargo.toml b/Cargo.toml index 1f8bc755..a22d20b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ application = [ # Be aware that the included features might change in the future minimal-application = [ "clap", - "dirs", + "etcetera", "paging", "regex-onig", "wild", @@ -58,7 +58,7 @@ semver = "1.0" path_abs = { version = "0.5", default-features = false } clircle = "0.4" bugreport = { version = "0.5.0", optional = true } -dirs = { version = "5.0.0", optional = true } +etcetera = { version = "0.8.0", optional = true } grep-cli = { version = "0.1.9", optional = true } regex = { version = "1.8.3", optional = true } walkdir = { version = "2.3", optional = true } @@ -81,7 +81,7 @@ optional = true features = ["wrap_help", "cargo"] [target.'cfg(target_os = "macos")'.dependencies] -dirs = "5.0.0" +home = "0.5.4" plist = "1.4.3" [dev-dependencies] diff --git a/src/assets.rs b/src/assets.rs index 595d301b..7ce1a8f6 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -404,7 +404,7 @@ fn macos_dark_mode_active() -> bool { const PREFERENCES_FILE: &str = "Library/Preferences/.GlobalPreferences.plist"; const STYLE_KEY: &str = "AppleInterfaceStyle"; - let preferences_file = dirs::home_dir() + let preferences_file = home::home_dir() .map(|home| home.join(PREFERENCES_FILE)) .expect("Could not get home directory"); diff --git a/src/bin/bat/directories.rs b/src/bin/bat/directories.rs index d8f29153..f3fe4b98 100644 --- a/src/bin/bat/directories.rs +++ b/src/bin/bat/directories.rs @@ -1,12 +1,11 @@ use std::env; use std::path::{Path, PathBuf}; +use etcetera::BaseStrategy; use once_cell::sync::Lazy; -/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification. -/// The `XDG_CACHE_HOME` environment variable is checked first. `BAT_CONFIG_DIR` -/// is then checked before the `XDG_CONFIG_HOME` environment variable. -/// The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively. +/// Wrapper for 'etcetera' that checks BAT_CACHE_PATH and BAT_CONFIG_DIR and falls back to the +/// Windows known folder locations on Windows & the XDG Base Directory Specification everywhere else. pub struct BatProjectDirs { cache_dir: PathBuf, config_dir: PathBuf, @@ -14,25 +13,24 @@ pub struct BatProjectDirs { impl BatProjectDirs { fn new() -> Option { - let cache_dir = BatProjectDirs::get_cache_dir()?; + let basedirs = etcetera::choose_base_strategy().ok()?; - // Checks whether or not $BAT_CONFIG_DIR exists. If it doesn't, set our config dir - // to our system's default configuration home. - let config_dir = - if let Some(config_dir_op) = env::var_os("BAT_CONFIG_DIR").map(PathBuf::from) { - config_dir_op - } else { - #[cfg(target_os = "macos")] - let config_dir_op = env::var_os("XDG_CONFIG_HOME") - .map(PathBuf::from) - .filter(|p| p.is_absolute()) - .or_else(|| dirs::home_dir().map(|d| d.join(".config"))); + // Checks whether or not `$BAT_CACHE_PATH` exists. If it doesn't, set the cache dir to our + // system's default cache home. + let cache_dir = if let Some(cache_dir) = env::var_os("BAT_CACHE_PATH").map(PathBuf::from) { + cache_dir + } else { + basedirs.cache_dir().join("bat") + }; - #[cfg(not(target_os = "macos"))] - let config_dir_op = dirs::config_dir(); - - config_dir_op.map(|d| d.join("bat"))? - }; + // Checks whether or not `$BAT_CONFIG_DIR` exists. If it doesn't, set the config dir to our + // system's default configuration home. + let config_dir = if let Some(config_dir) = env::var_os("BAT_CONFIG_DIR").map(PathBuf::from) + { + config_dir + } else { + basedirs.config_dir().join("bat") + }; Some(BatProjectDirs { cache_dir, @@ -40,25 +38,6 @@ impl BatProjectDirs { }) } - fn get_cache_dir() -> Option { - // on all OS prefer BAT_CACHE_PATH if set - let cache_dir_op = env::var_os("BAT_CACHE_PATH").map(PathBuf::from); - if cache_dir_op.is_some() { - return cache_dir_op; - } - - #[cfg(target_os = "macos")] - let cache_dir_op = env::var_os("XDG_CACHE_HOME") - .map(PathBuf::from) - .filter(|p| p.is_absolute()) - .or_else(|| dirs::home_dir().map(|d| d.join(".cache"))); - - #[cfg(not(target_os = "macos"))] - let cache_dir_op = dirs::cache_dir(); - - cache_dir_op.map(|d| d.join("bat")) - } - pub fn cache_dir(&self) -> &Path { &self.cache_dir }