From 2aebf56c3eb49185baa903a3d8710fc5e1b45ec7 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 18 Jun 2022 07:14:07 -0700 Subject: [PATCH] lua api: refine gui-startup example, add some more mux functions The example now shows how to set up multiple workspaces refs: #674 --- docs/config/lua/gui-events/gui-startup.md | 44 ++++++++++++++++++- ...e_workspace.md => get_active_workspace.md} | 2 +- .../lua/wezterm.mux/get_workspace_names.md | 6 +++ .../lua/wezterm.mux/set_active_workspace.md | 9 ++++ lua-api-crates/mux/src/lib.rs | 23 +++++++++- 5 files changed, 81 insertions(+), 3 deletions(-) rename docs/config/lua/wezterm.mux/{active_workspace.md => get_active_workspace.md} (65%) create mode 100644 docs/config/lua/wezterm.mux/get_workspace_names.md create mode 100644 docs/config/lua/wezterm.mux/set_active_workspace.md diff --git a/docs/config/lua/gui-events/gui-startup.md b/docs/config/lua/gui-events/gui-startup.md index 1035cf785..82b61fe86 100644 --- a/docs/config/lua/gui-events/gui-startup.md +++ b/docs/config/lua/gui-events/gui-startup.md @@ -13,7 +13,9 @@ precedence over the default program configuration and no additional default 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: +configuration to save you the effort of doing it manually each time. + +This basic example splits an initial window into thirds: ```lua local wezterm = require 'wezterm' @@ -21,12 +23,52 @@ local mux = wezterm.mux wezterm.on("gui-startup", function() local tab, pane, window = mux.spawn_window{} + -- 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 + -- of the space; the resultant split is in the middle + -- 1/3 of the display and has the focus. pane:split{size=0.5} end) return {} ``` +Here's a more elaborate example that configures two workspaces: + +```lua +local wezterm = require 'wezterm' +local mux = wezterm.mux + +wezterm.on("gui-startup", function() + -- 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, + } + local editor_pane = build_pane:split{ + direction="Top", + size=0.6, + cwd=project_dir + } + -- may as well kick off a build in that pane + build_pane:send_text("cargo build\n") + + -- A workspace for interacting with a local machine that + -- runs some docker containners for home automation + local tab, pane, window = mux.spawn_window{ + workspace="automation", + args={"ssh", "vault"}, + } + + -- We want to startup in the coding workspace + mux.set_active_workspace("coding") +end) + +return {} +``` + See also: * [wezterm.mux](../wezterm.mux/index.md) diff --git a/docs/config/lua/wezterm.mux/active_workspace.md b/docs/config/lua/wezterm.mux/get_active_workspace.md similarity index 65% rename from docs/config/lua/wezterm.mux/active_workspace.md rename to docs/config/lua/wezterm.mux/get_active_workspace.md index ee2e217f1..af36c48c1 100644 --- a/docs/config/lua/wezterm.mux/active_workspace.md +++ b/docs/config/lua/wezterm.mux/get_active_workspace.md @@ -1,4 +1,4 @@ -# `wezterm.mux.active_workspace()` +# `wezterm.mux.get_active_workspace()` *Since: nightly builds only* diff --git a/docs/config/lua/wezterm.mux/get_workspace_names.md b/docs/config/lua/wezterm.mux/get_workspace_names.md new file mode 100644 index 000000000..772584786 --- /dev/null +++ b/docs/config/lua/wezterm.mux/get_workspace_names.md @@ -0,0 +1,6 @@ +# `wezterm.mux.get_workspace_names()` + +*Since: nightly builds only* + +Returns a table containing the names of the workspaces known to the mux. + diff --git a/docs/config/lua/wezterm.mux/set_active_workspace.md b/docs/config/lua/wezterm.mux/set_active_workspace.md new file mode 100644 index 000000000..70a73e4ab --- /dev/null +++ b/docs/config/lua/wezterm.mux/set_active_workspace.md @@ -0,0 +1,9 @@ +# `wezterm.mux.set_active_workspace(WORKSPACE)` + +*Since: nightly builds only* + +Sets the active workspace name. + +If the requested name doesn't correspond to an existing workspace, +then an error is raised. + diff --git a/lua-api-crates/mux/src/lib.rs b/lua-api-crates/mux/src/lib.rs index edf5cba3e..3ef5adf32 100644 --- a/lua-api-crates/mux/src/lib.rs +++ b/lua-api-crates/mux/src/lib.rs @@ -24,13 +24,34 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> { let mux_mod = lua.create_table()?; mux_mod.set( - "active_workspace", + "get_active_workspace", lua.create_function(|_, _: ()| { let mux = get_mux()?; Ok(mux.active_workspace()) })?, )?; + mux_mod.set( + "get_workspace_names", + lua.create_function(|_, _: ()| { + let mux = get_mux()?; + Ok(mux.iter_workspaces()) + })?, + )?; + + mux_mod.set( + "set_active_workspace", + lua.create_function(|_, workspace: String| { + let mux = get_mux()?; + let workspaces = mux.iter_workspaces(); + if workspaces.contains(&workspace) { + Ok(mux.set_active_workspace(&workspace)) + } else { + Err(mlua::Error::external(format!("{:?} is not an existing workspace", workspace))) + } + })?, + )?; + mux_mod.set( "get_window", lua.create_function(|_, window_id: WindowId| {