Files
LLeMbas-TTY/config/src/lua.rs
T
2022-05-24 17:24:19 -07:00

667 lines
23 KiB
Rust

use crate::keyassignment::KeyAssignment;
use crate::{
FontAttributes, FontStretch, FontStyle, FontWeight, FreeTypeLoadTarget, Gradient, RgbaColor,
TextStyle,
};
use anyhow::anyhow;
use luahelper::from_lua_value_dynamic;
use mlua::{FromLua, Lua, Table, ToLuaMulti, Value, Variadic};
use ordered_float::NotNan;
use std::convert::TryFrom;
use std::path::Path;
use std::sync::Mutex;
use wezterm_dynamic::{FromDynamic, ToDynamic};
pub use mlua;
static LUA_REGISTRY_USER_CALLBACK_COUNT: &str = "wezterm-user-callback-count";
pub type SetupFunc = fn(&Lua) -> anyhow::Result<()>;
lazy_static::lazy_static! {
static ref SETUP_FUNCS: Mutex<Vec<SetupFunc>> = Mutex::new(vec![]);
}
pub fn add_context_setup_func(func: SetupFunc) {
SETUP_FUNCS.lock().unwrap().push(func);
}
pub fn get_or_create_module<'lua>(lua: &'lua Lua, name: &str) -> anyhow::Result<mlua::Table<'lua>> {
let globals = lua.globals();
let package: Table = globals.get("package")?;
let loaded: Table = package.get("loaded")?;
let module = loaded.get(name)?;
match module {
Value::Nil => {
let module = lua.create_table()?;
loaded.set(name, module.clone())?;
Ok(module)
}
Value::Table(table) => Ok(table),
wat => anyhow::bail!(
"cannot register module {} as package.loaded.{} is already set to a value of type {}",
name,
name,
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
/// 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_file: &Path) -> anyhow::Result<Lua> {
let lua = Lua::new();
let config_dir = config_file.parent().unwrap_or_else(|| Path::new("/"));
{
let globals = lua.globals();
// This table will be the `wezterm` module in the script
let wezterm_mod = get_or_create_module(&lua, "wezterm")?;
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::HOME_DIR.join(".wezterm"));
prefix_path(&mut path_array, &crate::CONFIG_DIR);
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"));
}
}
}
let config_file_str = config_file
.to_str()
.ok_or_else(|| anyhow!("config file path is not UTF-8"))?;
wezterm_mod.set("config_file", config_file_str)?;
wezterm_mod.set(
"config_dir",
config_dir
.to_str()
.ok_or_else(|| anyhow!("config dir path is not UTF-8"))?,
)?;
lua.set_named_registry_value("wezterm-watch-paths", Vec::<String>::new())?;
wezterm_mod.set(
"add_to_config_reload_watch_list",
lua.create_function(add_to_config_reload_watch_list)?,
)?;
wezterm_mod.set("target_triple", crate::wezterm_target_triple())?;
wezterm_mod.set("version", crate::wezterm_version())?;
wezterm_mod.set("home_dir", crate::HOME_DIR.to_str())?;
wezterm_mod.set(
"running_under_wsl",
lua.create_function(|_, ()| Ok(crate::running_under_wsl()))?,
)?;
wezterm_mod.set(
"default_wsl_domains",
lua.create_function(|_, ()| Ok(crate::WslDomain::default_domains()))?,
)?;
wezterm_mod.set(
"get_builtin_color_schemes",
lua.create_function(|_, ()| Ok(crate::COLOR_SCHEMES.clone()))?,
)?;
wezterm_mod.set("font", lua.create_function(font)?)?;
wezterm_mod.set(
"font_with_fallback",
lua.create_function(font_with_fallback)?,
)?;
wezterm_mod.set("hostname", lua.create_function(hostname)?)?;
wezterm_mod.set("action", luahelper::enumctor::Enum::<KeyAssignment>::new())?;
lua.set_named_registry_value(LUA_REGISTRY_USER_CALLBACK_COUNT, 0)?;
wezterm_mod.set("action_callback", lua.create_function(action_callback)?)?;
wezterm_mod.set("utf16_to_utf8", lua.create_function(utf16_to_utf8)?)?;
wezterm_mod.set("split_by_newlines", lua.create_function(split_by_newlines)?)?;
wezterm_mod.set("on", lua.create_function(register_event)?)?;
wezterm_mod.set("emit", lua.create_async_function(emit_event)?)?;
wezterm_mod.set("sleep_ms", lua.create_async_function(sleep_ms)?)?;
wezterm_mod.set("strftime", lua.create_function(strftime)?)?;
wezterm_mod.set("strftime_utc", lua.create_function(strftime_utc)?)?;
wezterm_mod.set("gradient_colors", lua.create_function(gradient_colors)?)?;
package.set("path", path_array.join(";"))?;
}
for func in SETUP_FUNCS.lock().unwrap().iter() {
func(&lua)?;
}
Ok(lua)
}
fn strftime_utc<'lua>(_: &'lua Lua, format: String) -> mlua::Result<String> {
use chrono::prelude::*;
let local: DateTime<Utc> = Utc::now();
Ok(local.format(&format).to_string())
}
fn strftime<'lua>(_: &'lua Lua, format: String) -> mlua::Result<String> {
use chrono::prelude::*;
let local: DateTime<Local> = Local::now();
Ok(local.format(&format).to_string())
}
async fn sleep_ms<'lua>(_: &'lua Lua, milliseconds: u64) -> mlua::Result<()> {
let duration = std::time::Duration::from_millis(milliseconds);
smol::Timer::after(duration).await;
Ok(())
}
/// 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"))),
}
}
#[derive(Debug, Default, FromDynamic, ToDynamic, Clone, PartialEq, Eq, Hash)]
struct TextStyleAttributes {
/// Whether the font should be a bold variant
#[dynamic(default)]
pub bold: Option<bool>,
#[dynamic(default)]
pub weight: Option<FontWeight>,
#[dynamic(default)]
pub stretch: FontStretch,
/// Whether the font should be an italic variant
#[dynamic(default)]
pub style: FontStyle,
// Ideally we'd simply use serde's aliasing functionality on the `style`
// field to support backwards compatibility, but aliases are invisible
// to serde_lua, so we do a little fixup here ourselves in our from_lua impl.
italic: Option<bool>,
/// If set, when rendering text that is set to the default
/// foreground color, use this color instead. This is most
/// useful in a `[[font_rules]]` section to implement changing
/// the text color for eg: bold text.
pub foreground: Option<RgbaColor>,
}
impl<'lua> FromLua<'lua> for TextStyleAttributes {
fn from_lua(value: Value<'lua>, _lua: &'lua Lua) -> Result<Self, mlua::Error> {
let mut attr: TextStyleAttributes = from_lua_value_dynamic(value)?;
if let Some(italic) = attr.italic.take() {
attr.style = if italic {
FontStyle::Italic
} else {
FontStyle::Normal
};
}
Ok(attr)
}
}
#[derive(Debug, Default, FromDynamic, ToDynamic, Clone, PartialEq, Eq, Hash)]
struct LuaFontAttributes {
/// The font family name
pub family: String,
/// Whether the font should be a bold variant
#[dynamic(default)]
pub weight: FontWeight,
#[dynamic(default)]
pub stretch: FontStretch,
/// Whether the font should be an italic variant
#[dynamic(default)]
pub style: FontStyle,
// Ideally we'd simply use serde's aliasing functionality on the `style`
// field to support backwards compatibility, but aliases are invisible
// to serde_lua, so we do a little fixup here ourselves in our from_lua impl.
#[dynamic(default)]
italic: Option<bool>,
#[dynamic(default)]
pub harfbuzz_features: Option<Vec<String>>,
#[dynamic(default)]
pub freetype_load_target: Option<FreeTypeLoadTarget>,
#[dynamic(default)]
pub freetype_render_target: Option<FreeTypeLoadTarget>,
#[dynamic(default)]
pub freetype_load_flags: Option<String>,
#[dynamic(default)]
pub scale: Option<NotNan<f64>>,
}
impl<'lua> FromLua<'lua> for LuaFontAttributes {
fn from_lua(value: Value<'lua>, _lua: &'lua Lua) -> Result<Self, mlua::Error> {
match value {
Value::String(s) => {
let mut attr = LuaFontAttributes::default();
attr.family = s.to_str()?.to_string();
Ok(attr)
}
v => {
let mut attr: LuaFontAttributes = from_lua_value_dynamic(v)?;
if let Some(italic) = attr.italic.take() {
attr.style = if italic {
FontStyle::Italic
} else {
FontStyle::Normal
};
}
Ok(attr)
}
}
}
}
/// On macOS, both Menlo and Monaco fonts have ligatures for `fi` that
/// take effect for words like `find` and which are a source of
/// confusion/annoyance and issues filed on Github.
/// Let's default to disabling ligatures for these fonts unless
/// the user has explicitly specified harfbuzz_features.
/// <https://github.com/wez/wezterm/issues/1736>
/// <https://github.com/wez/wezterm/issues/1786>
fn disable_ligatures_for_menlo_or_monaco(mut attrs: FontAttributes) -> FontAttributes {
if attrs.harfbuzz_features.is_none() && (attrs.family == "Menlo" || attrs.family == "Monaco") {
attrs.harfbuzz_features = Some(vec![
"kern".to_string(),
"clig".to_string(),
"liga=0".to_string(),
]);
}
attrs
}
/// Given a simple font family name, returns a text style instance.
/// The second optional argument is a list of the other TextStyle
/// fields, which at the time of writing includes only the
/// `foreground` color that can be used to force a particular
/// color to be used for this text style.
///
/// `wezterm.font("foo", {foreground="tomato"})`
/// yields:
/// `{ font = {{ family = "foo" }}, foreground="tomato"}`
fn font<'lua>(
_lua: &'lua Lua,
(mut attrs, map_defaults): (LuaFontAttributes, Option<TextStyleAttributes>),
) -> mlua::Result<TextStyle> {
let mut text_style = TextStyle::default();
text_style.font.clear();
if let Some(map_defaults) = map_defaults {
attrs.weight = match map_defaults.bold {
Some(true) => FontWeight::BOLD,
Some(false) => FontWeight::REGULAR,
None => map_defaults.weight.unwrap_or(FontWeight::REGULAR),
};
attrs.stretch = map_defaults.stretch;
attrs.style = map_defaults.style;
text_style.foreground = map_defaults.foreground;
}
text_style
.font
.push(disable_ligatures_for_menlo_or_monaco(FontAttributes {
family: attrs.family,
stretch: attrs.stretch,
weight: attrs.weight,
style: attrs.style,
is_fallback: false,
is_synthetic: false,
harfbuzz_features: attrs.harfbuzz_features,
freetype_load_target: attrs.freetype_load_target,
freetype_render_target: attrs.freetype_render_target,
freetype_load_flags: match attrs.freetype_load_flags {
Some(flags) => {
Some(TryFrom::try_from(flags).map_err(|e| mlua::Error::external(e))?)
}
None => None,
},
scale: attrs.scale,
}));
Ok(text_style)
}
/// Given a list of font family names in order of preference, return a
/// text style instance for that font configuration.
///
/// `wezterm.font_with_fallback({"Operator Mono", "DengXian"})`
///
/// The second optional argument is a list of other TextStyle fields,
/// as described by the `wezterm.font` documentation.
fn font_with_fallback<'lua>(
_lua: &'lua Lua,
(fallback, map_defaults): (Vec<LuaFontAttributes>, Option<TextStyleAttributes>),
) -> mlua::Result<TextStyle> {
let mut text_style = TextStyle::default();
text_style.font.clear();
for (idx, mut attrs) in fallback.into_iter().enumerate() {
if let Some(map_defaults) = &map_defaults {
attrs.weight = match map_defaults.bold {
Some(true) => FontWeight::BOLD,
Some(false) => FontWeight::REGULAR,
None => map_defaults.weight.unwrap_or(FontWeight::REGULAR),
};
attrs.stretch = map_defaults.stretch;
attrs.style = map_defaults.style;
text_style.foreground = map_defaults.foreground;
}
text_style
.font
.push(disable_ligatures_for_menlo_or_monaco(FontAttributes {
family: attrs.family,
stretch: attrs.stretch,
weight: attrs.weight,
style: attrs.style,
is_fallback: idx != 0,
is_synthetic: false,
harfbuzz_features: attrs.harfbuzz_features,
freetype_load_target: attrs.freetype_load_target,
freetype_render_target: attrs.freetype_render_target,
freetype_load_flags: match attrs.freetype_load_flags {
Some(flags) => {
Some(TryFrom::try_from(flags).map_err(|e| mlua::Error::external(e))?)
}
None => None,
},
scale: attrs.scale,
}));
}
Ok(text_style)
}
fn action_callback<'lua>(lua: &'lua Lua, callback: mlua::Function) -> mlua::Result<KeyAssignment> {
let callback_count: i32 = lua.named_registry_value(LUA_REGISTRY_USER_CALLBACK_COUNT)?;
let user_event_id = format!("user-defined-{}", callback_count);
lua.set_named_registry_value(LUA_REGISTRY_USER_CALLBACK_COUNT, callback_count + 1)?;
register_event(lua, (user_event_id.clone(), callback))?;
return Ok(KeyAssignment::EmitEvent(user_event_id));
}
fn split_by_newlines<'lua>(_: &'lua Lua, text: String) -> mlua::Result<Vec<String>> {
Ok(text
.lines()
.map(|s| {
// Ungh, `str.lines()` is supposed to split by `\n` or `\r\n`, but I've
// found that it is necessary to have an additional trim here in order
// to actually remove the `\r`.
s.trim_end_matches('\r').to_string()
})
.collect())
}
/// This implements `wezterm.on`, whose goal is to register an event handler
/// callback.
/// The callback function may return `false` to prevent other handlers from
/// triggering. The `false` return means "prevent the default action",
/// and thus, depending on the semantics of the emitted event, can be used
/// to override rather augment built-in behavior.
///
/// To allow the default action you can omit a return statement, or
/// explicitly return `true`.
///
/// The arguments to the handler are passed through from the corresponding
/// `wezterm.emit` call.
///
/// ```lua
/// wezterm.on("event-name", function(arg1, arg2)
/// -- do something
/// return false -- if you want to prevent other handlers running
/// end);
///
/// wezterm.emit("event-name", "foo", "bar");
/// ```
fn register_event<'lua>(
lua: &'lua Lua,
(name, func): (String, mlua::Function),
) -> mlua::Result<()> {
let decorated_name = format!("wezterm-event-{}", name);
let tbl: mlua::Value = lua.named_registry_value(&decorated_name)?;
match tbl {
mlua::Value::Nil => {
let tbl = lua.create_table()?;
tbl.set(1, func)?;
lua.set_named_registry_value(&decorated_name, tbl)?;
Ok(())
}
mlua::Value::Table(tbl) => {
let len = tbl.raw_len();
tbl.set(len + 1, func)?;
Ok(())
}
_ => Err(mlua::Error::external(anyhow!(
"registry key for {} has invalid type",
decorated_name
))),
}
}
/// This implements `wezterm.emit`.
/// The first parameter to emit is the name of a signal that may or may not
/// have previously been registered via `wezterm.on`.
/// `wezterm.emit` will call each of the registered handlers in the order
/// that they were registered and pass the remainder of the `emit` arguments
/// to those handler functions.
/// If a handler returns `false` then `wezterm.emit` will stop calling
/// any additional handlers and then return `false`.
/// Otherwise, once all handlers have been called and none of them returned
/// `false`, `wezterm.emit` will return `true`.
/// The return value indicates to the caller whether the default action
/// should take place.
pub async fn emit_event<'lua>(
lua: &'lua Lua,
(name, args): (String, mlua::MultiValue<'lua>),
) -> mlua::Result<bool> {
let decorated_name = format!("wezterm-event-{}", name);
let tbl: mlua::Value = lua.named_registry_value(&decorated_name)?;
match tbl {
mlua::Value::Table(tbl) => {
for func in tbl.sequence_values::<mlua::Function>() {
let func = func?;
match func.call_async(args.clone()).await? {
mlua::Value::Boolean(b) if !b => {
// Default action prevented
return Ok(false);
}
_ => {
// Continue with other handlers
}
}
}
Ok(true)
}
_ => Ok(true),
}
}
pub fn emit_sync_callback<'lua, A>(
lua: &'lua Lua,
(name, args): (String, A),
) -> mlua::Result<mlua::Value<'lua>>
where
A: ToLuaMulti<'lua>,
{
let decorated_name = format!("wezterm-event-{}", name);
let tbl: mlua::Value = lua.named_registry_value(&decorated_name)?;
match tbl {
mlua::Value::Table(tbl) => {
for func in tbl.sequence_values::<mlua::Function>() {
let func = func?;
return func.call(args);
}
Ok(mlua::Value::Nil)
}
_ => Ok(mlua::Value::Nil),
}
}
/// Ungh: https://github.com/microsoft/WSL/issues/4456
fn utf16_to_utf8<'lua>(_: &'lua Lua, text: mlua::String) -> mlua::Result<String> {
let bytes = text.as_bytes();
if bytes.len() % 2 != 0 {
return Err(mlua::Error::external(anyhow!(
"input data has odd length, cannot be utf16"
)));
}
// This is "safe" because we checked that the length seems reasonable,
// and our new slice is within those same bounds.
let wide: &[u16] =
unsafe { std::slice::from_raw_parts(bytes.as_ptr() as *const u16, bytes.len() / 2) };
String::from_utf16(wide).map_err(|e| mlua::Error::external(e))
}
fn gradient_colors<'lua>(
_lua: &'lua Lua,
(gradient, num_colors): (Gradient, usize),
) -> mlua::Result<Vec<String>> {
let g = gradient.build().map_err(|e| mlua::Error::external(e))?;
Ok(g.colors(num_colors)
.into_iter()
.map(|c| c.to_hex_string())
.collect())
}
pub fn add_to_config_reload_watch_list<'lua>(
lua: &'lua Lua,
args: Variadic<String>,
) -> mlua::Result<()> {
let mut watch_paths: Vec<String> = lua.named_registry_value("wezterm-watch-paths")?;
watch_paths.extend_from_slice(&args);
lua.set_named_registry_value("wezterm-watch-paths", watch_paths)?;
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
use std::sync::{Arc, Mutex};
#[test]
fn can_register_and_emit_multiple_events() -> anyhow::Result<()> {
let _ = env_logger::Builder::new()
.is_test(true)
.filter_level(log::LevelFilter::Trace)
.try_init();
let lua = make_lua_context(Path::new("testing"))?;
let total = Arc::new(Mutex::new(0));
let first = lua.create_function({
let total = total.clone();
move |_lua: &mlua::Lua, n: i32| {
let mut l = total.lock().unwrap();
*l += n;
Ok(())
}
})?;
let second = lua.create_function({
let total = total.clone();
move |_lua: &mlua::Lua, n: i32| {
let mut l = total.lock().unwrap();
*l += n * 2;
// Prevent any later functions from being called
Ok(false)
}
})?;
let third = lua.create_function({
let total = total.clone();
move |_lua: &mlua::Lua, n: i32| {
let mut l = total.lock().unwrap();
*l += n * 3;
Ok(())
}
})?;
register_event(&lua, ("foo".to_string(), first))?;
register_event(&lua, ("foo".to_string(), second))?;
register_event(&lua, ("foo".to_string(), third))?;
register_event(
&lua,
(
"bar".to_string(),
lua.create_function(|_: &mlua::Lua, (a, b): (i32, String)| {
eprintln!("a: {}, b: {}", a, b);
Ok(())
})?,
),
)?;
smol::block_on(
lua.load(
r#"
local wezterm = require 'wezterm';
wezterm.on('foo', function (n)
print("lua hook recording " .. n);
end);
-- one of the foo handlers returns false, so the emit
-- returns false overall, indicating that the default
-- action should not be taken
assert(wezterm.emit('foo', 2) == false)
wezterm.on('bar', function (n, str)
print("bar says " .. n .. " " .. str)
end);
-- None of the bar handlers return anything, so the
-- emit returns true to indicate that the default
-- action should be performed
assert(wezterm.emit('bar', 42, 'woot') == true)
"#,
)
.exec_async(),
)?;
assert_eq!(*total.lock().unwrap(), 6);
Ok(())
}
}