feat: plugin-specific state persistence

This commit is contained in:
sxyazi 2024-01-27 22:57:46 +08:00
parent c325c332de
commit 7bb5fbb4bb
No known key found for this signature in database
13 changed files with 77 additions and 56 deletions

View file

@ -1,7 +1,7 @@
use std::fmt::Display;
use mlua::{ExternalError, ExternalResult, IntoLua, Table, TableExt, Value, Variadic};
use tracing::{error, warn};
use mlua::{ExternalError, ExternalResult, IntoLua, Table, TableExt, Variadic};
use tracing::warn;
use yazi_plugin::{LOADED, LUA};
use yazi_shared::{emit, event::Exec, Layer};
@ -36,31 +36,26 @@ impl App {
};
let args = Variadic::from_iter(opt.data.args.into_iter().filter_map(|v| v.into_lua(&LUA).ok()));
let mut ret: mlua::Result<Value> = Err("uninitialized plugin".into_lua_err());
let result = Lives::scope(&self.cx, |_| {
LUA.globals().set("YAZI_PLUGIN_NAME", LUA.create_string(&opt.name)?)?;
Lives::scope(&self.cx, |_| {
let mut plugin: Option<Table> = None;
if let Some(b) = LOADED.read().get(&opt.name) {
match LUA.load(b).call(args) {
Ok(t) => plugin = Some(t),
Err(e) => ret = Err(e),
}
plugin = LUA.load(b).call(args)?;
}
if let Some(plugin) = plugin {
ret = if let Some(cb) = opt.data.cb { cb(plugin) } else { plugin.call_method("entry", ()) };
}
});
if let Err(e) = ret {
error!("{e}");
return;
}
let Some(plugin) = plugin else {
return Err("plugin not found".into_lua_err());
};
if let Some(cb) = opt.data.cb { cb(plugin) } else { plugin.call_method("entry", ()) }
});
let Some(tx) = opt.data.tx else {
return;
};
if let Ok(v) = ret.and_then(|v| v.try_into().into_lua_err()) {
if let Ok(v) = result.and_then(|v| v.try_into().into_lua_err()) {
tx.send(v).ok();
}
}

View file

