on master help sign in
Specify cache dir with BAT_CACHE_DIR

- if set, BAT_CACHE_DIR is used. otherwise use OS default.

commita9a31dca7fce553d0180ae33f6229922a4354900 authorKyle Criddle <criddle.kyle@gmail.com> dateSun 15 Mar 2020, 19:52 (7 years ago) parent9a9902b
1 file changed+23−13
src/dirs.rs+23−13
src/dirs.rs+23−13
@@ -1,8 +1,6 @@
11use crate::dirs_rs;
2−use std::path::{Path, PathBuf};
3−
4−#[cfg(target_os = "macos")]
52use std::env;
3+use std::path::{Path, PathBuf};
64
75/// Wrapper for 'dirs' that treats MacOS more like Linux, by following the XDG specification.
86/// This means that the `XDG_CACHE_HOME` and `XDG_CONFIG_HOME` environment variables are
@@ -14,16 +12,7 @@ pub struct BatProjectDirs {
1412
1513impl BatProjectDirs {
1614 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")));
22−
23− #[cfg(not(target_os = "macos"))]
24− let cache_dir_op = dirs_rs::cache_dir();
25−
26− let cache_dir = cache_dir_op.map(|d| d.join("bat"))?;
15+ let cache_dir = BatProjectDirs::get_cache_dir()?;
2716
2817 #[cfg(target_os = "macos")]
2918 let config_dir_op = env::var_os("XDG_CONFIG_HOME")
@@ -42,6 +31,27 @@ impl BatProjectDirs {
4231 })
4332 }
4433
34+ pub fn get_cache_dir() -> Option<PathBuf> {
35+ // on all OS prefer BAT_CACHE_PATH if set
36+ let cache_dir_op = env::var_os("BAT_CACHE_PATH")
37+ .map(PathBuf::from)
38+ .filter(|p| p.is_absolute());
39+ if !cache_dir_op.is_none() {
40+ return cache_dir_op;
41+ }
42+
43+ #[cfg(target_os = "macos")]
44+ let cache_dir_op = env::var_os("XDG_CACHE_HOME")
45+ .map(PathBuf::from)
46+ .filter(|p| p.is_absolute())
47+ .or_else(|| dirs_rs::home_dir().map(|d| d.join(".cache")));
48+
49+ #[cfg(not(target_os = "macos"))]
50+ let cache_dir_op = dirs_rs::cache_dir();
51+
52+ Some(cache_dir_op.map(|d| d.join("bat")))?
53+ }
54+
4555 pub fn cache_dir(&self) -> &Path {
4656 &self.cache_dir
4757 }