diff --git a/yazi-binding/src/elements/highlight.rs b/yazi-binding/src/elements/highlight.rs new file mode 100644 index 00000000..e94dc8da --- /dev/null +++ b/yazi-binding/src/elements/highlight.rs @@ -0,0 +1,39 @@ +use mlua::{ + ExternalError, FromLua, IntoLua, Lua, Result as LuaResult, Table, UserData, UserDataMethods, + Value, +}; + +const EXPECTED: &str = "expected a table containing a line and a length"; + +#[derive(Clone, Debug)] +pub struct HighlightPosition { + pub line: usize, + pub length: usize, +} + +impl HighlightPosition { + pub fn compose(lua: &Lua) -> LuaResult { + let new = lua.create_function(|_, (line, length): (usize, usize)| Ok(Self { line, length }))?; + + let tbl = lua.create_table_from([("new", new)])?; + tbl.into_lua(lua) + } +} + +impl FromLua for HighlightPosition { + fn from_lua(value: Value, _: &Lua) -> mlua::Result { + match value { + Value::Table(tbl) => Ok(Self { line: tbl.raw_get("line")?, length: tbl.raw_get("length")? }), + _ => Err(EXPECTED.into_lua_err()), + } + } +} + +impl IntoLua for HighlightPosition { + fn into_lua(self, lua: &Lua) -> LuaResult { + let tbl = lua.create_table()?; + tbl.set("line", self.line)?; + tbl.set("length", self.length)?; + Ok(Value::Table(tbl)) + } +} diff --git a/yazi-binding/src/elements/mod.rs b/yazi-binding/src/elements/mod.rs index 61dcd02f..6e1066f1 100644 --- a/yazi-binding/src/elements/mod.rs +++ b/yazi-binding/src/elements/mod.rs @@ -1 +1 @@ -yazi_macro::mod_flat!(align area bar border cell clear color constraint edge elements gauge layout line list pad pos rect renderable row span table text wrap); +yazi_macro::mod_flat!(align area bar border cell clear color constraint edge highlight elements gauge layout line list pad pos rect renderable row span table text wrap); diff --git a/yazi-core/src/highlighter.rs b/yazi-core/src/highlighter.rs index 2b3f292b..046d3667 100644 --- a/yazi-core/src/highlighter.rs +++ b/yazi-core/src/highlighter.rs @@ -1,8 +1,20 @@ -use std::{io::{BufRead, BufReader, Cursor, Seek}, path::PathBuf, sync::OnceLock}; +use std::{ + io::{BufRead, BufReader, Cursor, Seek}, + path::PathBuf, + sync::OnceLock, +}; use anyhow::{Result, anyhow, bail}; -use ratatui::{layout::Size, text::{Line, Span, Text}}; -use syntect::{LoadingError, dumps, easy::HighlightLines, highlighting::{self, Theme, ThemeSet}, parsing::{SyntaxReference, SyntaxSet}}; +use ratatui::{ + layout::Size, + text::{Line, Span, Text}, +}; +use syntect::{ + LoadingError, dumps, + easy::HighlightLines, + highlighting::{self, Theme, ThemeSet}, + parsing::{SyntaxReference, SyntaxSet}, +}; use yazi_config::{THEME, YAZI}; use yazi_runner::previewer::PeekError; use yazi_shared::{Id, Ids, replace_to_printable}; @@ -11,17 +23,17 @@ use yazi_shim::ratatui::LineIter; static INCR: Ids = Ids::new(); pub struct Highlighter { - path: PathBuf, + path: PathBuf, reader: BufReader, - skip: usize, - size: Size, + skip: usize, + size: Size, ticket: Id, - theme: &'static Theme, + theme: &'static Theme, syntaxes: &'static SyntaxSet, - inner: Option>, - syntax: Option<&'static SyntaxReference>, + inner: Option>, + syntax: Option<&'static SyntaxReference>, } impl Highlighter { @@ -57,7 +69,9 @@ impl Highlighter { }) } - pub fn abort() { INCR.next(); } + pub fn abort() { + INCR.next(); + } fn highlight(mut self) -> Result, PeekError> { self.load_syntax()?; @@ -220,7 +234,7 @@ impl Highlighter { Span { content: s.into(), - style: ratatui::style::Style { + style: ratatui::style::Style { fg: Self::to_ansi_color(style.foreground), // bg: Self::to_ansi_color(style.background), add_modifier: modifier,