1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-10 06:34:17 +03:00

add user-var-changed event

This commit is contained in:
Wez Furlong 2022-08-27 17:22:02 -07:00
parent ca8d3f3fee
commit 1398c0d4b0
3 changed files with 78 additions and 6 deletions

View File

@ -17,6 +17,7 @@ As features stabilize some brief notes about them will accumulate here.
* [window_frame](config/lua/config/window_frame.md) now supports setting border size and color [#2417](https://github.com/wez/wezterm/issues/2417)
* [CopyMode](copymode.md) now supports selecting and move by semantic zones. [#2346](https://github.com/wez/wezterm/issues/2346)
* [max_fps](config/lua/config/max_fps.md) option to limit maximum frame rate [#2419](https://github.com/wez/wezterm/discussions/2419)
* [`user-var-changed` event](config/lua/window-events/user-var-changed.md) allows triggering lua code in response to user vars being changed
#### Changed

View File

@ -0,0 +1,30 @@
# `user-var-changed`
*Since: nightly builds only*
The `user-var-changed` event is emitted when a *user var* escape sequence is
used to set a user var.
You can use something like the following from your shell:
```bash
printf "\033]1337;SetUserVar=%s=%s\007" foo `echo -n bar | base64`
```
to set the user var named `foo` to the value `bar`.
Then, if you have this in your config:
```lua
local wezterm = require 'wezterm'
wezterm.on('user-var-changed', function(window, pane, name, value)
wezterm.log_info('var', name, value)
end)
return {}
```
your event handler will be called with `name = 'foo'` and `value = 'bar'`.
See also [pane:get_user_vars()](../pane/get_user_vars.md).

View File

@ -1007,14 +1007,19 @@ impl TermWindow {
self.cancel_overlay_for_tab(tab_id, pane_id);
}
TermWindowNotif::MuxNotification(n) => match n {
MuxNotification::Alert {
alert: Alert::SetUserVar { name, value },
pane_id,
} => {
self.emit_user_var_event(pane_id, name, value);
}
MuxNotification::Alert {
alert:
Alert::OutputSinceFocusLost
| Alert::CurrentWorkingDirectoryChanged
| Alert::WindowTitleChanged(_)
| Alert::TabTitleChanged(_)
| Alert::IconTitleChanged(_)
| Alert::SetUserVar { .. },
| Alert::IconTitleChanged(_),
..
} => {
self.update_title();
@ -1231,6 +1236,7 @@ impl TermWindow {
| Alert::WindowTitleChanged(_)
| Alert::TabTitleChanged(_)
| Alert::IconTitleChanged(_)
| Alert::SetUserVar { .. }
| Alert::Bell,
}
| MuxNotification::PaneOutput(pane_id) => {
@ -1266,10 +1272,7 @@ impl TermWindow {
}
}
MuxNotification::Alert {
alert:
Alert::SetUserVar { .. }
| Alert::ToastNotification { .. }
| Alert::PaletteChanged { .. },
alert: Alert::ToastNotification { .. } | Alert::PaletteChanged { .. },
..
}
| MuxNotification::AssignClipboard { .. }
@ -1613,6 +1616,44 @@ impl TermWindow {
self.update_title_impl();
}
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),
None => return,
};
async fn do_event(
lua: Option<Rc<mlua::Lua>>,
name: String,
value: String,
window: GuiWin,
pane: PaneObject,
) -> anyhow::Result<()> {
if let Some(lua) = lua {
let args = lua.pack_multi((window.clone(), pane, name, value))?;
if let Err(err) =
config::lua::emit_event(&lua, ("user-var-changed".to_string(), args)).await
{
log::error!("while processing user-var-changed event: {:#}", err);
}
}
window
.window
.notify(TermWindowNotif::Apply(Box::new(move |term_window| {
term_window.update_title();
})));
Ok(())
}
promise::spawn::spawn(config::with_lua_config_on_main_thread(move |lua| {
do_event(lua, name, value, window, pane)
}))
.detach();
}
/// Called by window:set_right_status after the status has
/// been updated; let's update the bar
pub fn update_title_post_status(&mut self) {