mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Update command.rs
This commit is contained in:
parent
abf5af727f
commit
9babeef6d9
1 changed files with 16 additions and 18 deletions
|
|
@ -14,13 +14,13 @@ const INHERIT: u8 = 2;
|
||||||
|
|
||||||
impl Command {
|
impl Command {
|
||||||
pub fn install(lua: &Lua) -> mlua::Result<()> {
|
pub fn install(lua: &Lua) -> mlua::Result<()> {
|
||||||
let new = lua.create_function(|lua, (table, program): (Table, String)| {
|
let new = lua.create_function(|_, (_, program): (Table, String)| {
|
||||||
let mut inner = tokio::process::Command::new(program);
|
let mut inner = tokio::process::Command::new(program);
|
||||||
inner.kill_on_drop(true)
|
inner.kill_on_drop(true)
|
||||||
.stdin(Stdio::null())
|
.stdin(Stdio::null())
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::null());
|
.stderr(Stdio::null());
|
||||||
Ok(Command { inner })
|
Ok(Self { inner })
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let command = lua.create_table_from([
|
let command = lua.create_table_from([
|
||||||
|
|
@ -30,21 +30,20 @@ impl Command {
|
||||||
])?;
|
])?;
|
||||||
|
|
||||||
command.set_metatable(Some(lua.create_table_from([("__call", new)])?));
|
command.set_metatable(Some(lua.create_table_from([("__call", new)])?));
|
||||||
lua.globals().set("Command", command)
|
lua.globals().raw_set("Command", command)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UserData for Command {
|
impl UserData for Command {
|
||||||
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
|
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
|
#[inline]
|
||||||
fn make_stdio(v: Value) -> mlua::Result<Stdio> {
|
fn make_stdio(v: Value) -> mlua::Result<Stdio> {
|
||||||
match v {
|
match v {
|
||||||
Value::Integer(n) => {
|
Value::Integer(n) => Ok(match n as u8 {
|
||||||
Ok(match n as u8 {
|
PIPED => Stdio::piped(),
|
||||||
PIPED => Stdio::piped(),
|
INHERIT => Stdio::inherit(),
|
||||||
INHERIT => Stdio::inherit(),
|
_ => Stdio::null(),
|
||||||
_ => Stdio::null(),
|
}),
|
||||||
})
|
|
||||||
}
|
|
||||||
Value::UserData(ud) => {
|
Value::UserData(ud) => {
|
||||||
if let Ok(stdin) = ud.take::<ChildStdin>() {
|
if let Ok(stdin) = ud.take::<ChildStdin>() {
|
||||||
Ok(stdin.try_into()?)
|
Ok(stdin.try_into()?)
|
||||||
|
|
@ -57,21 +56,20 @@ impl UserData for Command {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Err(
|
_ => Err(
|
||||||
"must be one of Command.NULL, Command.PIPED, Command.INHERIT, or a ChildStdin, ChildStdout, or ChildStderr".into_lua_err(),
|
"must be one of Command.NULL, Command.PIPED, Command.INHERIT, or a ChildStdin, ChildStdout, or ChildStderr"
|
||||||
)
|
.into_lua_err(),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
methods.add_function_mut("arg", |_, (ud, arg): (AnyUserData, mlua::String)| {
|
methods.add_function_mut("arg", |_, (ud, arg): (AnyUserData, mlua::String)| {
|
||||||
ud.borrow_mut::<Self>()?.inner.arg(arg.to_str()?);
|
ud.borrow_mut::<Self>()?.inner.arg(arg.to_string_lossy());
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
});
|
});
|
||||||
|
|
||||||
methods.add_function_mut("args", |_, (ud, args): (AnyUserData, Vec<mlua::String>)| {
|
methods.add_function_mut("args", |_, (ud, args): (AnyUserData, Vec<mlua::String>)| {
|
||||||
let mut me = ud.borrow_mut::<Self>()?;
|
let args: Vec<_> = args.iter().map(|arg| arg.to_string_lossy()).collect();
|
||||||
for arg in args {
|
ud.borrow_mut::<Self>()?.inner.args(&args);
|
||||||
me.inner.arg(arg.to_str()?);
|
|
||||||
}
|
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -85,7 +83,7 @@ impl UserData for Command {
|
||||||
|_, (ud, key, value): (AnyUserData, mlua::String, mlua::String)| {
|
|_, (ud, key, value): (AnyUserData, mlua::String, mlua::String)| {
|
||||||
ud.borrow_mut::<Self>()?
|
ud.borrow_mut::<Self>()?
|
||||||
.inner
|
.inner
|
||||||
.env(key.to_str()?, value.to_str()?);
|
.env(key.to_string_lossy(), value.to_string_lossy());
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue