feat: new empty previewer for empty and /proc files (#1482)

This commit is contained in:
三咲雅 · Misaki Masa 2024-08-14 15:03:38 +08:00 committed by GitHub
parent 71d6b0d9f2
commit 62fcf27dfb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 64 additions and 3 deletions

View File

@ -122,6 +122,8 @@ previewers = [
# Font
{ mime = "font/*", run = "font" },
{ mime = "application/vnd.ms-opentype", run = "font" },
# Empty file
{ mime = "inode/x-empty", run = "empty" },
# Fallback
{ name = "*", run = "file" },
]

View File

@ -0,0 +1,49 @@
local M = {}
function M:msg(s)
local p = ui.Paragraph(self.area, {
ui.Line { ui.Span(s):reverse() },
})
ya.preview_widgets(self, { p:wrap(ui.Paragraph.WRAP) })
end
function M:peek()
local path = tostring(self.file.url)
if path:sub(1, 6) ~= "/proc/" then
return self:msg("Empty file")
end
local limit = self.area.h
local i, lines = 0, {}
local ok, err = pcall(function()
for line in io.lines(path) do
i = i + 1
if i > self.skip + limit then
break
elseif i > self.skip then
lines[#lines + 1] = ui.Line(line)
end
end
end)
if not ok then
self:msg(err)
elseif self.skip > 0 and i < self.skip + limit then
ya.manager_emit("peek", { math.max(0, i - limit), only_if = self.file.url, upper_bound = true })
else
ya.preview_widgets(self, { ui.Paragraph(self.area, lines) })
end
end
function M:seek(units)
local h = cx.active.current.hovered
if h and h.url == self.file.url then
local step = math.floor(units * self.area.h / 10)
ya.manager_emit("peek", {
math.max(0, cx.active.preview.skip + step),
only_if = self.file.url,
})
end
end
return M

View File

@ -51,6 +51,10 @@ impl UserData for Span {
ud.borrow_mut::<Self>()?.0.style.add_modifier |= ratatui::style::Modifier::RAPID_BLINK;
Ok(ud)
});
methods.add_function("reverse", |_, ud: AnyUserData| {
ud.borrow_mut::<Self>()?.0.style.add_modifier |= ratatui::style::Modifier::REVERSED;
Ok(ud)
});
methods.add_function("hidden", |_, ud: AnyUserData| {
ud.borrow_mut::<Self>()?.0.style.add_modifier |= ratatui::style::Modifier::HIDDEN;
Ok(ud)

View File

@ -72,6 +72,10 @@ impl UserData for Style {
ud.borrow_mut::<Self>()?.0.add_modifier |= ratatui::style::Modifier::RAPID_BLINK;
Ok(ud)
});
methods.add_function("reverse", |_, ud: AnyUserData| {
ud.borrow_mut::<Self>()?.0.add_modifier |= ratatui::style::Modifier::REVERSED;
Ok(ud)
});
methods.add_function("hidden", |_, ud: AnyUserData| {
ud.borrow_mut::<Self>()?.0.add_modifier |= ratatui::style::Modifier::HIDDEN;
Ok(ud)

View File

@ -47,7 +47,7 @@ pub fn peek(cmd: &Cmd, file: yazi_shared::fs::File, skip: usize) -> Cancellation
if let Err(e) = result {
if !e.to_string().contains("Peek task cancelled") {
error!("{e}");
error!("{e:?}");
}
}
});

View File

@ -1,6 +1,6 @@
use std::{borrow::Cow, collections::HashMap, ops::Deref};
use anyhow::Result;
use anyhow::{Context, Result};
use mlua::{ExternalError, Lua, Table};
use parking_lot::RwLock;
use tokio::fs;
@ -24,6 +24,7 @@ impl Loader {
"archive" => &include_bytes!("../../preset/plugins/archive.lua")[..],
"code" => include_bytes!("../../preset/plugins/code.lua"),
"dds" => include_bytes!("../../preset/plugins/dds.lua"),
"empty" => include_bytes!("../../preset/plugins/empty.lua"),
"extract" => include_bytes!("../../preset/plugins/extract.lua"),
"file" => include_bytes!("../../preset/plugins/file.lua"),
"folder" => include_bytes!("../../preset/plugins/folder.lua"),
@ -42,7 +43,8 @@ impl Loader {
};
let b = if preset.is_empty() {
Cow::Owned(fs::read(BOOT.plugin_dir.join(format!("{name}.yazi/init.lua"))).await?)
let p = BOOT.plugin_dir.join(format!("{name}.yazi/init.lua"));
Cow::Owned(fs::read(&p).await.with_context(|| format!("failed to load plugin from {p:?}"))?)
} else {
Cow::Borrowed(preset)
};