diff --git a/config/src/lua.rs b/config/src/lua.rs index 16320cac1..8a520f15e 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -48,6 +48,26 @@ pub fn get_or_create_module<'lua>(lua: &'lua Lua, name: &str) -> anyhow::Result< } } +pub fn get_or_create_sub_module<'lua>( + lua: &'lua Lua, + name: &str, +) -> anyhow::Result> { + let wezterm_mod = get_or_create_module(lua, "wezterm")?; + let sub = wezterm_mod.get(name)?; + match sub { + Value::Nil => { + let sub = lua.create_table()?; + wezterm_mod.set(name, sub.clone())?; + Ok(sub) + } + Value::Table(sub) => Ok(sub), + wat => anyhow::bail!( + "cannot register module wezterm.{name} as it is already set to a value of type {}", + wat.type_name() + ), + } +} + /// Set up a lua context for executing some code. /// The path to the directory containing the configuration is /// passed in and is used to pre-set some global values in diff --git a/lua-api-crates/mux/src/lib.rs b/lua-api-crates/mux/src/lib.rs index 0b5d9a930..2d0dc7622 100644 --- a/lua-api-crates/mux/src/lib.rs +++ b/lua-api-crates/mux/src/lib.rs @@ -1,5 +1,5 @@ use config::keyassignment::SpawnTabDomain; -use config::lua::get_or_create_module; +use config::lua::get_or_create_sub_module; use config::lua::mlua::{self, Lua, UserData, UserDataMethods, Value as LuaValue}; use luahelper::impl_lua_conversion_dynamic; use mux::domain::SplitSource; @@ -20,8 +20,7 @@ fn get_mux() -> mlua::Result> { } pub fn register(lua: &Lua) -> anyhow::Result<()> { - let wezterm_mod = get_or_create_module(lua, "wezterm")?; - let mux_mod = lua.create_table()?; + let mux_mod = get_or_create_sub_module(lua, "mux")?; mux_mod.set( "get_active_workspace", @@ -102,7 +101,6 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> { })?, )?; - wezterm_mod.set("mux", mux_mod)?; Ok(()) } diff --git a/lua-api-crates/window-funcs/src/lib.rs b/lua-api-crates/window-funcs/src/lib.rs index 31fce5b04..9e739bde6 100644 --- a/lua-api-crates/window-funcs/src/lib.rs +++ b/lua-api-crates/window-funcs/src/lib.rs @@ -1,4 +1,4 @@ -use config::lua::get_or_create_module; +use config::lua::get_or_create_sub_module; use config::lua::mlua::{self, Lua}; use luahelper::impl_lua_conversion_dynamic; use std::collections::HashMap; @@ -69,8 +69,7 @@ impl From for Screens { } pub fn register(lua: &Lua) -> anyhow::Result<()> { - let wezterm_mod = get_or_create_module(lua, "wezterm")?; - let window_mod = lua.create_table()?; + let window_mod = get_or_create_sub_module(lua, "gui")?; window_mod.set( "screens", @@ -84,6 +83,5 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> { })?, )?; - wezterm_mod.set("gui", window_mod)?; Ok(()) }