diff options
author | Canop <cano.petrole@gmail.com> | 2020-06-22 20:21:12 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2020-06-22 20:21:12 +0200 |
commit | 440c136f1f7e029720b68769c2569785fd52d41a (patch) | |
tree | b7daae6401a87aa9c3c82f2a00ba691f1e10c767 /src/conf/mod.rs | |
parent | d21fbdebbb0c58bac98e69419c8c4deb7d37a27a (diff) |
config directory for new installs on mac is ~/.config/broot
Fix #103
Diffstat (limited to 'src/conf/mod.rs')
-rw-r--r-- | src/conf/mod.rs | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/src/conf/mod.rs b/src/conf/mod.rs index 6b1b112..c0a3b58 100644 --- a/src/conf/mod.rs +++ b/src/conf/mod.rs @@ -1,11 +1,10 @@ use { - directories::ProjectDirs, + directories, std::{ - path::PathBuf, + path::{Path, PathBuf}, }, }; - mod conf; mod default_conf; @@ -14,12 +13,48 @@ pub use { }; /// return the instance of ProjectDirs holding broot's specific paths -pub fn app_dirs() -> ProjectDirs { - ProjectDirs::from("org", "dystroy", "broot") +pub fn app_dirs() -> directories::ProjectDirs { + directories::ProjectDirs::from("org", "dystroy", "broot") .expect("Unable to find configuration directories") } -/// return the path to the config directory, based on XDG -pub fn dir() -> PathBuf { +#[cfg(not(target_os = "macos"))] +fn find_conf_dir() -> PathBuf { app_dirs().config_dir().to_path_buf() } + +#[cfg(target_os = "macos")] +fn find_conf_dir() -> PathBuf { + if let Some(user_dirs) = directories::UserDirs::new() { + // We first search in ~/.config/broot which should be the prefered solution + let prefered = user_dirs.home_dir().join(".config/broot"); + if prefered.exists() { + return prefered; + } + // The directories crate has a non usual choice of config directory, + // especially for a CLI application. We use it only when + // the prefered directory doesn't exist and this one exists. + // See https://github.com/Canop/broot/issues/103 + let second_choice = app_dirs().config_dir().to_path_buf(); + if second_choice.exists() { + // An older version of broot was used to write the + // config, we don't want to lose it. + return second_choice; + } + // Either the config has been scraped or it's a new installation + return prefered; + } else { + // there's no home. There are probably other problems too but here we + // are just looking for a place for our config, not for a shelter for all + // so the default will do + app_dirs().config_dir().to_path_buf() + } +} + +/// return the path to the config directory +pub fn dir() -> &'static Path { + lazy_static! { + static ref CONF_DIR: PathBuf = find_conf_dir(); + } + &*CONF_DIR +} |