Files
LLeMbas-TTY/wezterm/src/scripting/pane.rs
T
Wez Furlong 9d9f3c3c1a 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 9397f2a2db
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
2020-10-09 13:55:36 -07:00

53 lines
1.6 KiB
Rust

//! 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<dyn Pane>) -> Self {
Self {
pane: pane.pane_id(),
}
}
pub fn pane(&self) -> mlua::Result<Rc<dyn Pane>> {
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())
});
}
}