فهرست منبع

fix: regression in theme config location, simplify path

PThorpe92 1 سال پیش
والد
کامیت
4bb2c0f108
2فایلهای تغییر یافته به همراه38 افزوده شده و 40 حذف شده
  1. 16 22
      src/options/config.rs
  2. 22 18
      src/options/theme.rs

+ 16 - 22
src/options/config.rs

@@ -12,17 +12,21 @@ use serde_norway;
 use std::collections::HashMap;
 use std::collections::HashMap;
 use std::path::PathBuf;
 use std::path::PathBuf;
 
 
-#[derive(Debug, Default, Eq, PartialEq)]
+#[derive(Debug, Eq, PartialEq)]
 pub struct ThemeConfig {
 pub struct ThemeConfig {
     // This is rather bare for now, will be expanded with config file
     // This is rather bare for now, will be expanded with config file
-    location: ConfigLoc,
+    location: PathBuf,
 }
 }
 
 
-#[derive(Debug, Default, PartialEq, Eq)]
-pub enum ConfigLoc {
-    #[default]
-    Default, // $XDG_CONFIG_HOME/eza/config|theme.yml
-    Env(PathBuf), // $EZA_CONFIG_DIR
+impl Default for ThemeConfig {
+    fn default() -> Self {
+        ThemeConfig {
+            location: dirs::config_dir()
+                .unwrap_or_default()
+                .join("eza")
+                .join("theme.yml"),
+        }
+    }
 }
 }
 
 
 trait FromOverride<T>: Sized {
 trait FromOverride<T>: Sized {
@@ -600,23 +604,13 @@ impl FromOverride<UiStylesOverride> for UiStyles {
     }
     }
 }
 }
 impl ThemeConfig {
 impl ThemeConfig {
-    pub fn from_path(path: &str) -> Self {
-        let path = PathBuf::from(path);
-        ThemeConfig {
-            location: ConfigLoc::Env(path),
-        }
+    pub fn from_path(path: PathBuf) -> Self {
+        ThemeConfig { location: path }
     }
     }
     pub fn to_theme(&self) -> Option<UiStyles> {
     pub fn to_theme(&self) -> Option<UiStyles> {
-        let ui_styles_override: Option<UiStylesOverride> = match &self.location {
-            ConfigLoc::Default => {
-                let path = dirs::config_dir()?.join("eza").join("theme.yml");
-                let file = std::fs::File::open(path).ok()?;
-                serde_norway::from_reader(&file).ok()
-            }
-            ConfigLoc::Env(path) => {
-                let file = std::fs::File::open(path).ok()?;
-                serde_norway::from_reader(&file).ok()
-            }
+        let ui_styles_override: Option<UiStylesOverride> = {
+            let file = std::fs::File::open(&self.location).ok()?;
+            serde_norway::from_reader(&file).ok()
         };
         };
         FromOverride::from(ui_styles_override, Some(UiStyles::default()))
         FromOverride::from(ui_styles_override, Some(UiStyles::default()))
     }
     }

+ 22 - 18
src/options/theme.rs

@@ -35,26 +35,30 @@ impl Options {
 
 
 impl ThemeConfig {
 impl ThemeConfig {
     fn deduce<V: Vars>(vars: &V) -> Option<Self> {
     fn deduce<V: Vars>(vars: &V) -> Option<Self> {
-        let (base_path, is_default_location) = if let Some(path) = vars.get("EZA_CONFIG_DIR") {
-            (PathBuf::from(path), false)
-        } else if let Some(path) = dirs::config_dir() {
-            (path.join("eza"), true)
+        if let Some(path) = vars.get("EZA_CONFIG_DIR") {
+            let path = PathBuf::from(path);
+            let theme = path.join("theme.yml");
+            if theme.exists() {
+                return Some(ThemeConfig::from_path(theme));
+            }
+            let theme = path.join("theme.yaml");
+            if theme.exists() {
+                return Some(ThemeConfig::from_path(theme));
+            }
+            None
         } else {
         } else {
-            return None;
-        };
-        let yml_path = base_path.join("theme.yml");
-        if yml_path.exists() {
-            return if is_default_location {
-                Some(ThemeConfig::from_path(&yml_path.to_string_lossy()))
-            } else {
-                Some(ThemeConfig::default())
-            };
-        }
-        let yaml_path = base_path.join("theme.yaml");
-        if yaml_path.exists() {
-            return Some(ThemeConfig::from_path(&yaml_path.to_string_lossy()));
+            let path = dirs::config_dir().unwrap_or_default();
+            let path = path.join("eza");
+            let theme = path.join("theme.yml");
+            if theme.exists() {
+                return Some(ThemeConfig::default());
+            }
+            let theme = path.join("theme.yaml");
+            if theme.exists() {
+                return Some(ThemeConfig::from_path(theme));
+            }
+            None
         }
         }
-        None
     }
     }
 }
 }