1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-01 00:35:46 +03:00

lua: add wezterm.hostname() function

This enables host-specific conditional logic in the config file
This commit is contained in:
Wez Furlong 2020-02-27 08:10:03 -08:00
parent 0792f43739
commit 52e4837fe2

View File

@ -87,6 +87,7 @@ pub fn make_lua_context(config_dir: &Path) -> anyhow::Result<Lua> {
)?;
wezterm_mod.set("font", lua.create_function(font_family)?)?;
wezterm_mod.set("hostname", lua.create_function(hostname)?)?;
package.set("path", path_array.join(";"))?;
@ -97,6 +98,17 @@ pub fn make_lua_context(config_dir: &Path) -> anyhow::Result<Lua> {
Ok(lua)
}
/// Returns the system hostname.
/// Errors may occur while retrieving the hostname from the system,
/// or if the hostname isn't a UTF-8 string.
fn hostname<'lua>(_: &'lua Lua, _: ()) -> mlua::Result<String> {
let hostname = hostname::get().map_err(|e| mlua::Error::external(e))?;
match hostname.to_str() {
Some(hostname) => Ok(hostname.to_owned()),
None => Err(mlua::Error::external(anyhow!("hostname isn't UTF-8"))),
}
}
/// Given a simple font family name, returns the fiddly lua table equivalent
/// of the underlying data structure:
/// `{ font = {{ family = FAMILY }}}`