@@ -4,8 +4,9 @@ use std::path::{Path, PathBuf};
4use lazy_static::lazy_static;
4use lazy_static::lazy_static;
6/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
6/// 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
7+/// The `XDG_CACHE_HOME` environment variable is checked first. `BAT_CONFIG_DIR`
8+/// is then checked before the `XDG_CONFIG_HOME` environment variable.
8−/// checked first. The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
9+/// The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
9pub struct BatProjectDirs {
10pub struct BatProjectDirs {
10 cache_dir: PathBuf,
11 cache_dir: PathBuf,
11 config_dir: PathBuf,
12 config_dir: PathBuf,
@@ -14,17 +15,24 @@ pub struct BatProjectDirs {
14impl BatProjectDirs {
15impl BatProjectDirs {
15 fn new() -> Option<BatProjectDirs> {
16 fn new() -> Option<BatProjectDirs> {
16 let cache_dir = BatProjectDirs::get_cache_dir()?;
17 let cache_dir = BatProjectDirs::get_cache_dir()?;
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) {
25+ #[cfg(target_os = "macos")]
26+ let config_dir_op = env::var_os("XDG_CONFIG_HOME")
28+ .filter(|p| p.is_absolute())
29+ .or_else(|| dirs_next::home_dir().map(|d| d.join(".config")));
18− #[cfg(target_os = "macos")]
19− let config_dir_op = env::var_os("XDG_CONFIG_HOME")
21− .filter(|p| p.is_absolute())
22− .or_else(|| dirs_next::home_dir().map(|d| d.join(".config")));
24− #[cfg(not(target_os = "macos"))]
31+ #[cfg(not(target_os = "macos"))]
25− let config_dir_op = dirs_next::config_dir();
32+ let config_dir_op = dirs_next::config_dir();
27− let config_dir = config_dir_op.map(|d| d.join("bat"))?;
34+ config_dir_op.map(|d| d.join("bat"))?
29 Some(BatProjectDirs {
37 Some(BatProjectDirs {
30 cache_dir,
38 cache_dir,