feat: wip highlight section of preview

This commit is contained in:
Jed 2026-06-08 23:42:30 +02:00
parent 947e734860
commit a29e5c9296
3 changed files with 65 additions and 12 deletions

View file

@ -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<Value> {
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<Self> {
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<Value> {
let tbl = lua.create_table()?;
tbl.set("line", self.line)?;
tbl.set("length", self.length)?;
Ok(Value::Table(tbl))
}
}

View file

@ -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);

View file

@ -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 anyhow::{Result, anyhow, bail};
use ratatui::{layout::Size, text::{Line, Span, Text}}; use ratatui::{
use syntect::{LoadingError, dumps, easy::HighlightLines, highlighting::{self, Theme, ThemeSet}, parsing::{SyntaxReference, SyntaxSet}}; 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_config::{THEME, YAZI};
use yazi_runner::previewer::PeekError; use yazi_runner::previewer::PeekError;
use yazi_shared::{Id, Ids, replace_to_printable}; use yazi_shared::{Id, Ids, replace_to_printable};
@ -57,7 +69,9 @@ impl Highlighter {
}) })
} }
pub fn abort() { INCR.next(); } pub fn abort() {
INCR.next();
}
fn highlight(mut self) -> Result<Text<'static>, PeekError> { fn highlight(mut self) -> Result<Text<'static>, PeekError> {
self.load_syntax()?; self.load_syntax()?;