From ccea650a93c4f8ce29703ba2e1c0e91ff00d9046 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 6 Oct 2020 22:04:48 -0700 Subject: [PATCH] add wezterm.on(), wezterm.emit() * `wezterm.on("event-name", func)` * `wezterm.emit("event-name", "arg1", "arg2")` `on` allows registering multiple functions. `emit` will call each of the registered functions in turn, passing a copy of the arguments. If a handler returns false, no additional handlers are called and `emit` will return false. Otherwise, once all the handlers have been called, `emit` will return true. `emit` is capable of being called by async code. These functions are available to the config layer, but nothing in wezterm uses them at this time. refs: #225 --- Cargo.lock | 2 + config/Cargo.toml | 4 ++ config/src/lua.rs | 177 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index c6315ce20..ef3cd7706 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -695,7 +695,9 @@ dependencies = [ "mlua", "notify", "portable-pty", + "pretty_env_logger", "serde", + "smol", "termwiz", "toml", "vergen", diff --git a/config/Cargo.toml b/config/Cargo.toml index 59c094b89..e78340729 100644 --- a/config/Cargo.toml +++ b/config/Cargo.toml @@ -10,6 +10,10 @@ build = "build.rs" [build-dependencies] vergen = "3" +[dev-dependencies] +pretty_env_logger = "0.4" +smol = "1.2" + [dependencies] anyhow = "1.0" bstr = "0.2" diff --git a/config/src/lua.rs b/config/src/lua.rs index ada1e5ea6..e04285d13 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -101,6 +101,8 @@ pub fn make_lua_context(config_dir: &Path) -> anyhow::Result { 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("run_child_process", lua.create_function(run_child_process)?)?; + wezterm_mod.set("on", lua.create_function(register_event)?)?; + wezterm_mod.set("emit", lua.create_async_function(emit_event)?)?; package.set("path", path_array.join(";"))?; @@ -259,6 +261,90 @@ fn split_by_newlines<'lua>(_: &'lua Lua, text: String) -> mlua::Result( + 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 { + 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::() { + 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), + } +} + /// Ungh: https://github.com/microsoft/WSL/issues/4456 fn utf16_to_utf8<'lua>(_: &'lua Lua, text: mlua::String) -> mlua::Result { let bytes = text.as_bytes(); @@ -301,3 +387,94 @@ fn run_child_process<'lua>( output.stderr.into(), )) } + +#[cfg(test)] +mod test { + use super::*; + use std::sync::{Arc, Mutex}; + + #[test] + fn can_register_and_emit_multiple_events() -> anyhow::Result<()> { + let _ = pretty_env_logger::formatted_builder() + .is_test(true) + .filter_level(log::LevelFilter::Trace) + .try_init(); + + let lua = make_lua_context(&std::env::current_dir()?)?; + + 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) + wezterm.log_error("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) + wezterm.log_error("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(()) + } +}