From d6744b4cb9daed1e29790a1e5041cd4b75e398fe Mon Sep 17 00:00:00 2001 From: sxyazi Date: Thu, 29 Feb 2024 10:49:27 +0800 Subject: [PATCH] .. --- yazi-config/src/lib.rs | 3 --- yazi-config/src/preset.rs | 26 ++++++++++++++++++------- yazi-config/src/theme/flavor.rs | 34 +++++++++------------------------ yazi-config/src/theme/theme.rs | 9 +++++---- 4 files changed, 33 insertions(+), 39 deletions(-) diff --git a/yazi-config/src/lib.rs b/yazi-config/src/lib.rs index bda794f2..117de23b 100644 --- a/yazi-config/src/lib.rs +++ b/yazi-config/src/lib.rs @@ -28,7 +28,6 @@ static MERGED_KEYMAP: RoCell = RoCell::new(); static MERGED_THEME: RoCell = RoCell::new(); pub static LAYOUT: RoCell> = RoCell::new(); -pub static FLAVOR: RoCell = RoCell::new(); pub static KEYMAP: RoCell = RoCell::new(); pub static LOG: RoCell = RoCell::new(); @@ -49,8 +48,6 @@ pub fn init() { MERGED_THEME.init(Preset::theme(&config_dir)); LAYOUT.with(Default::default); - FLAVOR.with(Default::default); - FLAVOR.merge_with(&MERGED_THEME, &config_dir); KEYMAP.with(Default::default); LOG.with(Default::default); diff --git a/yazi-config/src/preset.rs b/yazi-config/src/preset.rs index 9d10e563..f6d24470 100644 --- a/yazi-config/src/preset.rs +++ b/yazi-config/src/preset.rs @@ -1,23 +1,35 @@ use std::{mem, path::{Path, PathBuf}}; +use anyhow::Context; use toml::{Table, Value}; +use crate::theme::Flavor; + pub(crate) struct Preset; impl Preset { - #[inline] + pub(crate) fn yazi(p: &Path) -> String { + Self::merge_path(p.join("yazi.toml"), include_str!("../preset/yazi.toml")) + } + pub(crate) fn keymap(p: &Path) -> String { Self::merge_path(p.join("keymap.toml"), include_str!("../preset/keymap.toml")) } - #[inline] pub(crate) fn theme(p: &Path) -> String { - Self::merge_path(p.join("theme.toml"), include_str!("../preset/theme.toml")) - } + let Ok(user) = std::fs::read_to_string(p.join("theme.toml")) else { + return include_str!("../preset/theme.toml").to_owned(); + }; + let Some(use_) = Flavor::parse_name(&user) else { + return Self::merge_str(&user, include_str!("../preset/theme.toml")); + }; - #[inline] - pub(crate) fn yazi(p: &Path) -> String { - Self::merge_path(p.join("yazi.toml"), include_str!("../preset/yazi.toml")) + let p = p.join(format!("flavors/{}.yazi/flavor.toml", use_)); + let flavor = std::fs::read_to_string(&p) + .with_context(|| format!("Failed to load flavor {:?}", p)) + .unwrap(); + + Self::merge_str(&user, &Self::merge_str(&flavor, include_str!("../preset/theme.toml"))) } #[inline] diff --git a/yazi-config/src/theme/flavor.rs b/yazi-config/src/theme/flavor.rs index 975e7e86..b5f3f9e7 100644 --- a/yazi-config/src/theme/flavor.rs +++ b/yazi-config/src/theme/flavor.rs @@ -1,10 +1,4 @@ -use std::path::Path; - -use anyhow::Context; use serde::{Deserialize, Serialize}; -use yazi_shared::RoCell; - -use crate::{Preset, MERGED_THEME}; #[derive(Deserialize, Serialize)] pub struct Flavor { @@ -12,28 +6,18 @@ pub struct Flavor { pub use_: String, } -impl Default for Flavor { - fn default() -> Self { +impl Flavor { + pub fn parse_name(s: &str) -> Option { #[derive(Deserialize)] struct Outer { - flavor: Flavor, + flavor: Inner, + } + #[derive(Deserialize)] + struct Inner { + #[serde(rename = "use")] + pub use_: String, } - toml::from_str::(&MERGED_THEME).unwrap().flavor - } -} - -impl Flavor { - pub fn merge_with(&self, merged: &RoCell, p: &Path) { - if self.use_.is_empty() { - return; - } - - let path = p.join(format!("flavors/{}.yazi/theme.toml", self.use_)); - let s = std::fs::read_to_string(&path) - .with_context(|| format!("Failed to load flavor from: {:?}", path)) - .unwrap(); - - merged.replace(Preset::merge_str(&s, merged)); + toml::from_str::(s).ok().map(|o| o.flavor.use_).filter(|s| !s.is_empty()) } } diff --git a/yazi-config/src/theme/theme.rs b/yazi-config/src/theme/theme.rs index e743418c..2bca9098 100644 --- a/yazi-config/src/theme/theme.rs +++ b/yazi-config/src/theme/theme.rs @@ -4,11 +4,12 @@ use serde::{Deserialize, Serialize}; use validator::Validate; use yazi_shared::{fs::expand_path, Xdg}; -use super::{Filetype, Icon, Style}; -use crate::{validation::check_validation, FLAVOR, MERGED_THEME}; +use super::{Filetype, Flavor, Icon, Style}; +use crate::{validation::check_validation, MERGED_THEME}; #[derive(Deserialize, Serialize)] pub struct Theme { + pub flavor: Flavor, pub manager: Manager, status: Status, pub input: Input, @@ -32,11 +33,11 @@ impl Default for Theme { check_validation(theme.manager.validate()); check_validation(theme.which.validate()); - if FLAVOR.use_.is_empty() { + if theme.flavor.use_.is_empty() { theme.manager.syntect_theme = expand_path(&theme.manager.syntect_theme); } else { theme.manager.syntect_theme = - Xdg::config_dir().join(format!("flavors/{}.yazi/tmtheme.xml", FLAVOR.use_)); + Xdg::config_dir().join(format!("flavors/{}.yazi/tmtheme.xml", theme.flavor.use_)); } theme