1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 06:54:45 +03:00

gui-startup event now accepts SpawnCommand

This allows the hook to choose how to handle eg: `wezterm start -- top`.
Previously, if you had implemented this event you would essentially lose
the ability to specify a command that you wanted to launch.

refs: https://github.com/wez/wezterm/issues/284
This commit is contained in:
Wez Furlong 2022-07-19 19:45:27 -07:00
parent 77b6870ff3
commit badfae7d23
4 changed files with 67 additions and 8 deletions

View File

@ -1,6 +1,7 @@
use crate::keys::KeyNoAction;
use luahelper::impl_lua_conversion_dynamic;
use ordered_float::NotNan;
use portable_pty::CommandBuilder;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::TryFrom;
@ -218,6 +219,34 @@ impl std::fmt::Display for SpawnCommand {
}
}
impl SpawnCommand {
pub fn from_command_builder(cmd: &CommandBuilder) -> anyhow::Result<Self> {
let mut args = vec![];
let mut set_environment_variables = HashMap::new();
for arg in cmd.get_argv() {
args.push(
arg.to_str()
.ok_or_else(|| anyhow::anyhow!("command argument is not utf8"))?
.to_string(),
);
}
for (k, v) in cmd.iter_full_env_as_str() {
set_environment_variables.insert(k.to_string(), v.to_string());
}
let cwd = match cmd.get_cwd() {
Some(cwd) => Some(PathBuf::from(cwd)),
None => None,
};
Ok(Self {
label: None,
domain: SpawnTabDomain::DefaultDomain,
args: if args.is_empty() { None } else { Some(args) },
set_environment_variables,
cwd,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromDynamic, ToDynamic)]
pub enum PaneDirection {
Up,

View File

@ -67,6 +67,7 @@ As features stabilize some brief notes about them will accumulate here.
* Overlays such as debug and launcher menu now handle resize better
* Shift-F1 through F4 generated different encoding than xterm [#2263](https://github.com/wez/wezterm/issues/2263)
* X11/Wayland: apps that extract the `Exec` field from wezterm.desktop (such as thunar, Dolphin and others) can now simply concatenate the command line they want to invoke, and it will spawn in the their current working directory. Thanks to [@Anomalocaridid](https://github.com/Anomalocaridid)! [#2271](https://github.com/wez/wezterm/pull/2271) [#2103](https://github.com/wez/wezterm/issues/2103)
* [gui-startup](config/lua/gui-events/gui-startup.md) now passes a [SpawnCommand](config/lua/SpawnCommand.md) parameter representing the `wezterm start` command arguments.
#### Updated
* Bundled harfbuzz to 4.4.1

View File

@ -15,14 +15,25 @@ program will be spawned.
This event is useful for starting a set of programs in a standard
configuration to save you the effort of doing it manually each time.
*Since: nightly builds only*
The event receives an optional [SpawnCommand](../SpawnCommand.md) argument that
corresponds to any arguments that may have been passed via `wezterm start`.
In earlier releases if you implemented this event, you would essentially
prevent `wezterm start -- something` from spawning `something`.
The intent is for you to use the information in the command object to
spawn a new window, but you can choose to use or ignore it as suits
your purpose.
This basic example splits an initial window into thirds:
```lua
local wezterm = require 'wezterm'
local mux = wezterm.mux
wezterm.on('gui-startup', function()
local tab, pane, window = mux.spawn_window {}
wezterm.on('gui-startup', function(cmd)
local tab, pane, window = mux.spawn_window(cmd or {})
-- Create a split occupying the right 1/3 of the screen
pane:split { size = 0.3 }
-- Create another split in the right of the remaining 2/3
@ -40,8 +51,8 @@ This example creates a default window but makes it maximize on startup:
local wezterm = require 'wezterm'
local mux = wezterm.mux
wezterm.on('gui-startup', function()
local tab, pane, window = mux.spawn_window {}
wezterm.on('gui-startup', function(cmd)
local tab, pane, window = mux.spawn_window(cmd or {})
window:gui_window():maximize()
end)
@ -54,13 +65,21 @@ Here's a more elaborate example that configures two workspaces:
local wezterm = require 'wezterm'
local mux = wezterm.mux
wezterm.on('gui-startup', function()
wezterm.on('gui-startup', function(cmd)
-- allow `wezterm start -- something` to affect what we spawn
-- in our initial window
local args = {}
if cmd then
args = cmd.args
end
-- Set a workspace for coding on a current project
-- Top pane is for the editor, bottom pane is for the build tool
local project_dir = wezterm.home_dir .. '/wezterm'
local tab, build_pane, window = mux.spawn_window {
workspace = 'coding',
cwd = project_dir,
args = args,
}
local editor_pane = build_pane:split {
direction = 'Top',

View File

@ -4,6 +4,7 @@
use ::window::*;
use anyhow::{anyhow, Context};
use clap::{Parser, ValueHint};
use config::keyassignment::SpawnCommand;
use config::{ConfigHandle, SshDomain, SshMultiplexing};
use mux::activity::Activity;
use mux::domain::{Domain, LocalDomain};
@ -414,16 +415,25 @@ async fn async_run_terminal_gui(
connect_to_auto_connect_domains().await?;
}
async fn trigger_gui_startup(lua: Option<Rc<mlua::Lua>>) -> anyhow::Result<()> {
async fn trigger_gui_startup(
lua: Option<Rc<mlua::Lua>>,
spawn: Option<SpawnCommand>,
) -> anyhow::Result<()> {
if let Some(lua) = lua {
let args = lua.pack_multi(())?;
let args = lua.pack_multi(spawn)?;
config::lua::emit_event(&lua, ("gui-startup".to_string(), args)).await?;
}
Ok(())
}
let spawn_command = match &cmd {
Some(cmd) => Some(SpawnCommand::from_command_builder(cmd)?),
None => None,
};
if let Err(err) =
config::with_lua_config_on_main_thread(move |lua| trigger_gui_startup(lua)).await
config::with_lua_config_on_main_thread(move |lua| trigger_gui_startup(lua, spawn_command))
.await
{
let message = format!("while processing gui-startup event: {:#}", err);
log::error!("{}", message);