Skip to content

Commit

Permalink
add gui-startup and mux-startup events
Browse files Browse the repository at this point in the history
Using the newly exposed-to-lua mux apis, you may now run some lua code
at GUI startup and/or mux startup, just prior to any default windows
being created.

If you happen to spawn any panes as a result of this, wezterm will
skip creating the default program.

```lua
local wezterm = require 'wezterm'
local mux = wezterm.mux

-- This produces a window split horizontally into three equal parts
wezterm.on("gui-startup", function()
  wezterm.log_info("doing gui startup")
  local tab, pane, window = mux.spawn_window{}
  mux.split_pane(pane, {size=0.3})
  mux.split_pane(pane, {size=0.5})
end)

wezterm.on("mux-startup", function()
  wezterm.log_info("doing mux startup")
  local tab, pane, window = mux.spawn_window{}
  mux.split_pane(pane, {size=0.5, direction="Top"})
end)

return {
  unix_domains = {
    {name="unix"}
  },
}
```

refs: #674
refs: #1949
  • Loading branch information
wez committed Jun 17, 2022
1 parent 824dc66 commit 699bbce
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 36 additions & 2 deletions wezterm-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ async fn async_run_with_domain_as_default(
mux.add_domain(&domain);
mux.set_default_domain(&domain);

spawn_tab_in_default_domain_if_mux_is_empty(cmd).await
let is_connecting = true;
spawn_tab_in_default_domain_if_mux_is_empty(cmd, is_connecting).await
}

async fn async_run_mux_client(opts: ConnectCommand) -> anyhow::Result<()> {
Expand Down Expand Up @@ -273,10 +274,23 @@ fn run_mux_client(opts: ConnectCommand) -> anyhow::Result<()> {

async fn spawn_tab_in_default_domain_if_mux_is_empty(
cmd: Option<CommandBuilder>,
is_connecting: bool,
) -> anyhow::Result<()> {
let mux = Mux::get().unwrap();

let domain = mux.default_domain();

if !is_connecting {
let have_panes_in_domain = mux
.iter_panes()
.iter()
.any(|p| p.domain_id() == domain.domain_id());

if have_panes_in_domain {
return Ok(());
}
}

let window_id = mux.new_empty_window(None);

domain.attach(Some(*window_id)).await?;
Expand Down Expand Up @@ -381,7 +395,27 @@ async fn async_run_terminal_gui(
if !opts.no_auto_connect {
connect_to_auto_connect_domains().await?;
}
spawn_tab_in_default_domain_if_mux_is_empty(cmd).await

async fn trigger_gui_startup(lua: Option<Rc<mlua::Lua>>) -> anyhow::Result<()> {
if let Some(lua) = lua {
let args = lua.pack_multi(())?;
config::lua::emit_event(&lua, ("gui-startup".to_string(), args))
.await
.map_err(|e| {
log::error!("while processing gui-startup event: {:#}", e);
e
})?;
}
Ok(())
}

promise::spawn::spawn(config::with_lua_config_on_main_thread(move |lua| {
trigger_gui_startup(lua)
}))
.await?;

let is_connecting = false;
spawn_tab_in_default_domain_if_mux_is_empty(cmd, is_connecting).await
}

#[derive(Debug)]
Expand Down
1 change: 1 addition & 0 deletions wezterm-mux-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ env-bootstrap = { path = "../env-bootstrap" }
libc = "0.2"
log = "0.4"
mux = { path = "../mux" }
mlua = "0.8.0-beta.4"
openssl = "0.10"
portable-pty = { path = "../pty", features = ["serde_support"]}
promise = { path = "../promise" }
Expand Down
39 changes: 32 additions & 7 deletions wezterm-mux-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,43 @@ fn run() -> anyhow::Result<()> {
}
}

async fn trigger_mux_startup(lua: Option<Rc<mlua::Lua>>) -> anyhow::Result<()> {
if let Some(lua) = lua {
let args = lua.pack_multi(())?;
config::lua::emit_event(&lua, ("mux-startup".to_string(), args))
.await
.map_err(|e| {
log::error!("while processing mux-startup event: {:#}", e);
e
})?;
}
Ok(())
}

async fn async_run(cmd: Option<CommandBuilder>) -> anyhow::Result<()> {
let mux = Mux::get().unwrap();

let domain = mux.default_domain();
let window_id = mux.new_empty_window(None);
domain.attach(Some(*window_id)).await?;

let config = config::configuration();
let _tab = mux
.default_domain()
.spawn(config.initial_size(0), cmd, None, *window_id)
.await?;
{
config::with_lua_config_on_main_thread(move |lua| trigger_mux_startup(lua)).await?;
}

let have_panes_in_domain = mux
.iter_panes()
.iter()
.any(|p| p.domain_id() == domain.domain_id());

if !have_panes_in_domain {
let window_id = mux.new_empty_window(None);
domain.attach(Some(*window_id)).await?;

let config = config::configuration();
let _tab = mux
.default_domain()
.spawn(config.initial_size(0), cmd, None, *window_id)
.await?;
}
Ok(())
}

Expand Down

0 comments on commit 699bbce

Please sign in to comment.