Files
LLeMbas-TTY/lua-api-crates/spawn-funcs/src/lib.rs
T
Wez Furlong 870ffdf59a ensure that we spawn open calls on a background thread
Otherwise we can block the gui waiting for eg: a freshly opened firefox to
terminate.

See also comment worrying about this in
75066cb522.
That fear was realized but now resolved!

refs: https://github.com/wez/wezterm/issues/2245
2022-07-11 08:03:37 -07:00

76 lines
1.9 KiB
Rust

use bstr::BString;
use config::lua::get_or_create_module;
use config::lua::mlua::{self, Lua};
pub fn register(lua: &Lua) -> anyhow::Result<()> {
let wezterm_mod = get_or_create_module(lua, "wezterm")?;
wezterm_mod.set("open_with", lua.create_function(open_with)?)?;
wezterm_mod.set(
"run_child_process",
lua.create_async_function(run_child_process)?,
)?;
wezterm_mod.set(
"background_child_process",
lua.create_async_function(background_child_process)?,
)?;
Ok(())
}
fn open_with<'lua>(_: &'lua Lua, (url, app): (String, Option<String>)) -> mlua::Result<()> {
if let Some(app) = app {
open::with_in_background(url, app);
} else {
std::thread::spawn(move || {
if let Err(err) = open::that(&url) {
log::error!("Error opening {}: {:#}", url, err);
}
});
}
Ok(())
}
async fn run_child_process<'lua>(
_: &'lua Lua,
args: Vec<String>,
) -> mlua::Result<(bool, BString, BString)> {
let mut cmd = smol::process::Command::new(&args[0]);
if args.len() > 1 {
cmd.args(&args[1..]);
}
#[cfg(windows)]
{
use smol::process::windows::CommandExt;
cmd.creation_flags(winapi::um::winbase::CREATE_NO_WINDOW);
}
let output = cmd.output().await.map_err(|e| mlua::Error::external(e))?;
Ok((
output.status.success(),
output.stdout.into(),
output.stderr.into(),
))
}
async fn background_child_process<'lua>(_: &'lua Lua, args: Vec<String>) -> mlua::Result<()> {
let mut cmd = smol::process::Command::new(&args[0]);
if args.len() > 1 {
cmd.args(&args[1..]);
}
#[cfg(windows)]
{
use smol::process::windows::CommandExt;
cmd.creation_flags(winapi::um::winbase::CREATE_NO_WINDOW);
}
cmd.stdin(smol::process::Stdio::null())
.spawn()
.map_err(|e| mlua::Error::external(e))?;
Ok(())
}