This commit is contained in:
sxyazi 2024-02-29 10:49:27 +08:00
parent d3204c2c96
commit d6744b4cb9
No known key found for this signature in database
4 changed files with 33 additions and 39 deletions

View file

@ -28,7 +28,6 @@ static MERGED_KEYMAP: RoCell<String> = RoCell::new();
static MERGED_THEME: RoCell<String> = RoCell::new();
pub static LAYOUT: RoCell<arc_swap::ArcSwap<Layout>> = RoCell::new();
pub static FLAVOR: RoCell<theme::Flavor> = RoCell::new();
pub static KEYMAP: RoCell<keymap::Keymap> = RoCell::new();
pub static LOG: RoCell<log::Log> = 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);

View file

@ -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]

View file

@ -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<String> {
#[derive(Deserialize)]
struct Outer {
flavor: Flavor,
flavor: Inner,
}
#[derive(Deserialize)]
struct Inner {
#[serde(rename = "use")]
pub use_: String,
}
toml::from_str::<Outer>(&MERGED_THEME).unwrap().flavor
}
}
impl Flavor {
pub fn merge_with(&self, merged: &RoCell<String>, 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::<Outer>(s).ok().map(|o| o.flavor.use_).filter(|s| !s.is_empty())
}
}

View file

@ -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