1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 15:04:36 +03:00

lua: reduce a bit of boilerplate around registering wezterm.submodule

This commit is contained in:
Wez Furlong 2022-07-06 15:26:21 -07:00
parent d130164fb9
commit f7848cf96d
3 changed files with 24 additions and 8 deletions

View File

@ -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<mlua::Table<'lua>> {
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

View File

@ -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<Rc<Mux>> {
}
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(())
}

View File

@ -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<window::screen::Screens> 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(())
}