Files
LLeMbas-TTY/src/scripting/mod.rs
T
Wez Furlong ab03147bd9 config: we can now read ~/.wezterm.lua
This allows for slightly more fancy configuration in the future, but for
now it is rather simple: your lua script returns a configuration struct
with the same shape as that from the TOML file.

A `wezterm` module is provided to the script that provides some
constants to help understand the environment in which wezterm
is running.

I want to add some helpers that make setting up the fonts feel less
weird (lots of nesting in the data model makes this weird).

The ability to conditionally construct configuration is powerful
and helps to address the broader request in
refs: https://github.com/wez/wezterm/issues/152

An example config looks like this:

```lua
local wezterm = require 'wezterm';

print(wezterm.config_dir);
print(wezterm.executable_dir);
wezterm.log_error("w00t! running " .. wezterm.version
  .. " on " .. wezterm.target_triple ..  " " .. wezterm.home_dir);

return {
  enable_scroll_bar = true,
  enable_tab_bar = true,
  ratelimit_output_bytes_per_second = 400000,
  scrollback_lines = 350000,
  font_dirs = {".dotfiles/fonts"},

  window_padding = {
    left = 2,
    bottom = 2,
  },

  font = {
    font = {{
      family = "Operator Mono SSm Lig Medium",
    }},
  },

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

  ssh_domains = {
    {
      name = "localhost",
      remote_address = "localhost",
      username = "wez",
    },
  },

  tls_clients = {
    {
      name = "cubetls",
      remote_address = "cube-localdomain:8080",
      bootstrap_via_ssh = "cube-localdomain",
    },
  },

  tls_servers = {
    {
      bind_address = "192.168.1.8:8080",
    },
  },

  hyperlink_rules = {
    {
      regex = "\\b\\w+://(?:[\\w.-]+)\\.[a-z]{2,15}\\S*\\b",
      format = "$0",
    },
  },

  font_rules= {
    {
      italic = true,
      font = {
        font = {{
          family = "Operator Mono SSm Lig Medium Italic",
        }}
      },
    },

    {
      italic = true,
      intensity = "Bold",
      font = {
        font = {{
          family = "Operator Mono SSm Lig Book Italic",
        }}
      },
    },

    {
      intensity = "Bold",
      font = {
        foreground = "tomato",
        font = {{
          family = "Operator Mono SSm Lig Bold",
        }}
      },
    },

    {
      intensity = "Half",
      font = {
        font = {{
          family = "Operator Mono SSm Lig Light",
        }}
      },
    },
  },
}
```
2020-02-26 22:11:23 -08:00

97 lines
3.4 KiB
Rust

use anyhow::anyhow;
use mlua::{Lua, Table};
use std::path::Path;
mod serde_lua;
pub use serde_lua::from_lua_value;
/// 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
/// the environment.
///
/// The `package.path` is configured to search the user's
/// wezterm specific config paths for lua modules, should
/// they choose to `require` additional code from their config.
///
/// A `wezterm` module is registered so that the script can
/// `require "wezterm"` and call into functions provided by
/// wezterm. The wezterm module contains:
/// * `executable_dir` - the directory containing the wezterm
/// executable. This is potentially useful for portable
/// installs on Windows.
/// * `config_dir` - the directory containing the wezterm
/// configuration.
/// * `log_error` - a function that logs to stderr (or the server
/// log file for daemonized wezterm).
/// * `target_triple` - the rust compilation target triple.
/// * `version` - the version of the running wezterm instance.
/// * `home_dir` - the path to the user's home directory
///
/// In addition to this, the lua standard library, except for
/// the `debug` module, is also available to the script.
pub fn make_lua_context(config_dir: &Path) -> anyhow::Result<Lua> {
let lua = Lua::new();
{
let globals = lua.globals();
// This table will be the `wezterm` module in the script
let wezterm_mod = lua.create_table()?;
let package: Table = globals.get("package")?;
let package_path: String = package.get("path")?;
let mut path_array: Vec<String> = package_path.split(";").map(|s| s.to_owned()).collect();
fn prefix_path(array: &mut Vec<String>, path: &Path) {
array.insert(0, format!("{}/?.lua", path.display()));
array.insert(1, format!("{}/?/init.lua", path.display()));
}
prefix_path(&mut path_array, &crate::config::HOME_DIR.join(".wezterm"));
prefix_path(
&mut path_array,
&crate::config::HOME_DIR.join(".config").join("wezterm"),
);
if let Ok(exe) = std::env::current_exe() {
if let Some(path) = exe.parent() {
wezterm_mod.set(
"executable_dir",
path.to_str().ok_or_else(|| anyhow!("path is not UTF-8"))?,
)?;
if cfg!(windows) {
// For a portable windows install, force in this path ahead
// of the rest
prefix_path(&mut path_array, &path.join("wezterm_modules"));
}
}
}
wezterm_mod.set(
"config_dir",
config_dir
.to_str()
.ok_or_else(|| anyhow!("config dir path is not UTF-8"))?,
)?;
wezterm_mod.set("target_triple", env!("VERGEN_TARGET_TRIPLE"))?;
wezterm_mod.set("version", crate::wezterm_version())?;
wezterm_mod.set("home_dir", crate::config::HOME_DIR.to_str())?;
wezterm_mod.set(
"log_error",
lua.create_function(|_, msg: String| {
log::error!("lua: {}", msg);
Ok(())
})?,
)?;
package.set("path", path_array.join(";"))?;
let loaded: Table = package.get("loaded")?;
loaded.set("wezterm", wezterm_mod)?;
}
Ok(lua)
}