@@ -4,8 +4,9 @@ use std::path::{Path, PathBuf};
44use lazy_static::lazy_static;
55
66/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
7−/// This means that the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables are
8−/// checked first. The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
7+/// The `XDG_CACHE_HOME` environment variable is checked first. `BAT_CONFIG_DIR`
8+/// is then checked before the `XDG_CONFIG_HOME` environment variable.
9+/// The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
910pub struct BatProjectDirs {
1011 cache_dir: PathBuf,
1112 config_dir: PathBuf,
@@ -14,17 +15,24 @@ pub struct BatProjectDirs {
1415impl BatProjectDirs {
1516 fn new() -> Option<BatProjectDirs> {
1617 let cache_dir = BatProjectDirs::get_cache_dir()?;
18+
19+ // Checks whether or not $BAT_CONFIG_DIR exists. If it doesn't, set our config dir
20+ // to our system's default configuration home.
21+ let config_dir = if let Some(config_dir_op) = env::var_os("BAT_CONFIG_DIR")
22+ .map(PathBuf::from) {
23+ config_dir_op
24+ } else {
25+ #[cfg(target_os = "macos")]
26+ let config_dir_op = env::var_os("XDG_CONFIG_HOME")
27+ .map(PathBuf::from)
28+ .filter(|p| p.is_absolute())
29+ .or_else(|| dirs_next::home_dir().map(|d| d.join(".config")));
1730
18− #[cfg(target_os = "macos")]
19− let config_dir_op = env::var_os("XDG_CONFIG_HOME")
20− .map(PathBuf::from)
21− .filter(|p| p.is_absolute())
22− .or_else(|| dirs_next::home_dir().map(|d| d.join(".config")));
23−
24− #[cfg(not(target_os = "macos"))]
25− let config_dir_op = dirs_next::config_dir();
31+ #[cfg(not(target_os = "macos"))]
32+ let config_dir_op = dirs_next::config_dir();
2633
27− let config_dir = config_dir_op.map(|d| d.join("bat"))?;
34+ config_dir_op.map(|d| d.join("bat"))?
35+ };
2836
2937 Some(BatProjectDirs {
3038 cache_dir,