From 9d9f3c3c1af7d94f4e13c8ff3f6306f3f037a5ef Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 9 Oct 2020 13:45:34 -0700 Subject: [PATCH] lua: add GuiWin and PaneObject proxies for use in script This commit adds very basic first passes at representing the Pane and GuiWindow types in lua script. The `open-uri` event from 9397f2a2dbdb47b43e18e5efdabf3ef8a0b18936 has been redefined to receive `(window, pane, uri)` parameters instead of its prior very basic `uri` parameter. A new key assignment `wezterm.action{EmitEvent="event-name"}` is now available that allows a key binding assignment to emit an arbitrary event, which in turn allows for triggering an arbitrary lua callback in response to a key or mouse click. `EmitEvent` passes the `(window, pane)` from the triggering window and pane as parameters. Here's a brief example: ```lua local wezterm = require 'wezterm'; wezterm.on("my-thingy", function(window, pane) local dims = pane:get_dimensions(); wezterm.log_error("did my thingy with window " .. window:window_id() .. " pane " .. pane:pane_id() .. " " .. dims.cols .. "x" .. dims.viewport_rows); window:perform_action("IncreaseFontSize", pane); end) return { keys = { {key="E", mods="CTRL", action=wezterm.action{EmitEvent="my-thingy"}}, } } ``` refs: #223 refs: #225 --- Cargo.lock | 1 + config/src/keyassignment.rs | 1 + config/src/lib.rs | 2 +- luahelper/src/lib.rs | 24 ++++++-------- mux/Cargo.toml | 1 + mux/src/renderable.rs | 3 ++ wezterm/src/gui/mod.rs | 1 + wezterm/src/gui/termwindow.rs | 43 +++++++++++++++++++++--- wezterm/src/main.rs | 1 + wezterm/src/scripting/guiwin.rs | 59 +++++++++++++++++++++++++++++++++ wezterm/src/scripting/mod.rs | 6 ++++ wezterm/src/scripting/pane.rs | 52 +++++++++++++++++++++++++++++ 12 files changed, 175 insertions(+), 19 deletions(-) create mode 100644 wezterm/src/scripting/guiwin.rs create mode 100644 wezterm/src/scripting/mod.rs create mode 100644 wezterm/src/scripting/pane.rs diff --git a/Cargo.lock b/Cargo.lock index 323959961..0f7729fa4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2048,6 +2048,7 @@ dependencies = [ "downcast-rs", "libc", "log", + "luahelper", "portable-pty", "promise", "rangeset", diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index a312ba816..5f88e5624 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -155,6 +155,7 @@ pub enum KeyAssignment { ActivatePaneDirection(PaneDirection), TogglePaneZoomState, CloseCurrentPane { confirm: bool }, + EmitEvent(String), } impl_lua_conversion!(KeyAssignment); diff --git a/config/src/lib.rs b/config/src/lib.rs index e26fff1e9..1c4bf413d 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -142,7 +142,7 @@ pub fn designate_this_as_the_main_thread() { /// call from a secondary thread. pub async fn with_lua_config_on_main_thread(func: F) -> anyhow::Result where - F: Fn(Option>) -> RETF, + F: FnOnce(Option>) -> RETF, RETF: Future>, { let lua = LUA_CONFIG.with(|lc| { diff --git a/luahelper/src/lib.rs b/luahelper/src/lib.rs index 40a881776..32415f1fa 100644 --- a/luahelper/src/lib.rs +++ b/luahelper/src/lib.rs @@ -1,6 +1,7 @@ #![macro_use] mod serde_lua; +pub use mlua; pub use serde_lua::from_lua_value; pub use serde_lua::ser::to_lua_value; @@ -13,27 +14,22 @@ pub use serde_lua::ser::to_lua_value; #[macro_export] macro_rules! impl_lua_conversion { ($struct:ident) => { - impl<'lua> mlua::ToLua<'lua> for $struct { - fn to_lua(self, lua: &'lua mlua::Lua) -> Result, mlua::Error> { + impl<'lua> $crate::mlua::ToLua<'lua> for $struct { + fn to_lua( + self, + lua: &'lua $crate::mlua::Lua, + ) -> Result<$crate::mlua::Value<'lua>, $crate::mlua::Error> { Ok(luahelper::to_lua_value(lua, self)?) } } - impl<'lua> mlua::FromLua<'lua> for $struct { + impl<'lua> $crate::mlua::FromLua<'lua> for $struct { fn from_lua( - value: mlua::Value<'lua>, - _lua: &'lua mlua::Lua, - ) -> Result { + value: $crate::mlua::Value<'lua>, + _lua: &'lua $crate::mlua::Lua, + ) -> Result { Ok(luahelper::from_lua_value(value)?) } } }; } - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} diff --git a/mux/Cargo.toml b/mux/Cargo.toml index f6c87e3a9..7dbfc1f16 100644 --- a/mux/Cargo.toml +++ b/mux/Cargo.toml @@ -14,6 +14,7 @@ config = { path = "../config" } downcast-rs = "1.0" libc = "0.2" log = "0.4" +luahelper = { path = "../luahelper" } portable-pty = { path = "../pty", features = ["serde_support"]} promise = { path = "../promise" } rangeset = { path = "../rangeset" } diff --git a/mux/src/renderable.rs b/mux/src/renderable.rs index 4618f4e1d..6c2020431 100644 --- a/mux/src/renderable.rs +++ b/mux/src/renderable.rs @@ -1,5 +1,6 @@ use config::configuration; use downcast_rs::{impl_downcast, Downcast}; +use luahelper::impl_lua_conversion; use rangeset::RangeSet; use serde::{Deserialize, Serialize}; use std::ops::Range; @@ -13,6 +14,7 @@ pub struct StableCursorPosition { pub shape: termwiz::surface::CursorShape, pub visibility: termwiz::surface::CursorVisibility, } +impl_lua_conversion!(StableCursorPosition); #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize, Serialize)] pub struct RenderableDimensions { @@ -33,6 +35,7 @@ pub struct RenderableDimensions { /// expressed as a stable index. pub scrollback_top: StableRowIndex, } +impl_lua_conversion!(RenderableDimensions); /// Renderable allows passing something that isn't an actual wezterm_term::Terminal /// instance into the renderer, which opens up remoting of the terminal diff --git a/wezterm/src/gui/mod.rs b/wezterm/src/gui/mod.rs index 6ed3a992b..ff401fb66 100644 --- a/wezterm/src/gui/mod.rs +++ b/wezterm/src/gui/mod.rs @@ -17,6 +17,7 @@ mod termwindow; mod utilsprites; pub use selection::SelectionMode; +pub use termwindow::TermWindow; pub struct GuiFrontEnd { connection: Rc, diff --git a/wezterm/src/gui/termwindow.rs b/wezterm/src/gui/termwindow.rs index 2d35674a1..49d2deb3f 100644 --- a/wezterm/src/gui/termwindow.rs +++ b/wezterm/src/gui/termwindow.rs @@ -12,6 +12,8 @@ use crate::gui::overlay::{ use crate::gui::scrollbar::*; use crate::gui::selection::*; use crate::gui::tabbar::{TabBarItem, TabBarState}; +use crate::scripting::guiwin::GuiWin; +use crate::scripting::pane::PaneObject; use ::wezterm_term::input::MouseButton as TMB; use ::wezterm_term::input::MouseEventKind as TMEK; use ::window::bitmaps::atlas::{OutOfTextureSpace, SpriteSlice}; @@ -251,7 +253,7 @@ pub struct TermWindow { dimensions: Dimensions, /// Terminal dimensions terminal_size: PtySize, - mux_window_id: MuxWindowId, + pub mux_window_id: MuxWindowId, render_metrics: RenderMetrics, render_state: RenderState, input_map: InputMap, @@ -1790,7 +1792,7 @@ impl TermWindow { .detach(); } - fn perform_key_assignment( + pub fn perform_key_assignment( &mut self, pane: &Rc, assignment: &KeyAssignment, @@ -1882,13 +1884,18 @@ impl TermWindow { // perform below; here we allow the user to define an `open-uri` event // handler that can bypass the normal `open::that` functionality. if let Some(link) = self.current_highlight.as_ref().cloned() { + let window = GuiWin::new(self); + let pane = PaneObject::new(pane); + async fn open_uri( lua: Option>, + window: GuiWin, + pane: PaneObject, link: String, ) -> anyhow::Result<()> { let default_click = match lua { Some(lua) => { - let args = lua.pack_multi(link.clone())?; + let args = lua.pack_multi((window, pane, link.clone()))?; config::lua::emit_event(&lua, ("open-uri".to_string(), args)) .await .map_err(|e| { @@ -1908,11 +1915,39 @@ impl TermWindow { } promise::spawn::spawn(config::with_lua_config_on_main_thread(move |lua| { - open_uri(lua, link.uri().to_string()) + open_uri(lua, window, pane, link.uri().to_string()) })) .detach(); } } + EmitEvent(name) => { + let window = GuiWin::new(self); + let pane = PaneObject::new(pane); + + async fn emit_event( + lua: Option>, + name: String, + window: GuiWin, + pane: PaneObject, + ) -> anyhow::Result<()> { + if let Some(lua) = lua { + let args = lua.pack_multi((window, pane))?; + config::lua::emit_event(&lua, (name.clone(), args)) + .await + .map_err(|e| { + log::error!("while processing EmitEvent({}): {:#}", name, e); + e + })?; + } + Ok(()) + } + + let name = name.to_string(); + promise::spawn::spawn(config::with_lua_config_on_main_thread(move |lua| { + emit_event(lua, name, window, pane) + })) + .detach(); + } CompleteSelectionOrOpenLinkAtMouseCursor => { let text = self.selection_text(pane); if !text.is_empty() { diff --git a/wezterm/src/main.rs b/wezterm/src/main.rs index f5c85fa2c..bef027f96 100644 --- a/wezterm/src/main.rs +++ b/wezterm/src/main.rs @@ -21,6 +21,7 @@ mod connui; mod gui; use config::keyassignment; mod markdown; +mod scripting; mod server; mod ssh; mod stats; diff --git a/wezterm/src/scripting/guiwin.rs b/wezterm/src/scripting/guiwin.rs new file mode 100644 index 000000000..456f7cde2 --- /dev/null +++ b/wezterm/src/scripting/guiwin.rs @@ -0,0 +1,59 @@ +//! GuiWin represents a Gui TermWindow (as opposed to a Mux window) in lua code +use super::luaerr; +use super::pane::PaneObject; +use crate::gui::TermWindow; +use anyhow::anyhow; +use config::keyassignment::KeyAssignment; +use mlua::{UserData, UserDataMethods}; +use mux::window::WindowId as MuxWindowId; +use window::WindowOps; + +#[derive(Clone)] +pub struct GuiWin { + mux_window_id: MuxWindowId, + window: ::window::Window, +} + +impl GuiWin { + pub fn new(term_window: &TermWindow) -> Self { + let window = term_window.window.clone().unwrap(); + let mux_window_id = term_window.mux_window_id; + Self { + window, + mux_window_id, + } + } + + async fn with_term_window(&self, mut f: F) -> mlua::Result + where + F: FnMut(&mut TermWindow, &dyn WindowOps) -> anyhow::Result, + F: Send + 'static, + T: Send + 'static, + { + self.window + .apply(move |tw, ops| { + if let Some(term_window) = tw.downcast_mut::() { + f(term_window, ops) + } else { + Err(anyhow!("Window is not TermWindow!?")) + } + }) + .await + .map_err(luaerr) + } +} + +impl UserData for GuiWin { + fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_method("window_id", |_, this, _: ()| Ok(this.mux_window_id)); + methods.add_async_method( + "perform_action", + |_, this, (assignment, pane): (KeyAssignment, PaneObject)| async move { + this.with_term_window(move |term_window, _ops| { + term_window.perform_key_assignment(&pane.pane()?, &assignment) + }) + .await + }, + ); + } +} diff --git a/wezterm/src/scripting/mod.rs b/wezterm/src/scripting/mod.rs new file mode 100644 index 000000000..0064e12fb --- /dev/null +++ b/wezterm/src/scripting/mod.rs @@ -0,0 +1,6 @@ +pub mod guiwin; +pub mod pane; + +fn luaerr(err: anyhow::Error) -> mlua::Error { + mlua::Error::external(err) +} diff --git a/wezterm/src/scripting/pane.rs b/wezterm/src/scripting/pane.rs new file mode 100644 index 000000000..8e45ef8a9 --- /dev/null +++ b/wezterm/src/scripting/pane.rs @@ -0,0 +1,52 @@ +//! PaneObject represents a Mux Pane instance in lua code +use super::luaerr; +use anyhow::anyhow; +use mlua::{UserData, UserDataMethods}; +use mux::pane::{Pane, PaneId}; +use mux::Mux; +use std::rc::Rc; + +#[derive(Clone)] +pub struct PaneObject { + pane: PaneId, +} + +impl PaneObject { + pub fn new(pane: &Rc) -> Self { + Self { + pane: pane.pane_id(), + } + } + + pub fn pane(&self) -> mlua::Result> { + let mux = Mux::get() + .ok_or_else(|| anyhow!("must be called on main thread")) + .map_err(luaerr)?; + mux.get_pane(self.pane) + .ok_or_else(|| anyhow!("pane id {} is not valid", self.pane)) + .map_err(luaerr) + } +} + +impl UserData for PaneObject { + fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_method("pane_id", |_, this, _: ()| Ok(this.pane()?.pane_id())); + methods.add_method("get_title", |_, this, _: ()| Ok(this.pane()?.get_title())); + methods.add_method("get_current_working_dir", |_, this, _: ()| { + Ok(this + .pane()? + .get_current_working_dir() + .map(|u| u.to_string())) + }); + methods.add_method("paste", |_, this, text: String| { + this.pane()?.send_paste(&text).map_err(luaerr)?; + Ok(()) + }); + methods.add_method("get_cursor_position", |_, this, _: ()| { + Ok(this.pane()?.renderer().get_cursor_position()) + }); + methods.add_method("get_dimensions", |_, this, _: ()| { + Ok(this.pane()?.renderer().get_dimensions()) + }); + } +}