mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-22 07:11:03 +00:00
feat: add parse method to Line element
This commit is contained in:
parent
b7d9a0ad6e
commit
497aa26f75
5 changed files with 27 additions and 3 deletions
|
|
@ -3,7 +3,9 @@ package.path = BOOT.plugin_dir .. "/?.yazi/init.lua;" .. package.path
|
|||
local _require = require
|
||||
require = function(name)
|
||||
YAZI_PLUGIN_NAME, YAZI_SYNC_CALLS = name, 0
|
||||
return _require(name)
|
||||
local mod = _require(name)
|
||||
mod._name = name
|
||||
return mod
|
||||
end
|
||||
|
||||
YAZI_SYNC_BLOCKS = {}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
use mlua::{AnyUserData, ExternalError, FromLua, IntoLua, Lua, Table, UserData, UserDataMethods, Value};
|
||||
use std::mem;
|
||||
|
||||
use ansi_to_tui::IntoText;
|
||||
use mlua::{AnyUserData, ExternalError, ExternalResult, FromLua, IntoLua, Lua, Table, UserData, UserDataMethods, Value};
|
||||
|
||||
use super::{Span, Style};
|
||||
|
||||
|
|
@ -39,7 +42,21 @@ impl Line {
|
|||
Err("expected a table of Spans or Lines".into_lua_err())
|
||||
})?;
|
||||
|
||||
let parse = lua.create_function(|_, code: mlua::String| {
|
||||
let Some(line) = code.as_bytes().split_inclusive(|&b| b == b'\n').next() else {
|
||||
return Ok(Line(Default::default()));
|
||||
};
|
||||
|
||||
let mut lines = line.into_text().into_lua_err()?.lines;
|
||||
if lines.is_empty() {
|
||||
return Ok(Line(Default::default()));
|
||||
}
|
||||
|
||||
Ok(Line(mem::take(&mut lines[0])))
|
||||
})?;
|
||||
|
||||
let line = lua.create_table_from([
|
||||
("parse", parse.into_lua(lua)?),
|
||||
// Alignment
|
||||
("LEFT", LEFT.into_lua(lua)?),
|
||||
("CENTER", CENTER.into_lua(lua)?),
|
||||
|
|
|
|||
|
|
@ -60,6 +60,7 @@ impl Loader {
|
|||
None => Err(format!("plugin `{name}` not found").into_lua_err())?,
|
||||
};
|
||||
|
||||
t.raw_set("_name", LUA.create_string(name)?)?;
|
||||
loaded.raw_set(name, t.clone())?;
|
||||
Ok(t)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ impl TryFrom<Cmd> for Opt {
|
|||
|
||||
fn try_from(mut c: Cmd) -> Result<Self, Self::Error> {
|
||||
let Some(name) = c.take_first().filter(|s| !s.is_empty()) else {
|
||||
bail!("invalid plugin name");
|
||||
bail!("plugin name cannot be empty");
|
||||
};
|
||||
|
||||
let mut data: OptData = c.take_data().unwrap_or_default();
|
||||
|
|
|
|||
|
|
@ -49,6 +49,10 @@ impl UserData for Command {
|
|||
}
|
||||
Ok(ud)
|
||||
});
|
||||
methods.add_function("cwd", |_, (ud, dir): (AnyUserData, mlua::String)| {
|
||||
ud.borrow_mut::<Self>()?.inner.current_dir(dir.to_str()?);
|
||||
Ok(ud)
|
||||
});
|
||||
methods.add_function(
|
||||
"env",
|
||||
|_, (ud, key, value): (AnyUserData, mlua::String, mlua::String)| {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue