diff --git a/yazi-plugin/src/elements/elements.rs b/yazi-plugin/src/elements/elements.rs index 3ce23e65..2b7a96a4 100644 --- a/yazi-plugin/src/elements/elements.rs +++ b/yazi-plugin/src/elements/elements.rs @@ -71,9 +71,12 @@ pub(super) fn lines(lua: &Lua) -> mlua::Result { let b = s.as_bytes(); let s = &*String::from_utf8_lossy(&b); + let parsed; let tab_size = opts.raw_get("tab_size")?; + let mut it = if opts.raw_get("ansi")? { - LineIter::parsed(s.to_text().into_lua_err()?.lines, tab_size) + parsed = s.to_text().into_lua_err()?.lines; + LineIter::parsed(&parsed, tab_size) } else { LineIter::source(&s, tab_size) }; diff --git a/yazi-plugin/src/external/highlighter.rs b/yazi-plugin/src/external/highlighter.rs index be980fcb..3dd15a49 100644 --- a/yazi-plugin/src/external/highlighter.rs +++ b/yazi-plugin/src/external/highlighter.rs @@ -8,7 +8,6 @@ use yazi_shared::{Id, Ids, errors::PeekError, replace_to_printable}; use yazi_shim::ratatui::LineIter; static INCR: Ids = Ids::new(); -static SYNTECT: OnceLock<(Theme, SyntaxSet)> = OnceLock::new(); pub struct Highlighter { path: PathBuf, @@ -37,8 +36,10 @@ impl Highlighter { where P: Into, { + static CACHE: OnceLock<(Theme, SyntaxSet)> = OnceLock::new(); + let path = path.into(); - let (theme, syntaxes) = Self::load(); + let (theme, syntaxes) = CACHE.get_or_init(Self::load); Ok(Self { reader: BufReader::new(std::fs::File::open(&path)?), @@ -117,9 +118,9 @@ impl Highlighter { let h = self.inner.get_or_insert_with(|| HighlightLines::new(syntax, self.theme)); let s = String::from_utf8_lossy(buf); - let line = Self::to_line_widget(h.highlight_line(&s, self.syntaxes)?); + let line = [Self::to_line_widget(h.highlight_line(&s, self.syntaxes)?)]; - let mut it = LineIter::parsed(vec![line], YAZI.preview.tab_size); + let mut it = LineIter::parsed(&line, YAZI.preview.tab_size); if let Some(wrap) = YAZI.preview.wrap.into() { it = it.wrapped(wrap, self.size.width); } @@ -141,20 +142,15 @@ impl Highlighter { if self.ticket != INCR.current() { Err("Highlighting cancelled".into()) } else { Ok(()) } } - fn load() -> (&'static Theme, &'static SyntaxSet) { - let f = || { - let theme = std::fs::File::open(&THEME.mgr.syntect_theme) - .map_err(LoadingError::Io) - .and_then(|f| ThemeSet::load_from_reader(&mut std::io::BufReader::new(f))) - .or_else(|_| ThemeSet::load_from_reader(&mut Cursor::new(yazi_prebuilt::ansi_theme()))); + fn load() -> (Theme, SyntaxSet) { + let theme = std::fs::File::open(&THEME.mgr.syntect_theme) + .map_err(LoadingError::Io) + .and_then(|f| ThemeSet::load_from_reader(&mut std::io::BufReader::new(f))) + .or_else(|_| ThemeSet::load_from_reader(&mut Cursor::new(yazi_prebuilt::ansi_theme()))); - let syntaxes = dumps::from_uncompressed_data(yazi_prebuilt::syntaxes()); + let syntaxes = dumps::from_uncompressed_data(yazi_prebuilt::syntaxes()); - (theme.unwrap(), syntaxes.unwrap()) - }; - - let (theme, syntaxes) = SYNTECT.get_or_init(f); - (theme, syntaxes) + (theme.unwrap(), syntaxes.unwrap()) } fn load_syntax(&mut self) -> Result<()> { diff --git a/yazi-plugin/src/utils/preview.rs b/yazi-plugin/src/utils/preview.rs index 302588b1..cfc1cf53 100644 --- a/yazi-plugin/src/utils/preview.rs +++ b/yazi-plugin/src/utils/preview.rs @@ -1,6 +1,5 @@ use mlua::{ExternalError, Function, IntoLuaMulti, Lua, Table, Value}; use yazi_binding::{Error, elements::{Area, Renderable, Text}}; -use yazi_config::YAZI; use yazi_fs::FsUrl; use yazi_parser::mgr::{PreviewLock, UpdatePeekedOpt}; use yazi_proxy::MgrProxy; @@ -24,12 +23,7 @@ impl Utils { } }; - lock.data = vec![Renderable::Text(Text { - area, - inner, - wrap: YAZI.preview.wrap.into(), - scroll: Default::default(), - })]; + lock.data = vec![Renderable::Text(Text { area, inner, ..Default::default() })]; MgrProxy::update_peeked(UpdatePeekedOpt { lock }); ().into_lua_multi(&lua) diff --git a/yazi-shim/src/ratatui/grapheme.rs b/yazi-shim/src/ratatui/grapheme.rs deleted file mode 100644 index a2b8da4c..00000000 --- a/yazi-shim/src/ratatui/grapheme.rs +++ /dev/null @@ -1,47 +0,0 @@ -// Copied from https://github.com/ratatui/ratatui/blob/main/ratatui-core/src/text/grapheme.rs -use std::borrow::Cow; - -use ratatui::style::{Style, Styled}; - -const NBSP: &str = "\u{00a0}"; -const ZWSP: &str = "\u{200b}"; - -/// A grapheme associated to a style. -/// Note that, although `StyledGrapheme` is the smallest divisible unit of text, -/// it actually is not a member of the text type hierarchy (`Text` -> `Line` -> -/// `Span`). It is a separate type used mostly for rendering purposes. A `Span` -/// consists of components that can be split into `StyledGrapheme`s, but it does -/// not contain a collection of `StyledGrapheme`s. -#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] -pub struct StyledGrapheme<'a> { - pub symbol: Cow<'a, str>, - pub style: Style, -} - -impl<'a> StyledGrapheme<'a> { - /// Creates a new `StyledGrapheme` with the given symbol and style. - /// - /// `style` accepts any type that is convertible to [`Style`] (e.g. [`Style`], - /// [`Color`], or your own type that implements [`Into