on a736889 help sign in
bat/src/dirs.rs
57 lines 1.7 kB history raw
1use crate::dirs_rs;
2use std::path::{Path, PathBuf};
4#[cfg(target_os = "macos")]
5use std::env;
7/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
8/// This means that the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables are
9/// checked first. The fallback directories are `~/.cache/bat` and `~/.config/bat`, respectively.
10pub struct BatProjectDirs {
11 cache_dir: PathBuf,
12 config_dir: PathBuf,
15impl BatProjectDirs {
16 fn new() -> Option<BatProjectDirs> {
17 #[cfg(target_os = "macos")]
18 let cache_dir_op = env::var_os("XDG_CACHE_HOME")
19 .map(PathBuf::from)
20 .filter(|p| p.is_absolute())
21 .or_else(|| dirs_rs::home_dir().map(|d| d.join(".cache")));
23 #[cfg(not(target_os = "macos"))]
24 let cache_dir_op = dirs_rs::cache_dir();
26 let cache_dir = cache_dir_op.map(|d| d.join("bat"))?;
28 #[cfg(target_os = "macos")]
29 let config_dir_op = env::var_os("XDG_CONFIG_HOME")
30 .map(PathBuf::from)
31 .filter(|p| p.is_absolute())
32 .or_else(|| dirs_rs::home_dir().map(|d| d.join(".config")));
34 #[cfg(not(target_os = "macos"))]
35 let config_dir_op = dirs_rs::config_dir();
37 let config_dir = config_dir_op.map(|d| d.join("bat"))?;
39 Some(BatProjectDirs {
40 cache_dir,
41 config_dir,
42 })
43 }
45 pub fn cache_dir(&self) -> &Path {
46 &self.cache_dir
47 }
49 pub fn config_dir(&self) -> &Path {
50 &self.config_dir
51 }
54lazy_static! {
55 pub static ref PROJECT_DIRS: BatProjectDirs =
56 BatProjectDirs::new().expect("Could not get home directory");