This commit is contained in:
sxyazi 2024-04-20 15:36:43 +08:00
parent 331d3185f3
commit 17ef7901f1
No known key found for this signature in database
10 changed files with 53 additions and 19 deletions

View file

@ -1,13 +1,20 @@
use std::ops::Deref;
use serde::{Deserialize, Deserializer};
use yazi_shared::{fs::File, theme::{Color, Style, StyleShadow}};
use yazi_shared::{fs::File, theme::{Color, StyleShadow}};
use crate::{preset::Preset, theme::Is, Pattern};
pub struct Icon {
is: Is,
name: Pattern,
pub text: String,
pub style: Style,
inner: yazi_shared::theme::Icon,
}
impl Deref for Icon {
type Target = yazi_shared::theme::Icon;
fn deref(&self) -> &Self::Target { &self.inner }
}
impl Icon {
@ -60,8 +67,10 @@ impl Icon {
.map(|r| Icon {
is: r.is,
name: r.name,
inner: yazi_shared::theme::Icon {
text: r.text,
style: StyleShadow { fg: r.fg, ..Default::default() }.into(),
},
})
.collect(),
)

View file

@ -58,7 +58,17 @@ impl File {
Some(lua.create_string(p.as_path().as_os_str().as_encoded_bytes())).transpose()
});
reg.add_method("icon", |lua, me, ()| {
THEME.icons.iter().find(|&x| x.matches(me)).map(|x| Icon::cast(lua, x)).transpose()
use yazi_shared::theme::IconCache;
match me.icon.get() {
IconCache::Missing => {
let matched = THEME.icons.iter().find(|&i| i.matches(me));
me.icon.set(matched.map_or(IconCache::Undefined, |i| IconCache::Icon(i)));
matched.map(|i| Icon::cast(lua, i)).transpose()
}
IconCache::Undefined => Ok(None),
IconCache::Icon(cached) => Some(Icon::cast(lua, cached)).transpose(),
}
});
reg.add_method("style", |lua, me, ()| {
let cx = lua.named_registry_value::<CtxRef>("cx")?;

View file

@ -7,7 +7,7 @@ pub struct Icon;
impl Icon {
pub fn register(lua: &Lua) -> mlua::Result<()> {
lua.register_userdata_type::<&yazi_config::theme::Icon>(|reg| {
lua.register_userdata_type::<&yazi_shared::theme::Icon>(|reg| {
reg.add_field_method_get("text", |lua, me| lua.create_string(&me.text));
reg.add_field_method_get("style", |_, me| Ok(Style::from(me.style)));
})?;
@ -16,10 +16,10 @@ impl Icon {
}
}
impl Cast<&'static yazi_config::theme::Icon> for Icon {
impl Cast<&'static yazi_shared::theme::Icon> for Icon {
fn cast<'lua>(
lua: &'lua Lua,
data: &'static yazi_config::theme::Icon,
data: &'static yazi_shared::theme::Icon,
) -> mlua::Result<AnyUserData<'lua>> {
lua.create_any_userdata(data)
}

View file

@ -1,15 +1,16 @@
use std::{ffi::OsStr, fs::Metadata, ops::Deref};
use std::{cell::Cell, ffi::OsStr, fs::Metadata, ops::Deref};
use anyhow::Result;
use tokio::fs;
use crate::fs::{Cha, ChaKind, Url};
use crate::{fs::{Cha, ChaKind, Url}, theme::IconCache};
#[derive(Clone, Debug, Default)]
pub struct File {
pub url: Url,
pub cha: Cha,
pub link_to: Option<Url>,
pub icon: Cell<IconCache>,
}
impl Deref for File {
@ -53,7 +54,7 @@ impl File {
}
}
Self { url, cha: Cha::from(meta).with_kind(ck), link_to }
Self { url, cha: Cha::from(meta).with_kind(ck), link_to, icon: Default::default() }
}
#[inline]

View file

@ -1 +0,0 @@
pub struct Icon {}

View file

@ -1,7 +1,6 @@
mod cha;
mod file;
mod fns;
mod icon;
mod op;
mod path;
mod url;
@ -9,7 +8,6 @@ mod url;
pub use cha::*;
pub use file::*;
pub use fns::*;
pub use icon::*;
pub use op::*;
pub use path::*;
pub use url::*;

View file

@ -3,7 +3,7 @@ use std::str::FromStr;
use anyhow::Result;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Deserialize)]
#[derive(Clone, Copy, Debug, Deserialize)]
#[serde(try_from = "String")]
pub struct Color(ratatui::style::Color);

View file

@ -0,0 +1,15 @@
use super::Style;
#[derive(Clone, Debug)]
pub struct Icon {
pub text: String,
pub style: Style,
}
#[derive(Clone, Copy, Debug, Default)]
pub enum IconCache {
#[default]
Missing,
Undefined,
Icon(&'static Icon),
}

View file

@ -1,5 +1,7 @@
mod color;
mod icon;
mod style;
pub use color::*;
pub use icon::*;
pub use style::*;

View file

@ -3,7 +3,7 @@ use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};
use super::Color;
#[derive(Clone, Copy, Deserialize)]
#[derive(Clone, Copy, Debug, Deserialize)]
#[serde(from = "StyleShadow")]
pub struct Style {
pub fg: Option<Color>,