Main thing to note here is that the open crate has deprecated open::that_in_background, but made open::that non-blocking. I think this is OK, but I'm a little cagey about what will happen with this on Windows. We may need to spawn our own thread for this if things go awry.
74 lines
1.9 KiB
Rust
74 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 {
|
|
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(())
|
|
}
|