From 60be1a98a0ab3ea29656b6f8f9be293e1999d60c Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 27 Feb 2021 12:37:04 -0800 Subject: [PATCH] lua: add window::effective_config() method Expose the current window configuration to lua scripts. refs: https://github.com/wez/wezterm/issues/469 --- config/src/font.rs | 8 +++---- config/src/lib.rs | 5 ++-- docs/config/lua/window/effective_config.md | 27 ++++++++++++++++++++++ termwiz/src/hyperlink.rs | 21 ++++++++++++++--- wezterm-gui/src/gui/termwindow.rs | 2 +- wezterm-gui/src/scripting/guiwin.rs | 4 ++++ 6 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 docs/config/lua/window/effective_config.md diff --git a/config/src/font.rs b/config/src/font.rs index 70a8654fc..d273d190c 100644 --- a/config/src/font.rs +++ b/config/src/font.rs @@ -344,7 +344,7 @@ pub struct StyleRule { } impl_lua_conversion!(StyleRule); -#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)] pub enum AllowSquareGlyphOverflow { Never, Always, @@ -357,7 +357,7 @@ impl Default for AllowSquareGlyphOverflow { } } -#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)] pub enum FontLocatorSelection { /// Use fontconfig APIs to resolve fonts (!macos, posix systems) FontConfig, @@ -418,7 +418,7 @@ impl std::str::FromStr for FontLocatorSelection { } } -#[derive(Debug, Deserialize, Clone, Copy)] +#[derive(Debug, Deserialize, Serialize, Clone, Copy)] pub enum FontRasterizerSelection { FreeType, } @@ -463,7 +463,7 @@ impl std::str::FromStr for FontRasterizerSelection { } } -#[derive(Debug, Deserialize, Clone, Copy)] +#[derive(Debug, Deserialize, Serialize, Clone, Copy)] pub enum FontShaperSelection { Allsorts, Harfbuzz, diff --git a/config/src/lib.rs b/config/src/lib.rs index e1a8ef064..ec47e6994 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -576,7 +576,7 @@ where } /// Behavior when the program spawned by wezterm terminates -#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)] pub enum ExitBehavior { /// Close the associated pane Close, @@ -592,7 +592,7 @@ impl Default for ExitBehavior { } } -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct Config { /// The font size, measured in points #[serde(default = "default_font_size", deserialize_with = "de_number")] @@ -1005,6 +1005,7 @@ pub struct Config { #[serde(default = "default_alternate_buffer_wheel_scroll_speed")] pub alternate_buffer_wheel_scroll_speed: u8, } +impl_lua_conversion!(Config); fn default_alternate_buffer_wheel_scroll_speed() -> u8 { 3 diff --git a/docs/config/lua/window/effective_config.md b/docs/config/lua/window/effective_config.md new file mode 100644 index 000000000..ec8556a1d --- /dev/null +++ b/docs/config/lua/window/effective_config.md @@ -0,0 +1,27 @@ +# `window:effective_config()` + +*Since: nightly* + +Returns a lua table representing the effective configuration for the Window. +The table is in the same format as that used to specify the config in +the `wezterm.lua` file, but represents the fully-populated state of the +configuration, including any CLI or per-window configuration overrides. + +Note: changing the config table will NOT change the effective window config; +it is just a copy of that information. + +This example will log the configured font size when `CTRL-SHIFT-E` is pressed: + +```lua +local wezterm = require'wezterm' + +wezterm.on("show-font-size", function(window, pane) + wezterm.log_error(window:effective_config().font_size); +end) + +return { + keys = { + {key="E", mods="CTRL", action=wezterm.action{EmitEvent="show-font-size"}}, + }, +} +``` diff --git a/termwiz/src/hyperlink.rs b/termwiz/src/hyperlink.rs index e57ded0ff..4a73acc63 100644 --- a/termwiz/src/hyperlink.rs +++ b/termwiz/src/hyperlink.rs @@ -7,7 +7,7 @@ use crate::{ensure, format_err, Result}; use regex::{Captures, Regex}; #[cfg(feature = "use_serde")] -use serde::{Deserialize, Deserializer, Serialize}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::collections::HashMap; use std::fmt::{Display, Error as FmtError, Formatter}; use std::ops::Range; @@ -123,13 +123,19 @@ impl Display for Hyperlink { /// URL to view the details for that issue. /// The Rule struct is configuration that is passed to the terminal /// and is evaluated when processing mouse hover events. -#[cfg_attr(feature = "use_serde", derive(Deserialize))] +#[cfg_attr(feature = "use_serde", derive(Deserialize, Serialize))] #[derive(Debug, Clone)] pub struct Rule { /// The compiled regex for the rule. This is used to match /// against a line of text from the screen (typically the line /// over which the mouse is hovering). - #[cfg_attr(feature = "use_serde", serde(deserialize_with = "deserialize_regex"))] + #[cfg_attr( + feature = "use_serde", + serde( + deserialize_with = "deserialize_regex", + serialize_with = "serialize_regex" + ) + )] regex: Regex, /// The format string that defines how to transform the matched /// text into a URL. For example, a format string of `$0` expands @@ -153,6 +159,15 @@ where Regex::new(&s).map_err(|e| serde::de::Error::custom(format!("{:?}", e))) } +#[cfg(feature = "use_serde")] +fn serialize_regex(regex: &Regex, serializer: S) -> std::result::Result +where + S: Serializer, +{ + let s = regex.to_string(); + s.serialize(serializer) +} + /// Holds a resolved rule match. #[derive(Debug, PartialEq)] pub struct RuleMatch { diff --git a/wezterm-gui/src/gui/termwindow.rs b/wezterm-gui/src/gui/termwindow.rs index 461c3e71e..6072d3647 100644 --- a/wezterm-gui/src/gui/termwindow.rs +++ b/wezterm-gui/src/gui/termwindow.rs @@ -221,7 +221,7 @@ pub struct TabState { pub struct TermWindow { pub window: Option, - config: ConfigHandle, + pub config: ConfigHandle, /// When we most recently received keyboard focus focused: Option, fonts: Rc, diff --git a/wezterm-gui/src/scripting/guiwin.rs b/wezterm-gui/src/scripting/guiwin.rs index 456f7cde2..154260362 100644 --- a/wezterm-gui/src/scripting/guiwin.rs +++ b/wezterm-gui/src/scripting/guiwin.rs @@ -55,5 +55,9 @@ impl UserData for GuiWin { .await }, ); + methods.add_async_method("effective_config", |_, this, _: ()| async move { + this.with_term_window(move |term_window, _ops| Ok((*term_window.config).clone())) + .await + }); } }