mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Run rustfmt +nightly **/*.rs and rebase
This commit is contained in:
parent
bb887f9230
commit
87d63dc273
1 changed files with 75 additions and 82 deletions
|
|
@ -1,12 +1,13 @@
|
||||||
use std::process::Stdio;
|
use std::{ffi::OsString, process::Stdio};
|
||||||
use std::ffi::OsString;
|
|
||||||
use mlua::{AnyUserData, ExternalError, IntoLuaMulti, Lua, Table, UserData, Value};
|
use mlua::{AnyUserData, ExternalError, IntoLuaMulti, Lua, Table, UserData, Value};
|
||||||
use tokio::process::{ChildStderr, ChildStdin, ChildStdout};
|
use tokio::process::{ChildStderr, ChildStdin, ChildStdout};
|
||||||
|
|
||||||
use super::{Child, output::Output};
|
use super::{Child, output::Output};
|
||||||
use crate::process::Status;
|
use crate::process::Status;
|
||||||
|
|
||||||
pub struct Command {
|
pub struct Command {
|
||||||
inner: tokio::process::Command,
|
inner: tokio::process::Command,
|
||||||
}
|
}
|
||||||
|
|
||||||
const NULL: u8 = 0;
|
const NULL: u8 = 0;
|
||||||
|
|
@ -14,32 +15,26 @@ const PIPED: u8 = 1;
|
||||||
const INHERIT: u8 = 2;
|
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(|_, (_, 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()).stdout(Stdio::null()).stderr(Stdio::null());
|
||||||
.stdin(Stdio::null())
|
Ok(Self { inner })
|
||||||
.stdout(Stdio::null())
|
})?;
|
||||||
.stderr(Stdio::null());
|
|
||||||
Ok(Self { inner })
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let command = lua.create_table_from([
|
let command =
|
||||||
("NULL", NULL),
|
lua.create_table_from([("NULL", NULL), ("PIPED", PIPED), ("INHERIT", INHERIT)])?;
|
||||||
("PIPED", PIPED),
|
|
||||||
("INHERIT", INHERIT),
|
command.set_metatable(Some(lua.create_table_from([("__call", new)])?));
|
||||||
])?;
|
lua.globals().raw_set("Command", command)
|
||||||
|
}
|
||||||
command.set_metatable(Some(lua.create_table_from([("__call", new)])?));
|
|
||||||
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]
|
#[inline]
|
||||||
fn make_stdio(v: Value) -> mlua::Result<Stdio> {
|
fn make_stdio(v: Value) -> mlua::Result<Stdio> {
|
||||||
match v {
|
match v {
|
||||||
Value::Integer(n) => Ok(match n as u8 {
|
Value::Integer(n) => Ok(match n as u8 {
|
||||||
PIPED => Stdio::piped(),
|
PIPED => Stdio::piped(),
|
||||||
INHERIT => Stdio::inherit(),
|
INHERIT => Stdio::inherit(),
|
||||||
|
|
@ -61,71 +56,69 @@ impl UserData for Command {
|
||||||
.into_lua_err(),
|
.into_lua_err(),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
methods.add_function_mut("arg", |_, (ud, arg): (AnyUserData, mlua::String)| {
|
methods.add_function_mut("arg", |_, (ud, arg): (AnyUserData, mlua::String)| {
|
||||||
let arg = OsString::from(arg.to_str()?);
|
let arg = OsString::from(arg.to_str()?);
|
||||||
ud.borrow_mut::<Self>()?.inner.arg(arg);
|
ud.borrow_mut::<Self>()?.inner.arg(arg);
|
||||||
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 args: Vec<OsString> = args
|
let args: Vec<OsString> =
|
||||||
.iter()
|
args.iter().map(|arg| OsString::from(arg.to_str().unwrap())).collect();
|
||||||
.map(|arg| OsString::from(arg.to_str().unwrap()))
|
ud.borrow_mut::<Self>()?.inner.args(&args);
|
||||||
.collect();
|
Ok(ud)
|
||||||
ud.borrow_mut::<Self>()?.inner.args(&args);
|
});
|
||||||
Ok(ud)
|
|
||||||
});
|
|
||||||
|
|
||||||
methods.add_function_mut("cwd", |_, (ud, dir): (AnyUserData, mlua::String)| {
|
methods.add_function_mut("cwd", |_, (ud, dir): (AnyUserData, mlua::String)| {
|
||||||
let dir = OsString::from(dir.to_str()?);
|
let dir = OsString::from(dir.to_str()?);
|
||||||
ud.borrow_mut::<Self>()?.inner.current_dir(dir);
|
ud.borrow_mut::<Self>()?.inner.current_dir(dir);
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
});
|
});
|
||||||
|
|
||||||
methods.add_function_mut(
|
methods.add_function_mut(
|
||||||
"env",
|
"env",
|
||||||
|_, (ud, key, value): (AnyUserData, mlua::String, mlua::String)| {
|
|_, (ud, key, value): (AnyUserData, mlua::String, mlua::String)| {
|
||||||
let key = OsString::from(key.to_str()?);
|
let key = OsString::from(key.to_str()?);
|
||||||
let value = OsString::from(value.to_str()?);
|
let value = OsString::from(value.to_str()?);
|
||||||
ud.borrow_mut::<Self>()?.inner.env(key, value);
|
ud.borrow_mut::<Self>()?.inner.env(key, value);
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
methods.add_function_mut("stdin", |_, (ud, stdio): (AnyUserData, Value)| {
|
methods.add_function_mut("stdin", |_, (ud, stdio): (AnyUserData, Value)| {
|
||||||
ud.borrow_mut::<Self>()?.inner.stdin(make_stdio(stdio)?);
|
ud.borrow_mut::<Self>()?.inner.stdin(make_stdio(stdio)?);
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
});
|
});
|
||||||
|
|
||||||
methods.add_function_mut("stdout", |_, (ud, stdio): (AnyUserData, Value)| {
|
methods.add_function_mut("stdout", |_, (ud, stdio): (AnyUserData, Value)| {
|
||||||
ud.borrow_mut::<Self>()?.inner.stdout(make_stdio(stdio)?);
|
ud.borrow_mut::<Self>()?.inner.stdout(make_stdio(stdio)?);
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
});
|
});
|
||||||
|
|
||||||
methods.add_function_mut("stderr", |_, (ud, stdio): (AnyUserData, Value)| {
|
methods.add_function_mut("stderr", |_, (ud, stdio): (AnyUserData, Value)| {
|
||||||
ud.borrow_mut::<Self>()?.inner.stderr(make_stdio(stdio)?);
|
ud.borrow_mut::<Self>()?.inner.stderr(make_stdio(stdio)?);
|
||||||
Ok(ud)
|
Ok(ud)
|
||||||
});
|
});
|
||||||
|
|
||||||
methods.add_method_mut("spawn", |lua, me, ()| match me.inner.spawn() {
|
methods.add_method_mut("spawn", |lua, me, ()| match me.inner.spawn() {
|
||||||
Ok(child) => (Child::new(child), Value::Nil).into_lua_multi(lua),
|
Ok(child) => (Child::new(child), Value::Nil).into_lua_multi(lua),
|
||||||
Err(e) => (Value::Nil, e.raw_os_error()).into_lua_multi(lua),
|
Err(e) => (Value::Nil, e.raw_os_error()).into_lua_multi(lua),
|
||||||
});
|
});
|
||||||
|
|
||||||
methods.add_async_method_mut("output", |lua, me, ()| async move {
|
methods.add_async_method_mut("output", |lua, me, ()| async move {
|
||||||
match me.inner.output().await {
|
match me.inner.output().await {
|
||||||
Ok(output) => (Output::new(output), Value::Nil).into_lua_multi(lua),
|
Ok(output) => (Output::new(output), Value::Nil).into_lua_multi(lua),
|
||||||
Err(e) => (Value::Nil, e.raw_os_error()).into_lua_multi(lua),
|
Err(e) => (Value::Nil, e.raw_os_error()).into_lua_multi(lua),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
methods.add_async_method_mut("status", |lua, me, ()| async move {
|
methods.add_async_method_mut("status", |lua, me, ()| async move {
|
||||||
match me.inner.status().await {
|
match me.inner.status().await {
|
||||||
Ok(status) => (Status::new(status), Value::Nil).into_lua_multi(lua),
|
Ok(status) => (Status::new(status), Value::Nil).into_lua_multi(lua),
|
||||||
Err(e) => (Value::Nil, e.raw_os_error()).into_lua_multi(lua),
|
Err(e) => (Value::Nil, e.raw_os_error()).into_lua_multi(lua),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue