feat: support passing arguments to plugin

This commit is contained in:
sxyazi 2024-01-27 14:07:35 +08:00
parent f31f78b063
commit c344dcd1a1
No known key found for this signature in database
4 changed files with 14 additions and 4 deletions

1
Cargo.lock generated
View file

@ -2730,6 +2730,7 @@ dependencies = [
"ratatui",
"serde",
"serde_json",
"shell-words",
"syntect",
"tokio",
"tokio-util",

View file

@ -37,14 +37,13 @@ impl App {
Lives::scope(&self.cx, |_| {
let mut plugin: Option<Table> = None;
if let Some(b) = LOADED.read().get(&opt.name) {
match LUA.load(b).call(()) {
match LUA.load(b).call(args) {
Ok(t) => plugin = Some(t),
Err(e) => ret = Err(e),
}
}
if let Some(plugin) = plugin {
ret =
if let Some(cb) = opt.data.cb { cb(plugin) } else { plugin.call_method("entry", args) };
ret = if let Some(cb) = opt.data.cb { cb(plugin) } else { plugin.call_method("entry", ()) };
}
});

View file

@ -24,6 +24,7 @@ parking_lot = "^0"
ratatui = "^0"
serde = "^1"
serde_json = "^1"
shell-words = "^1"
syntect = { version = "^5", default-features = false, features = [ "parsing", "plist-load", "regex-onig" ] }
tokio = { version = "^1", features = [ "parking_lot", "rt-multi-thread" ] }
tokio-util = "^0"

View file

@ -26,6 +26,15 @@ impl TryFrom<Exec> for Opt {
bail!("invalid plugin name");
};
Ok(Self { name, sync: e.named.contains_key("sync"), data: e.take_data().unwrap_or_default() })
let mut data: OptData = e.take_data().unwrap_or_default();
if let Some(args) = e.named.get("args") {
data.args = shell_words::split(args)?
.into_iter()
.map(|s| ValueSendable::String(s.into_bytes()))
.collect();
}
Ok(Self { name, sync: e.named.contains_key("sync"), data })
}
}