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 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<std::fs::File>,
skip: usize,
size: Size,
skip: usize,
size: Size,
ticket: Id,
theme: &'static Theme,
theme: &'static Theme,
syntaxes: &'static SyntaxSet,
inner: Option<HighlightLines<'static>>,
syntax: Option<&'static SyntaxReference>,
inner: Option<HighlightLines<'static>>,
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<Text<'static>, 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,