Merge pull request #2548 from utkarshgupta137/master

Replace `dirs` crate with `etcetera` crate
This commit is contained in:
David Peter 2023-09-01 21:41:24 +02:00 committed by GitHub
commit 3abc0c0fc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 93 deletions

71
Cargo.lock generated
View File

@ -129,13 +129,14 @@ dependencies = [
"clircle", "clircle",
"console", "console",
"content_inspector", "content_inspector",
"dirs",
"encoding_rs", "encoding_rs",
"etcetera",
"expect-test", "expect-test",
"flate2", "flate2",
"git2", "git2",
"globset", "globset",
"grep-cli", "grep-cli",
"home",
"nix", "nix",
"nu-ansi-term", "nu-ansi-term",
"once_cell", "once_cell",
@ -338,26 +339,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" 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]] [[package]]
name = "dissimilar" name = "dissimilar"
version = "1.0.5" version = "1.0.5"
@ -412,6 +393,17 @@ dependencies = [
"libc", "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]] [[package]]
name = "expect-test" name = "expect-test"
version = "1.4.1" version = "1.4.1"
@ -472,17 +464,6 @@ dependencies = [
"percent-encoding", "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]] [[package]]
name = "git-version" name = "git-version"
version = "0.3.5" version = "0.3.5"
@ -559,6 +540,15 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" 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]] [[package]]
name = "idna" name = "idna"
version = "0.3.0" version = "0.3.0"
@ -923,17 +913,6 @@ dependencies = [
"bitflags 1.3.2", "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]] [[package]]
name = "regex" name = "regex"
version = "1.8.3" version = "1.8.3"
@ -1343,12 +1322,6 @@ dependencies = [
"winapi-util", "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]] [[package]]
name = "wild" name = "wild"
version = "2.1.0" version = "2.1.0"

View File

@ -26,7 +26,7 @@ application = [
# Be aware that the included features might change in the future # Be aware that the included features might change in the future
minimal-application = [ minimal-application = [
"clap", "clap",
"dirs", "etcetera",
"paging", "paging",
"regex-onig", "regex-onig",
"wild", "wild",
@ -58,7 +58,7 @@ semver = "1.0"
path_abs = { version = "0.5", default-features = false } path_abs = { version = "0.5", default-features = false }
clircle = "0.4" clircle = "0.4"
bugreport = { version = "0.5.0", optional = true } 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 } grep-cli = { version = "0.1.9", optional = true }
regex = { version = "1.8.3", optional = true } regex = { version = "1.8.3", optional = true }
walkdir = { version = "2.3", optional = true } walkdir = { version = "2.3", optional = true }
@ -81,7 +81,7 @@ optional = true
features = ["wrap_help", "cargo"] features = ["wrap_help", "cargo"]
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
dirs = "5.0.0" home = "0.5.4"
plist = "1.4.3" plist = "1.4.3"
[dev-dependencies] [dev-dependencies]

View File

@ -404,7 +404,7 @@ fn macos_dark_mode_active() -> bool {
const PREFERENCES_FILE: &str = "Library/Preferences/.GlobalPreferences.plist"; const PREFERENCES_FILE: &str = "Library/Preferences/.GlobalPreferences.plist";
const STYLE_KEY: &str = "AppleInterfaceStyle"; const STYLE_KEY: &str = "AppleInterfaceStyle";
let preferences_file = dirs::home_dir() let preferences_file = home::home_dir()
.map(|home| home.join(PREFERENCES_FILE)) .map(|home| home.join(PREFERENCES_FILE))
.expect("Could not get home directory"); .expect("Could not get home directory");

View File

@ -1,12 +1,11 @@
use std::env; use std::env;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use etcetera::BaseStrategy;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification. /// Wrapper for 'etcetera' that checks BAT_CACHE_PATH and BAT_CONFIG_DIR and falls back to the
/// The `XDG_CACHE_HOME` environment variable is checked first. `BAT_CONFIG_DIR` /// Windows known folder locations on Windows & the XDG Base Directory Specification everywhere else.
/// is then checked before the `XDG_CONFIG_HOME` environment variable.
/// The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
pub struct BatProjectDirs { pub struct BatProjectDirs {
cache_dir: PathBuf, cache_dir: PathBuf,
config_dir: PathBuf, config_dir: PathBuf,
@ -14,25 +13,24 @@ pub struct BatProjectDirs {
impl BatProjectDirs { impl BatProjectDirs {
fn new() -> Option<BatProjectDirs> { fn new() -> Option<BatProjectDirs> {
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 // Checks whether or not `$BAT_CACHE_PATH` exists. If it doesn't, set the cache dir to our
// to our system's default configuration home. // system's default cache home.
let config_dir = let cache_dir = if let Some(cache_dir) = env::var_os("BAT_CACHE_PATH").map(PathBuf::from) {
if let Some(config_dir_op) = env::var_os("BAT_CONFIG_DIR").map(PathBuf::from) { cache_dir
config_dir_op } else {
} else { basedirs.cache_dir().join("bat")
#[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")));
#[cfg(not(target_os = "macos"))] // Checks whether or not `$BAT_CONFIG_DIR` exists. If it doesn't, set the config dir to our
let config_dir_op = dirs::config_dir(); // system's default configuration home.
let config_dir = if let Some(config_dir) = env::var_os("BAT_CONFIG_DIR").map(PathBuf::from)
config_dir_op.map(|d| d.join("bat"))? {
}; config_dir
} else {
basedirs.config_dir().join("bat")
};
Some(BatProjectDirs { Some(BatProjectDirs {
cache_dir, cache_dir,
@ -40,25 +38,6 @@ impl BatProjectDirs {
}) })
} }
fn get_cache_dir() -> Option<PathBuf> {
// 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 { pub fn cache_dir(&self) -> &Path {
&self.cache_dir &self.cache_dir
} }