From 42e4a51a2825e1a4166d6bd2dc92571c9494550c Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 11 Sep 2022 07:05:48 -0700 Subject: [PATCH] lua: no longer distinction between gui and mux panes There is no gui-specific functionality for PaneObject, so just fold everything into MuxPane and use that. --- lua-api-crates/mux/src/pane.rs | 147 ++++++++++++++++++++++++++++ wezterm-gui/src/scripting/guiwin.rs | 14 +-- wezterm-gui/src/scripting/mod.rs | 1 - wezterm-gui/src/scripting/pane.rs | 147 ---------------------------- wezterm-gui/src/termwindow/mod.rs | 14 +-- 5 files changed, 161 insertions(+), 162 deletions(-) delete mode 100644 wezterm-gui/src/scripting/pane.rs diff --git a/lua-api-crates/mux/src/pane.rs b/lua-api-crates/mux/src/pane.rs index 99f63f088..decdedfa9 100644 --- a/lua-api-crates/mux/src/pane.rs +++ b/lua-api-crates/mux/src/pane.rs @@ -1,4 +1,5 @@ use super::*; +use luahelper::dynamic_to_lua_value; #[derive(Clone, Copy, Debug)] pub struct MuxPane(pub PaneId); @@ -18,10 +19,12 @@ impl UserData for MuxPane { })) }); methods.add_method("pane_id", |_, this, _: ()| Ok(this.0)); + methods.add_async_method("split", |_, this, args: Option| async move { let args = args.unwrap_or_default(); args.run(this).await }); + methods.add_method("send_paste", |_, this, text: String| { let mux = get_mux()?; let pane = this.resolve(&mux)?; @@ -29,6 +32,17 @@ impl UserData for MuxPane { .map_err(|e| mlua::Error::external(format!("{:#}", e)))?; Ok(()) }); + + // An alias of send-paste for backwards compatibility with prior releases when there was a + // separate Gui-level PaneObject + methods.add_method("paste", |_, this, text: String| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + pane.send_paste(&text) + .map_err(|e| mlua::Error::external(format!("{:#}", e)))?; + Ok(()) + }); + methods.add_method("send_text", |_, this, text: String| { let mux = get_mux()?; let pane = this.resolve(&mux)?; @@ -49,6 +63,139 @@ impl UserData for MuxPane { .resolve_pane_id(this.0) .map(|(_domain_id, _window_id, tab_id)| MuxTab(tab_id))) }); + + // For backwards compatibility with prior releases when there + // was a separate Gui-level PaneObject + methods.add_method("mux_pane", |_, this, _: ()| Ok(*this)); + + methods.add_method("get_title", |_, this, _: ()| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + Ok(pane.get_title()) + }); + + methods.add_method("get_current_working_dir", |_, this, _: ()| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + Ok(pane.get_current_working_dir().map(|u| u.to_string())) + }); + + methods.add_method("get_metadata", |lua, this, _: ()| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + let value = pane.get_metadata(); + dynamic_to_lua_value(lua, value) + }); + + methods.add_method("get_foreground_process_name", |_, this, _: ()| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + Ok(pane.get_foreground_process_name()) + }); + + methods.add_method("get_foreground_process_info", |_, this, _: ()| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + Ok(pane.get_foreground_process_info()) + }); + + methods.add_method("get_cursor_position", |_, this, _: ()| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + Ok(pane.get_cursor_position()) + }); + + methods.add_method("get_dimensions", |_, this, _: ()| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + Ok(pane.get_dimensions()) + }); + + methods.add_method("get_user_vars", |_, this, _: ()| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + Ok(pane.copy_user_vars()) + }); + + methods.add_method("has_unseen_output", |_, this, _: ()| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + Ok(pane.has_unseen_output()) + }); + + methods.add_method("is_alt_screen_active", |_, this, _: ()| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + Ok(pane.is_alt_screen_active()) + }); + + // When called with no arguments, returns the lines from the + // viewport as plain text (no escape sequences). + // When called with an optional integer argument, returns the + // last nlines lines of the terminal output. + // The returned string will have trailing whitespace trimmed. + methods.add_method("get_lines_as_text", |_, this, nlines: Option| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + let dims = pane.get_dimensions(); + let nlines = nlines.unwrap_or(dims.viewport_rows); + let bottom_row = dims.physical_top + dims.viewport_rows as isize; + let top_row = bottom_row.saturating_sub(nlines as isize); + let (_first_row, lines) = pane.get_lines(top_row..bottom_row); + let mut text = String::new(); + for line in lines { + for cell in line.visible_cells() { + text.push_str(cell.str()); + } + let trimmed = text.trim_end().len(); + text.truncate(trimmed); + text.push('\n'); + } + let trimmed = text.trim_end().len(); + text.truncate(trimmed); + Ok(text) + }); + + methods.add_method( + "get_logical_lines_as_text", + |_, this, nlines: Option| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + let dims = pane.get_dimensions(); + let nlines = nlines.unwrap_or(dims.viewport_rows); + let bottom_row = dims.physical_top + dims.viewport_rows as isize; + let top_row = bottom_row.saturating_sub(nlines as isize); + let lines = pane.get_logical_lines(top_row..bottom_row); + let mut text = String::new(); + for line in lines { + for cell in line.logical.visible_cells() { + text.push_str(cell.str()); + } + let trimmed = text.trim_end().len(); + text.truncate(trimmed); + text.push('\n'); + } + let trimmed = text.trim_end().len(); + text.truncate(trimmed); + Ok(text) + }, + ); + + methods.add_method("get_domain_name", |_, this, _: ()| { + let mux = get_mux()?; + let pane = this.resolve(&mux)?; + let mut name = None; + if let Some(mux) = Mux::get() { + let domain_id = pane.domain_id(); + name = mux + .get_domain(domain_id) + .map(|dom| dom.domain_name().to_string()); + } + match name { + Some(name) => Ok(name), + None => Ok("".to_string()), + } + }); } } diff --git a/wezterm-gui/src/scripting/guiwin.rs b/wezterm-gui/src/scripting/guiwin.rs index b4f661fd8..0c8d541bf 100644 --- a/wezterm-gui/src/scripting/guiwin.rs +++ b/wezterm-gui/src/scripting/guiwin.rs @@ -1,6 +1,5 @@ //! GuiWin represents a Gui TermWindow (as opposed to a Mux window) in lua code use super::luaerr; -use super::pane::PaneObject; use crate::termwindow::TermWindowNotif; use crate::TermWindow; use config::keyassignment::{ClipboardCopyDestination, KeyAssignment}; @@ -9,6 +8,7 @@ use mlua::{UserData, UserDataMethods}; use mux::pane::PaneId; use mux::window::WindowId as MuxWindowId; use mux::Mux; +use mux_lua::MuxPane; use termwiz::cell::CellAttributes; use termwiz::surface::{Change, Line}; use termwiz_funcs::new_wezterm_terminfo_renderer; @@ -122,10 +122,10 @@ impl UserData for GuiWin { }); methods.add_async_method( "get_selection_text_for_pane", - |_, this, pane: PaneObject| async move { + |_, this, pane: MuxPane| async move { let (tx, rx) = smol::channel::bounded(1); this.window.notify(TermWindowNotif::GetSelectionForPane { - pane_id: pane.pane, + pane_id: pane.0, tx, }); let text = rx @@ -148,9 +148,9 @@ impl UserData for GuiWin { }); methods.add_method( "perform_action", - |_, this, (assignment, pane): (KeyAssignment, PaneObject)| { + |_, this, (assignment, pane): (KeyAssignment, MuxPane)| { this.window.notify(TermWindowNotif::PerformAssignment { - pane_id: pane.pane, + pane_id: pane.0, assignment, }); Ok(()) @@ -249,9 +249,9 @@ impl UserData for GuiWin { ); methods.add_async_method( "get_selection_escapes_for_pane", - |_, this, pane: PaneObject| async move { + |_, this, pane: MuxPane| async move { let (tx, rx) = smol::channel::bounded(1); - let pane_id = pane.pane; + let pane_id = pane.0; this.window .notify(TermWindowNotif::Apply(Box::new(move |term_window| { fn do_it( diff --git a/wezterm-gui/src/scripting/mod.rs b/wezterm-gui/src/scripting/mod.rs index 29c7f21b0..9311a3b6c 100644 --- a/wezterm-gui/src/scripting/mod.rs +++ b/wezterm-gui/src/scripting/mod.rs @@ -10,7 +10,6 @@ use std::collections::HashMap; use wezterm_dynamic::ToDynamic; pub mod guiwin; -pub mod pane; fn luaerr(err: anyhow::Error) -> mlua::Error { mlua::Error::external(err) diff --git a/wezterm-gui/src/scripting/pane.rs b/wezterm-gui/src/scripting/pane.rs deleted file mode 100644 index fbd331fd2..000000000 --- a/wezterm-gui/src/scripting/pane.rs +++ /dev/null @@ -1,147 +0,0 @@ -//! PaneObject represents a Mux Pane instance in lua code -use super::luaerr; -use anyhow::anyhow; -use luahelper::dynamic_to_lua_value; -use mlua::{UserData, UserDataMethods}; -use mux::pane::{Pane, PaneId}; -use mux::Mux; -use std::rc::Rc; - -#[derive(Clone)] -pub struct PaneObject { - pub 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_meta_method(mlua::MetaMethod::ToString, |_, this, _: ()| { - Ok(format!( - "GuiPane(pane_id:{}, pid:{})", - this.pane()?.pane_id(), - unsafe { libc::getpid() } - )) - }); - methods.add_method("pane_id", |_, this, _: ()| Ok(this.pane()?.pane_id())); - methods.add_method("mux_pane", |_, this, _: ()| { - Ok(mux_lua::MuxPane(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("get_metadata", |lua, this, _: ()| { - let value = this.pane()?.get_metadata(); - dynamic_to_lua_value(lua, value) - }); - methods.add_method("get_foreground_process_name", |_, this, _: ()| { - Ok(this.pane()?.get_foreground_process_name()) - }); - methods.add_method("get_foreground_process_info", |_, this, _: ()| { - Ok(this.pane()?.get_foreground_process_info()) - }); - 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()?.get_cursor_position()) - }); - methods.add_method("get_dimensions", |_, this, _: ()| { - Ok(this.pane()?.get_dimensions()) - }); - methods.add_method("get_user_vars", |_, this, _: ()| { - Ok(this.pane()?.copy_user_vars()) - }); - methods.add_method("has_unseen_output", |_, this, _: ()| { - Ok(this.pane()?.has_unseen_output()) - }); - methods.add_method("is_alt_screen_active", |_, this, _: ()| { - Ok(this.pane()?.is_alt_screen_active()) - }); - - // When called with no arguments, returns the lines from the - // viewport as plain text (no escape sequences). - // When called with an optional integer argument, returns the - // last nlines lines of the terminal output. - // The returned string will have trailing whitespace trimmed. - methods.add_method("get_lines_as_text", |_, this, nlines: Option| { - let pane = this.pane()?; - let dims = pane.get_dimensions(); - let nlines = nlines.unwrap_or(dims.viewport_rows); - let bottom_row = dims.physical_top + dims.viewport_rows as isize; - let top_row = bottom_row.saturating_sub(nlines as isize); - let (_first_row, lines) = pane.get_lines(top_row..bottom_row); - let mut text = String::new(); - for line in lines { - for cell in line.visible_cells() { - text.push_str(cell.str()); - } - let trimmed = text.trim_end().len(); - text.truncate(trimmed); - text.push('\n'); - } - let trimmed = text.trim_end().len(); - text.truncate(trimmed); - Ok(text) - }); - - methods.add_method( - "get_logical_lines_as_text", - |_, this, nlines: Option| { - let pane = this.pane()?; - let dims = pane.get_dimensions(); - let nlines = nlines.unwrap_or(dims.viewport_rows); - let bottom_row = dims.physical_top + dims.viewport_rows as isize; - let top_row = bottom_row.saturating_sub(nlines as isize); - let lines = pane.get_logical_lines(top_row..bottom_row); - let mut text = String::new(); - for line in lines { - for cell in line.logical.visible_cells() { - text.push_str(cell.str()); - } - let trimmed = text.trim_end().len(); - text.truncate(trimmed); - text.push('\n'); - } - let trimmed = text.trim_end().len(); - text.truncate(trimmed); - Ok(text) - }, - ); - - methods.add_method("get_domain_name", |_, this, _: ()| { - let pane = this.pane()?; - let mut name = None; - if let Some(mux) = Mux::get() { - let domain_id = pane.domain_id(); - name = mux - .get_domain(domain_id) - .map(|dom| dom.domain_name().to_string()); - } - match name { - Some(name) => Ok(name), - None => Ok("".to_string()), - } - }); - } -} diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 548070fae..5df9d0ae0 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -12,7 +12,6 @@ use crate::overlay::{ QuickSelectOverlay, }; use crate::scripting::guiwin::GuiWin; -use crate::scripting::pane::PaneObject; use crate::scrollbar::*; use crate::selection::Selection; use crate::shapecache::*; @@ -46,6 +45,7 @@ use mux::tab::{ }; use mux::window::WindowId as MuxWindowId; use mux::{Mux, MuxNotification}; +use mux_lua::MuxPane; use smol::channel::Sender; use smol::Timer; use std::cell::{RefCell, RefMut}; @@ -1333,14 +1333,14 @@ impl TermWindow { None => return, }, }; - let pane = PaneObject::new(&pane); + let pane = MuxPane(pane.pane_id()); let name = name.to_string(); async fn do_event( lua: Option>, name: String, window: GuiWin, - pane: PaneObject, + pane: MuxPane, ) -> anyhow::Result<()> { let again = if let Some(lua) = lua { let args = lua.pack_multi((window.clone(), pane))?; @@ -1647,7 +1647,7 @@ impl TermWindow { fn emit_user_var_event(&mut self, pane_id: PaneId, name: String, value: String) { let window = GuiWin::new(self); let pane = match Mux::get().expect("on main thread").get_pane(pane_id) { - Some(pane) => PaneObject::new(&pane), + Some(pane) => mux_lua::MuxPane(pane.pane_id()), None => return, }; @@ -1656,7 +1656,7 @@ impl TermWindow { name: String, value: String, window: GuiWin, - pane: PaneObject, + pane: MuxPane, ) -> anyhow::Result<()> { if let Some(lua) = lua { let args = lua.pack_multi((window.clone(), pane, name, value))?; @@ -2729,12 +2729,12 @@ impl TermWindow { // 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); + let pane = MuxPane(pane.pane_id()); async fn open_uri( lua: Option>, window: GuiWin, - pane: PaneObject, + pane: MuxPane, link: String, ) -> anyhow::Result<()> { let default_click = match lua {