1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-27 02:25:28 +03:00

Add wezterm.background_child_process

This commit is contained in:
Wez Furlong 2021-10-17 10:37:47 -07:00
parent cf58d5b696
commit 32d18905cc
4 changed files with 60 additions and 1 deletions

View File

@ -254,6 +254,10 @@ pub fn make_lua_context(config_file: &Path) -> anyhow::Result<Lua> {
"run_child_process",
lua.create_async_function(run_child_process)?,
)?;
wezterm_mod.set(
"background_child_process",
lua.create_async_function(background_child_process)?,
)?;
wezterm_mod.set("on", lua.create_function(register_event)?)?;
wezterm_mod.set("emit", lua.create_async_function(emit_event)?)?;
wezterm_mod.set("sleep_ms", lua.create_async_function(sleep_ms)?)?;
@ -808,6 +812,26 @@ async fn run_child_process<'lua>(
))
}
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(())
}
fn permute_any_mods<'lua>(
lua: &'lua Lua,
item: mlua::Table,

View File

@ -23,6 +23,7 @@ As features stabilize some brief notes about them will accumulate here.
* `wezterm connect` now also supports the `--class` parameter to override the window class
* [window_padding](config/lua/config/window_padding.md) now accepts values such as `"1cell"` or `"30%"` to compute values based on font or window metrics.
* BSDish systems now support [toast notifications](https://github.com/wez/wezterm/issues/489)
* [wezterm.background_child_process](config/lua/wezterm/background_child_process.md) function to spawn a process without waiting.
#### Changed

View File

@ -0,0 +1,34 @@
# `wezterm.background_child_process(args)`
*Since: nightly builds only*
This function accepts an argument list; it will attempt to spawn that command
in the background.
May generate an error if the command is not able to be spawned (eg: perhaps
the executable doesn't exist), but not all operating systems/environments
report all types of spawn failures immediately upon spawn.
This function doesn't return any value.
This example shows how you might set up a custom key assignment that opens
the terminal background image in a separate image viewer process:
```lua
local wezterm = require 'wezterm'
return {
window_background_image = "/home/wez/Downloads/sunset-american-fork-canyon.jpg",
keys = {
{mods="CTRL|SHIFT", key="m",
action=wezterm.action_callback(function(win, pane)
wezterm.background_child_process(
{"xdg-open", win:effective_config().window_background_image})
end)
}
}
}
```
See also [run_child_process](run_child_process.md)

View File

@ -12,4 +12,4 @@ local wezterm = require 'wezterm';
local success, stdout, stderr = wezterm.run_child_process({"ls", "-l"})
```
See also [background_child_process](background_child_process.md)