mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
..
This commit is contained in:
parent
d3204c2c96
commit
d6744b4cb9
4 changed files with 33 additions and 39 deletions
|
|
@ -28,7 +28,6 @@ static MERGED_KEYMAP: RoCell<String> = RoCell::new();
|
||||||
static MERGED_THEME: RoCell<String> = RoCell::new();
|
static MERGED_THEME: RoCell<String> = RoCell::new();
|
||||||
|
|
||||||
pub static LAYOUT: RoCell<arc_swap::ArcSwap<Layout>> = 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 KEYMAP: RoCell<keymap::Keymap> = RoCell::new();
|
||||||
pub static LOG: RoCell<log::Log> = RoCell::new();
|
pub static LOG: RoCell<log::Log> = RoCell::new();
|
||||||
|
|
@ -49,8 +48,6 @@ pub fn init() {
|
||||||
MERGED_THEME.init(Preset::theme(&config_dir));
|
MERGED_THEME.init(Preset::theme(&config_dir));
|
||||||
|
|
||||||
LAYOUT.with(Default::default);
|
LAYOUT.with(Default::default);
|
||||||
FLAVOR.with(Default::default);
|
|
||||||
FLAVOR.merge_with(&MERGED_THEME, &config_dir);
|
|
||||||
|
|
||||||
KEYMAP.with(Default::default);
|
KEYMAP.with(Default::default);
|
||||||
LOG.with(Default::default);
|
LOG.with(Default::default);
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,35 @@
|
||||||
use std::{mem, path::{Path, PathBuf}};
|
use std::{mem, path::{Path, PathBuf}};
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
use toml::{Table, Value};
|
use toml::{Table, Value};
|
||||||
|
|
||||||
|
use crate::theme::Flavor;
|
||||||
|
|
||||||
pub(crate) struct Preset;
|
pub(crate) struct Preset;
|
||||||
|
|
||||||
impl 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 {
|
pub(crate) fn keymap(p: &Path) -> String {
|
||||||
Self::merge_path(p.join("keymap.toml"), include_str!("../preset/keymap.toml"))
|
Self::merge_path(p.join("keymap.toml"), include_str!("../preset/keymap.toml"))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn theme(p: &Path) -> String {
|
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]
|
let p = p.join(format!("flavors/{}.yazi/flavor.toml", use_));
|
||||||
pub(crate) fn yazi(p: &Path) -> String {
|
let flavor = std::fs::read_to_string(&p)
|
||||||
Self::merge_path(p.join("yazi.toml"), include_str!("../preset/yazi.toml"))
|
.with_context(|| format!("Failed to load flavor {:?}", p))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Self::merge_str(&user, &Self::merge_str(&flavor, include_str!("../preset/theme.toml")))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,4 @@
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use anyhow::Context;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use yazi_shared::RoCell;
|
|
||||||
|
|
||||||
use crate::{Preset, MERGED_THEME};
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct Flavor {
|
pub struct Flavor {
|
||||||
|
|
@ -12,28 +6,18 @@ pub struct Flavor {
|
||||||
pub use_: String,
|
pub use_: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Flavor {
|
impl Flavor {
|
||||||
fn default() -> Self {
|
pub fn parse_name(s: &str) -> Option<String> {
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Outer {
|
struct Outer {
|
||||||
flavor: Flavor,
|
flavor: Inner,
|
||||||
|
}
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Inner {
|
||||||
|
#[serde(rename = "use")]
|
||||||
|
pub use_: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
toml::from_str::<Outer>(&MERGED_THEME).unwrap().flavor
|
toml::from_str::<Outer>(s).ok().map(|o| o.flavor.use_).filter(|s| !s.is_empty())
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,12 @@ use serde::{Deserialize, Serialize};
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
use yazi_shared::{fs::expand_path, Xdg};
|
use yazi_shared::{fs::expand_path, Xdg};
|
||||||
|
|
||||||
use super::{Filetype, Icon, Style};
|
use super::{Filetype, Flavor, Icon, Style};
|
||||||
use crate::{validation::check_validation, FLAVOR, MERGED_THEME};
|
use crate::{validation::check_validation, MERGED_THEME};
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct Theme {
|
pub struct Theme {
|
||||||
|
pub flavor: Flavor,
|
||||||
pub manager: Manager,
|
pub manager: Manager,
|
||||||
status: Status,
|
status: Status,
|
||||||
pub input: Input,
|
pub input: Input,
|
||||||
|
|
@ -32,11 +33,11 @@ impl Default for Theme {
|
||||||
check_validation(theme.manager.validate());
|
check_validation(theme.manager.validate());
|
||||||
check_validation(theme.which.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);
|
theme.manager.syntect_theme = expand_path(&theme.manager.syntect_theme);
|
||||||
} else {
|
} else {
|
||||||
theme.manager.syntect_theme =
|
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
|
theme
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue