From 10f2eed693d6418ec3f477080f06b0d31420f2fd Mon Sep 17 00:00:00 2001
From: Jeff Zhao <jeff.no.zhao@gmail.com>
Date: Sun, 30 May 2021 22:57:30 -0400
Subject: [PATCH] add additional configuration directories
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

 - in order of precedence:
   - $JOSHUTO_CONFIG_DIR
   - $XDG_CONFIG_HOME/joshuto
   - $HOME/.config/joshuto

Signed-off-by: Alexander Miroshnichenko <a.miroshnichenko@rbk.money>
---
 src/main.rs | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 1716ece75e07..a3e0f3658683 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -34,16 +34,32 @@ const PREVIEW_FILE: &str = "preview.toml";
 lazy_static! {
     // dynamically builds the config hierarchy
     static ref CONFIG_HIERARCHY: Vec<PathBuf> = {
-        let mut temp = vec![];
-        match xdg::BaseDirectories::with_prefix(PROGRAM_NAME) {
-            Ok(dirs) => temp.push(dirs.get_config_home()),
-            Err(e) => eprintln!("{}", e),
-        };
+        let mut config_dirs = vec![];
+
+        if let Ok(p) = std::env::var("JOSHUTO_CONFIG_HOME") {
+            let p = PathBuf::from(p);
+            if p.is_dir() {
+                config_dirs.push(p);
+            }
+        }
+
+        if let Ok(dirs) = xdg::BaseDirectories::with_prefix(PROGRAM_NAME) {
+            config_dirs.push(dirs.get_config_home());
+        }
+
+        if let Ok(p) = std::env::var("HOME") {
+            let mut p = PathBuf::from(p);
+            p.push(".config/joshuto");
+            if p.is_dir() {
+                config_dirs.push(p);
+            }
+        }
+
         // adds the default config files to the config hierarchy if running through cargo
         if cfg!(debug_assertions) {
-            temp.push(PathBuf::from("./config"));
+            config_dirs.push(PathBuf::from("./config"));
         }
-        temp
+        config_dirs
     };
     static ref THEME_T: AppTheme = AppTheme::get_config(THEME_FILE);
     static ref MIMETYPE_T: AppMimetypeRegistry = AppMimetypeRegistry::get_config(MIMETYPE_FILE);
@@ -80,9 +96,8 @@ fn run_joshuto(args: Args) -> Result<(), JoshutoError> {
     let config = AppConfig::get_config(CONFIG_FILE);
     let keymap = AppKeyMapping::get_config(KEYMAP_FILE);
 
-    let mut context = AppContext::new(config);
-
     {
+        let mut context = AppContext::new(config);
         let mut backend: ui::TuiBackend = ui::TuiBackend::new()?;
         run(&mut backend, &mut context, keymap)?;
     }
-- 
2.26.2