@ -13,8 +13,9 @@ impl App {
let collision = COLLISION.swap(false, Ordering::Relaxed);
let frame = term.draw(|f| {
Lives::scope(&self.cx, |_| {
_ = Lives::scope(&self.cx, |_| {
f.render_widget(Root::new(&self.cx), f.size());
Ok(())
});
if let Some((x, y)) = self.cx.cursor() {

View file

@ -23,7 +23,10 @@ impl Lives {
Ok(())
}
pub(crate) fn scope<'a>(cx: &'a Ctx, f: impl FnOnce(&Scope<'a, 'a>)) {
pub(crate) fn scope<'a, T>(
cx: &'a Ctx,
f: impl FnOnce(&Scope<'a, 'a>) -> mlua::Result<T>,
) -> mlua::Result<T> {
let result = LUA.scope(|scope| {
LUA.set_named_registry_value("cx", scope.create_any_userdata_ref(cx)?)?;
@ -37,7 +40,7 @@ impl Lives {
])?,
)?;
f(scope);
let ret = f(scope)?;
LAYOUT.store(Arc::new(yazi_config::Layout {
header: *global.get::<_, Table>("Header")?.get::<_, RectRef>("area")?,
@ -47,12 +50,13 @@ impl Lives {
status: *global.get::<_, Table>("Status")?.get::<_, RectRef>("area")?,
}));
Ok(())
Ok(ret)
});
if let Err(e) = result {
if let Err(ref e) = result {
error!("{e}");
}
result
}
pub(crate) fn partial_scope<'a>(cx: &'a Ctx, f: impl FnOnce(&Scope<'a, 'a>)) {

View file

@ -19,6 +19,26 @@ function Folder:icon(file)
return icon and ui.Span(" " .. icon.text .. " "):style(icon.style) or ui.Span("")
end
function Folder:highlight_ranges(s, ranges)
if ranges == nil or #ranges == 0 then
return { ui.Span(s) }
end
local spans = {}
local last = 0
for _, r in ipairs(ranges) do
if r[1] > last then
spans[#spans + 1] = ui.Span(s:sub(last + 1, r[1]))
end
spans[#spans + 1] = ui.Span(s:sub(r[1] + 1, r[2])):style(THEME.manager.find_keyword)
last = r[2]
end
if last < #s then
spans[#spans + 1] = ui.Span(s:sub(last + 1))
end
return spans
end
function Folder:highlighted_name(file)
-- Complete prefix when searching across directories
local prefix = file:prefix() or ""
@ -28,7 +48,7 @@ function Folder:highlighted_name(file)
-- Range highlighting for filenames
local highlights = file:highlights()
local spans = ui.highlight_ranges(prefix .. file.name, highlights)
local spans = self:highlight_ranges(prefix .. file.name, highlights)
-- Show symlink target
if MANAGER.show_symlink and file.link_to ~= nil then

View file

@ -30,13 +30,17 @@ function Header:tabs()
return ui.Line(spans)
end
function Header:render(area)
function Header:layout(area)
self.area = area
local chunks = ui.Layout()
return ui.Layout()
:direction(ui.Layout.HORIZONTAL)
:constraints({ ui.Constraint.Percentage(50), ui.Constraint.Percentage(50) })
:split(area)
end
function Header:render(area)
local chunks = self:layout(area)
local left = ui.Line { self:cwd() }
local right = ui.Line { self:tabs() }

View file

@ -2,10 +2,10 @@ Manager = {
area = ui.Rect.default,
}
function Manager:render(area)
function Manager:layout(area)
self.area = area
local chunks = ui.Layout()
return ui.Layout()
:direction(ui.Layout.HORIZONTAL)
:constraints({
ui.Constraint.Ratio(MANAGER.ratio.parent, MANAGER.ratio.all),
@ -13,6 +13,10 @@ function Manager:render(area)
ui.Constraint.Ratio(MANAGER.ratio.preview, MANAGER.ratio.all),
})
:split(area)
end
function Manager:render(area)
local chunks = self:layout(area)
return ya.flat {
-- Borders

View file

@ -0,0 +1,16 @@
local cache = {}
state = setmetatable({
clear = function() cache[YAZI_PLUGIN_NAME] = nil end,
}, {
__index = function(_, k)
local bucket = YAZI_PLUGIN_NAME
return cache[bucket] and cache[bucket][k]
end,
__newindex = function(_, k, v)
local bucket = YAZI_PLUGIN_NAME
cache[bucket] = cache[bucket] or {}
cache[bucket][k] = v
end,
})

View file

@ -1,21 +0,0 @@
ui = {}
function ui.highlight_ranges(s, ranges)
if ranges == nil or #ranges == 0 then
return { ui.Span(s) }
end
local spans = {}
local last = 0
for _, r in ipairs(ranges) do
if r[1] > last then
spans[#spans + 1] = ui.Span(s:sub(last + 1, r[1]))
end
spans[#spans + 1] = ui.Span(s:sub(r[1] + 1, r[2])):style(THEME.manager.find_keyword)
last = r[2]
end
if last < #s then
spans[#spans + 1] = ui.Span(s:sub(last + 1))
end
return spans
end

View file

@ -39,7 +39,7 @@ function ya.sync(f)
end
local calls = ya.SYNC_CALLS
return function(...) return plugin_retrieve(ya.PLUGIN_NAME, calls, ...) end
return function(...) return plugin_retrieve(YAZI_PLUGIN_NAME, calls, ...) end
end
function ya.basename(str) return string.gsub(str, "(.*[/\\])(.*)", "%2") end

View file

@ -3,8 +3,7 @@ use mlua::{AnyUserData, Lua, Table};
use crate::cast_to_renderable;
pub fn init(lua: &Lua) -> mlua::Result<()> {
let globals = lua.globals();
let ui: Table = globals.get("ui").or_else(|_| lua.create_table())?;
let ui: Table = lua.create_table()?;
// Register
super::Padding::register(lua)?;

View file

@ -64,9 +64,7 @@ impl UserData for Layout {
ud.borrow_mut::<Self>()?.constraints = value.into_iter().map(|c| c.0).collect();
Ok(ud)
});
methods.add_function("split", |lua, (ud, value): (AnyUserData, RectRef)| {
let me = ud.borrow::<Self>()?;
methods.add_method("split", |lua, me, value: RectRef| {
let mut layout = ratatui::layout::Layout::new(
if me.direction == VERTICAL {
ratatui::layout::Direction::Vertical

View file

@ -9,8 +9,9 @@ pub async fn entry(name: String, args: Vec<ValueSendable>) -> mlua::Result<()> {
tokio::task::spawn_blocking(move || {
let lua = slim_lua()?;
let args = Variadic::from_iter(args.into_iter().filter_map(|v| v.into_lua(&lua).ok()));
lua.globals().set("YAZI_PLUGIN_NAME", lua.create_string(&name)?)?;
let args = Variadic::from_iter(args.into_iter().filter_map(|v| v.into_lua(&lua).ok()));
let plugin: Table = if let Some(b) = LOADED.read().get(&name) {
lua.load(b).call(args)?
} else {

View file

@ -14,7 +14,7 @@ pub fn init() {
// Base
lua.load(include_str!("../preset/inspect/inspect.lua")).exec()?;
lua.load(include_str!("../preset/ui.lua")).exec()?;
lua.load(include_str!("../preset/state.lua")).exec()?;
lua.load(include_str!("../preset/ya.lua")).exec()?;
crate::elements::init(lua)?;