mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: new empty previewer for empty and /proc files (#1482)
This commit is contained in:
parent
71d6b0d9f2
commit
62fcf27dfb
6 changed files with 64 additions and 3 deletions
|
|
@ -122,6 +122,8 @@ previewers = [
|
||||||
# Font
|
# Font
|
||||||
{ mime = "font/*", run = "font" },
|
{ mime = "font/*", run = "font" },
|
||||||
{ mime = "application/vnd.ms-opentype", run = "font" },
|
{ mime = "application/vnd.ms-opentype", run = "font" },
|
||||||
|
# Empty file
|
||||||
|
{ mime = "inode/x-empty", run = "empty" },
|
||||||
# Fallback
|
# Fallback
|
||||||
{ name = "*", run = "file" },
|
{ name = "*", run = "file" },
|
||||||
]
|
]
|
||||||
|
|
|
||||||
49
yazi-plugin/preset/plugins/empty.lua
Normal file
49
yazi-plugin/preset/plugins/empty.lua
Normal 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
|
||||||
|
|
@ -51,6 +51,10 @@ impl UserData for Span {
|
||||||
ud.borrow_mut::<Self>()?.0.style.add_modifier |= ratatui::style::Modifier::RAPID_BLINK;
|
ud.borrow_mut::<Self>()?.0.style.add_modifier |= ratatui::style::Modifier::RAPID_BLINK;
|
||||||
Ok(ud)
|
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| {
|
methods.add_function("hidden", |_, ud: AnyUserData| {
|
||||||
ud.borrow_mut::<Self>()?.0.style.add_modifier |= ratatui::style::Modifier::HIDDEN;
|
ud.borrow_mut::<Self>()?.0.style.add_modifier |= ratatui::style::Modifier::HIDDEN;
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,10 @@ impl UserData for Style {
|
||||||
ud.borrow_mut::<Self>()?.0.add_modifier |= ratatui::style::Modifier::RAPID_BLINK;
|
ud.borrow_mut::<Self>()?.0.add_modifier |= ratatui::style::Modifier::RAPID_BLINK;
|
||||||
Ok(ud)
|
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| {
|
methods.add_function("hidden", |_, ud: AnyUserData| {
|
||||||
ud.borrow_mut::<Self>()?.0.add_modifier |= ratatui::style::Modifier::HIDDEN;
|
ud.borrow_mut::<Self>()?.0.add_modifier |= ratatui::style::Modifier::HIDDEN;
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ pub fn peek(cmd: &Cmd, file: yazi_shared::fs::File, skip: usize) -> Cancellation
|
||||||
|
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
if !e.to_string().contains("Peek task cancelled") {
|
if !e.to_string().contains("Peek task cancelled") {
|
||||||
error!("{e}");
|
error!("{e:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{borrow::Cow, collections::HashMap, ops::Deref};
|
use std::{borrow::Cow, collections::HashMap, ops::Deref};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::{Context, Result};
|
||||||
use mlua::{ExternalError, Lua, Table};
|
use mlua::{ExternalError, Lua, Table};
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
|
@ -24,6 +24,7 @@ impl Loader {
|
||||||
"archive" => &include_bytes!("../../preset/plugins/archive.lua")[..],
|
"archive" => &include_bytes!("../../preset/plugins/archive.lua")[..],
|
||||||
"code" => include_bytes!("../../preset/plugins/code.lua"),
|
"code" => include_bytes!("../../preset/plugins/code.lua"),
|
||||||
"dds" => include_bytes!("../../preset/plugins/dds.lua"),
|
"dds" => include_bytes!("../../preset/plugins/dds.lua"),
|
||||||
|
"empty" => include_bytes!("../../preset/plugins/empty.lua"),
|
||||||
"extract" => include_bytes!("../../preset/plugins/extract.lua"),
|
"extract" => include_bytes!("../../preset/plugins/extract.lua"),
|
||||||
"file" => include_bytes!("../../preset/plugins/file.lua"),
|
"file" => include_bytes!("../../preset/plugins/file.lua"),
|
||||||
"folder" => include_bytes!("../../preset/plugins/folder.lua"),
|
"folder" => include_bytes!("../../preset/plugins/folder.lua"),
|
||||||
|
|
@ -42,7 +43,8 @@ impl Loader {
|
||||||
};
|
};
|
||||||
|
|
||||||
let b = if preset.is_empty() {
|
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 {
|
} else {
|
||||||
Cow::Borrowed(preset)
|
Cow::Borrowed(preset)
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue