mirror of
https://github.com/sxyazi/yazi.git
synced 2024-12-18 06:11:31 +03:00
perf: cache each file's icon to avoid redundant calculations at rendering (#931)
This commit is contained in:
parent
80000cfd86
commit
55da9e342c
@ -1,7 +1,7 @@
|
|||||||
use serde::{Deserialize, Deserializer};
|
use serde::{Deserialize, Deserializer};
|
||||||
use yazi_shared::fs::File;
|
use yazi_shared::{fs::File, theme::{Color, Style, StyleShadow}};
|
||||||
|
|
||||||
use super::{Color, Is, Style, StyleShadow};
|
use super::Is;
|
||||||
use crate::Pattern;
|
use crate::Pattern;
|
||||||
|
|
||||||
pub struct Filetype {
|
pub struct Filetype {
|
||||||
|
@ -1,14 +1,20 @@
|
|||||||
use serde::{Deserialize, Deserializer};
|
use std::ops::Deref;
|
||||||
use yazi_shared::fs::File;
|
|
||||||
|
|
||||||
use super::Style;
|
use serde::{Deserialize, Deserializer};
|
||||||
use crate::{preset::Preset, theme::{Color, Is, StyleShadow}, Pattern};
|
use yazi_shared::{fs::File, theme::{Color, StyleShadow}};
|
||||||
|
|
||||||
|
use crate::{preset::Preset, theme::Is, Pattern};
|
||||||
|
|
||||||
pub struct Icon {
|
pub struct Icon {
|
||||||
pub is: Is,
|
is: Is,
|
||||||
pub name: Pattern,
|
name: Pattern,
|
||||||
pub text: String,
|
inner: yazi_shared::theme::Icon,
|
||||||
pub style: Style,
|
}
|
||||||
|
|
||||||
|
impl Deref for Icon {
|
||||||
|
type Target = yazi_shared::theme::Icon;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target { &self.inner }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Icon {
|
impl Icon {
|
||||||
@ -61,8 +67,10 @@ impl Icon {
|
|||||||
.map(|r| Icon {
|
.map(|r| Icon {
|
||||||
is: r.is,
|
is: r.is,
|
||||||
name: r.name,
|
name: r.name,
|
||||||
text: r.text,
|
inner: yazi_shared::theme::Icon {
|
||||||
style: StyleShadow { fg: r.fg, ..Default::default() }.into(),
|
text: r.text,
|
||||||
|
style: StyleShadow { fg: r.fg, ..Default::default() }.into(),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
)
|
)
|
||||||
|
@ -1,15 +1,11 @@
|
|||||||
mod color;
|
|
||||||
mod filetype;
|
mod filetype;
|
||||||
mod flavor;
|
mod flavor;
|
||||||
mod icon;
|
mod icon;
|
||||||
mod is;
|
mod is;
|
||||||
mod style;
|
|
||||||
mod theme;
|
mod theme;
|
||||||
|
|
||||||
pub use color::*;
|
|
||||||
pub use filetype::*;
|
pub use filetype::*;
|
||||||
pub use flavor::*;
|
pub use flavor::*;
|
||||||
pub use icon::*;
|
pub use icon::*;
|
||||||
pub use is::*;
|
pub use is::*;
|
||||||
pub use style::*;
|
|
||||||
pub use theme::*;
|
pub use theme::*;
|
||||||
|
@ -2,9 +2,9 @@ use std::path::PathBuf;
|
|||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
use yazi_shared::{fs::expand_path, Xdg};
|
use yazi_shared::{fs::expand_path, theme::Style, Xdg};
|
||||||
|
|
||||||
use super::{Filetype, Flavor, Icon, Style};
|
use super::{Filetype, Flavor, Icon};
|
||||||
use crate::{validation::check_validation, MERGED_THEME};
|
use crate::{validation::check_validation, MERGED_THEME};
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
|
@ -58,7 +58,17 @@ impl File {
|
|||||||
Some(lua.create_string(p.as_path().as_os_str().as_encoded_bytes())).transpose()
|
Some(lua.create_string(p.as_path().as_os_str().as_encoded_bytes())).transpose()
|
||||||
});
|
});
|
||||||
reg.add_method("icon", |lua, me, ()| {
|
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, ()| {
|
reg.add_method("style", |lua, me, ()| {
|
||||||
let cx = lua.named_registry_value::<CtxRef>("cx")?;
|
let cx = lua.named_registry_value::<CtxRef>("cx")?;
|
||||||
|
@ -7,7 +7,7 @@ pub struct Icon;
|
|||||||
|
|
||||||
impl Icon {
|
impl Icon {
|
||||||
pub fn register(lua: &Lua) -> mlua::Result<()> {
|
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("text", |lua, me| lua.create_string(&me.text));
|
||||||
reg.add_field_method_get("style", |_, me| Ok(Style::from(me.style)));
|
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>(
|
fn cast<'lua>(
|
||||||
lua: &'lua Lua,
|
lua: &'lua Lua,
|
||||||
data: &'static yazi_config::theme::Icon,
|
data: &'static yazi_shared::theme::Icon,
|
||||||
) -> mlua::Result<AnyUserData<'lua>> {
|
) -> mlua::Result<AnyUserData<'lua>> {
|
||||||
lua.create_any_userdata(data)
|
lua.create_any_userdata(data)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use mlua::{AnyUserData, ExternalError, FromLua, Lua, Table, UserData, UserDataMethods, Value};
|
use mlua::{AnyUserData, ExternalError, FromLua, Lua, Table, UserData, UserDataMethods, Value};
|
||||||
use yazi_config::theme::Color;
|
use yazi_shared::theme::Color;
|
||||||
|
|
||||||
use super::Style;
|
use super::Style;
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use mlua::{AnyUserData, ExternalError, ExternalResult, Lua, Table, UserData, UserDataMethods, Value};
|
use mlua::{AnyUserData, ExternalError, ExternalResult, Lua, Table, UserData, UserDataMethods, Value};
|
||||||
use yazi_config::theme::Color;
|
use yazi_shared::theme::Color;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Default)]
|
#[derive(Clone, Copy, Default)]
|
||||||
pub struct Style(pub(super) ratatui::style::Style);
|
pub struct Style(pub(super) ratatui::style::Style);
|
||||||
@ -17,8 +17,8 @@ impl Style {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<yazi_config::theme::Style> for Style {
|
impl From<yazi_shared::theme::Style> for Style {
|
||||||
fn from(value: yazi_config::theme::Style) -> Self { Self(value.into()) }
|
fn from(value: yazi_shared::theme::Style) -> Self { Self(value.into()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TryFrom<Table<'a>> for Style {
|
impl<'a> TryFrom<Table<'a>> for Style {
|
||||||
|
@ -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 anyhow::Result;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
|
||||||
use crate::fs::{Cha, ChaKind, Url};
|
use crate::{fs::{Cha, ChaKind, Url}, theme::IconCache};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
pub struct File {
|
pub struct File {
|
||||||
pub url: Url,
|
pub url: Url,
|
||||||
pub cha: Cha,
|
pub cha: Cha,
|
||||||
pub link_to: Option<Url>,
|
pub link_to: Option<Url>,
|
||||||
|
pub icon: Cell<IconCache>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for File {
|
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]
|
#[inline]
|
||||||
|
@ -13,6 +13,7 @@ mod number;
|
|||||||
mod os;
|
mod os;
|
||||||
mod ro_cell;
|
mod ro_cell;
|
||||||
pub mod term;
|
pub mod term;
|
||||||
|
pub mod theme;
|
||||||
mod throttle;
|
mod throttle;
|
||||||
mod time;
|
mod time;
|
||||||
mod xdg;
|
mod xdg;
|
||||||
|
@ -3,7 +3,7 @@ use std::str::FromStr;
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Deserialize)]
|
#[derive(Clone, Copy, Debug, Deserialize)]
|
||||||
#[serde(try_from = "String")]
|
#[serde(try_from = "String")]
|
||||||
pub struct Color(ratatui::style::Color);
|
pub struct Color(ratatui::style::Color);
|
||||||
|
|
15
yazi-shared/src/theme/icon.rs
Normal file
15
yazi-shared/src/theme/icon.rs
Normal 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),
|
||||||
|
}
|
7
yazi-shared/src/theme/mod.rs
Normal file
7
yazi-shared/src/theme/mod.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
mod color;
|
||||||
|
mod icon;
|
||||||
|
mod style;
|
||||||
|
|
||||||
|
pub use color::*;
|
||||||
|
pub use icon::*;
|
||||||
|
pub use style::*;
|
@ -3,7 +3,7 @@ use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};
|
|||||||
|
|
||||||
use super::Color;
|
use super::Color;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Deserialize)]
|
#[derive(Clone, Copy, Debug, Deserialize)]
|
||||||
#[serde(from = "StyleShadow")]
|
#[serde(from = "StyleShadow")]
|
||||||
pub struct Style {
|
pub struct Style {
|
||||||
pub fg: Option<Color>,
|
pub fg: Option<Color>,
|
||||||
@ -37,29 +37,29 @@ impl From<Style> for ratatui::style::Style {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Deserialize)]
|
#[derive(Default, Deserialize)]
|
||||||
pub(super) struct StyleShadow {
|
pub struct StyleShadow {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(super) fg: Option<Color>,
|
pub fg: Option<Color>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(super) bg: Option<Color>,
|
pub bg: Option<Color>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(super) bold: bool,
|
pub bold: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(super) dim: bool,
|
pub dim: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(super) italic: bool,
|
pub italic: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(super) underline: bool,
|
pub underline: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(super) blink: bool,
|
pub blink: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(super) blink_rapid: bool,
|
pub blink_rapid: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(super) reversed: bool,
|
pub reversed: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(super) hidden: bool,
|
pub hidden: bool,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub(super) crossed: bool,
|
pub crossed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<StyleShadow> for Style {
|
impl From<StyleShadow> for Style {
|
Loading…
Reference in New Issue
Block a user