diff --git a/docs/config/lua/PaneInformation.md b/docs/config/lua/PaneInformation.md index 077a06250..fd1c0417f 100644 --- a/docs/config/lua/PaneInformation.md +++ b/docs/config/lua/PaneInformation.md @@ -18,3 +18,5 @@ The `PaneInformation` struct contains the following fields: * `pixel_width` - the width of the pane in pixels * `pixel_height` - the height of the pane in pixels * `title` - the title of the pane, per [pane:get_title()](pane/get_title.md) at the time the pane information was captured +* `user_vars` - the user variables defined for the pane, per [pane:get_user_vars()](pane/get_user_vars.md) at the time the pane information was captured. + diff --git a/docs/config/lua/pane/get_user_vars.md b/docs/config/lua/pane/get_user_vars.md new file mode 100644 index 000000000..54f70c65b --- /dev/null +++ b/docs/config/lua/pane/get_user_vars.md @@ -0,0 +1,21 @@ +# `pane:get_user_vars()` + +*Since: nightly builds only* + +Returns a table holding the user variables that have been assigned +to this pane. + +User variables are set using an escape sequence defined by iterm2, but +also recognized by wezterm; this example sets the `foo` user variable +to the value `bar`: + +```bash +printf "\033]1337;SetUserVar=%s=%s\007" foo `echo -n bar | base64` +``` + +you're then able to access this in your wezterm config: + +```lua +wezterm.log_info("foo var is " .. pane:get_user_vars().foo) +``` + diff --git a/mux/src/localpane.rs b/mux/src/localpane.rs index 5f006ac46..296885275 100644 --- a/mux/src/localpane.rs +++ b/mux/src/localpane.rs @@ -10,7 +10,7 @@ use config::{configuration, ExitBehavior}; use portable_pty::{Child, MasterPty, PtySize}; use rangeset::RangeSet; use std::cell::{RefCell, RefMut}; -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use std::ops::Range; use std::sync::Arc; use termwiz::escape::DeviceControlMode; @@ -86,6 +86,10 @@ impl Pane for LocalPane { terminal_get_dimensions(&mut self.terminal.borrow_mut()) } + fn copy_user_vars(&self) -> HashMap { + self.terminal.borrow().user_vars().clone() + } + fn kill(&self) { let mut proc = self.process.borrow_mut(); log::debug!( diff --git a/mux/src/pane.rs b/mux/src/pane.rs index 656aad4ea..9c8535407 100644 --- a/mux/src/pane.rs +++ b/mux/src/pane.rs @@ -8,6 +8,7 @@ use portable_pty::PtySize; use rangeset::RangeSet; use serde::{Deserialize, Serialize}; use std::cell::RefMut; +use std::collections::HashMap; use std::ops::Range; use std::sync::{Arc, Mutex}; use termwiz::hyperlink::Rule; @@ -299,6 +300,10 @@ pub trait Pane: Downcast { fn palette(&self) -> ColorPalette; fn domain_id(&self) -> DomainId; + fn copy_user_vars(&self) -> HashMap { + HashMap::new() + } + fn erase_scrollback(&self, _erase_mode: ScrollbackEraseMode) {} /// Called to advise on whether this tab has focus diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index d112a1505..d47f5b04d 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -301,6 +301,8 @@ pub struct TerminalState { writer: Box, image_cache: lru::LruCache<[u8; 32], Arc>, + + user_vars: HashMap, } fn encode_modifiers(mods: KeyModifiers) -> u8 { @@ -489,6 +491,7 @@ impl TerminalState { term_version: term_version.to_string(), writer: Box::new(std::io::BufWriter::new(writer)), image_cache: lru::LruCache::new(16), + user_vars: HashMap::new(), } } @@ -1212,6 +1215,10 @@ impl TerminalState { } } + pub fn user_vars(&self) -> &HashMap { + &self.user_vars + } + /// Sets the cursor position to precisely the x and values provided fn set_cursor_position_absolute(&mut self, x: usize, y: VisibleRowIndex) { let old_y = self.cursor.y; @@ -3300,6 +3307,9 @@ impl<'a> Performer<'a> { } OperatingSystemCommand::ITermProprietary(iterm) => match iterm { ITermProprietary::File(image) => self.set_image(*image), + ITermProprietary::SetUserVar { name, value } => { + self.user_vars.insert(name, value); + } _ => log::warn!("unhandled iterm2: {:?}", iterm), }, diff --git a/wezterm-gui/src/scripting/pane.rs b/wezterm-gui/src/scripting/pane.rs index c4ba45879..57c5d187c 100644 --- a/wezterm-gui/src/scripting/pane.rs +++ b/wezterm-gui/src/scripting/pane.rs @@ -48,6 +48,9 @@ impl UserData for PaneObject { methods.add_method("get_dimensions", |_, this, _: ()| { Ok(this.pane()?.get_dimensions()) }); + methods.add_method("get_user_vars", |_, this, _: ()| { + Ok(this.pane()?.copy_user_vars()) + }); // When called with no arguments, returns the lines from the // viewport as plain text (no escape sequences). diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 8861916c5..e2bb9cb80 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -109,6 +109,7 @@ pub struct PaneInformation { pub pixel_width: usize, pub pixel_height: usize, pub title: String, + pub user_vars: HashMap, } impl_lua_conversion!(PaneInformation); @@ -1857,6 +1858,7 @@ impl TermWindow { pixel_width: pos.pixel_width, pixel_height: pos.pixel_height, title: pos.pane.get_title(), + user_vars: pos.pane.copy_user_vars(), } }