2019-02-05 14:34:08 -05:00
|
|
|
use dirs_rs;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
2019-02-05 12:58:06 -05:00
|
|
|
use std::env;
|
2019-02-05 14:34:08 -05:00
|
|
|
#[cfg(target_os = "macos")]
|
2019-02-05 12:58:06 -05:00
|
|
|
use std::ffi::OsString;
|
2019-01-31 12:53:10 -05:00
|
|
|
|
2019-02-05 12:58:06 -05:00
|
|
|
/// Wrapper for dirs that treats MacOS more like Linux.
|
|
|
|
/// First, env variables `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` are checked and the fall back is
|
|
|
|
/// `~/.cache/bat` and `~/.config/bat`.
|
2019-01-31 12:53:10 -05:00
|
|
|
pub struct BatProjectDirs {
|
|
|
|
cache_dir: PathBuf,
|
|
|
|
config_dir: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BatProjectDirs {
|
|
|
|
fn new() -> Option<BatProjectDirs> {
|
|
|
|
#[cfg(target_os = "macos")]
|
2019-02-05 12:58:06 -05:00
|
|
|
let cache_dir_op = env::var_os("XDG_CACHE_HOME")
|
2019-02-07 21:05:59 +01:00
|
|
|
.map(PathBuf::from)
|
|
|
|
.filter(|p| p.is_absolute())
|
2019-02-05 14:34:08 -05:00
|
|
|
.or_else(|| dirs_rs::home_dir().map(|d| d.join(".cache")));
|
2019-01-31 12:53:10 -05:00
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
2019-02-05 12:58:06 -05:00
|
|
|
let cache_dir_op = dirs_rs::cache_dir();
|
|
|
|
|
|
|
|
let cache_dir = match cache_dir_op {
|
|
|
|
Some(d) => d.join("bat"),
|
2019-01-31 12:53:10 -05:00
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
2019-02-05 12:58:06 -05:00
|
|
|
let config_dir_op = env::var_os("XDG_CONFIG_HOME")
|
2019-02-07 21:05:59 +01:00
|
|
|
.map(PathBuf::from)
|
|
|
|
.filter(|p| p.is_absolute())
|
2019-02-05 14:34:08 -05:00
|
|
|
.or_else(|| dirs_rs::home_dir().map(|d| d.join(".config")));
|
2019-01-31 12:53:10 -05:00
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
2019-02-05 12:58:06 -05:00
|
|
|
let config_dir_op = dirs_rs::config_dir();
|
|
|
|
|
|
|
|
let config_dir = match config_dir_op {
|
|
|
|
Some(d) => d.join("bat"),
|
2019-01-31 12:53:10 -05:00
|
|
|
None => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(BatProjectDirs {
|
|
|
|
cache_dir,
|
|
|
|
config_dir,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cache_dir(&self) -> &Path {
|
|
|
|
&self.cache_dir
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn config_dir(&self) -> &Path {
|
|
|
|
&self.config_dir
|
|
|
|
}
|
|
|
|
}
|
2018-10-07 23:22:42 +02:00
|
|
|
|
|
|
|
lazy_static! {
|
2019-01-31 12:53:10 -05:00
|
|
|
pub static ref PROJECT_DIRS: BatProjectDirs =
|
|
|
|
BatProjectDirs::new().expect("Could not get home directory");
|
2018-10-07 23:22:42 +02:00
|
|
|
}
|