From 1cb2856223f0055cd30b9ff92f4ae9aed50243b3 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 25 Apr 2021 13:53:06 -0700 Subject: [PATCH] add hover parameter to format-tab-title refs: https://github.com/wez/wezterm/issues/647 --- .../lua/window-events/format-tab-title.md | 3 +- wezterm-gui/src/tabbar.rs | 109 +++++++++++------- 2 files changed, 68 insertions(+), 44 deletions(-) diff --git a/docs/config/lua/window-events/format-tab-title.md b/docs/config/lua/window-events/format-tab-title.md index 05a925ee9..beaf06277 100644 --- a/docs/config/lua/window-events/format-tab-title.md +++ b/docs/config/lua/window-events/format-tab-title.md @@ -21,7 +21,7 @@ but it demonstrates that it is possible to format more than just the text shown in the tab. ```lua -wezterm.on("format-tab-title", function(tab, tabs, panes, config) +wezterm.on("format-tab-title", function(tab, tabs, panes, config, hover) if tab.is_active then return { {Background={Color="blue"}}, @@ -38,6 +38,7 @@ The parameters to the event are: * `tabs` - an array containing [TabInformation](../TabInformation.md) for each of the tabs in the window * `panes` - an array containing [PaneInformation](../PaneInformation.md) for each of the panes in the active tab * `config` - the effective configuration for the window +* `hover` - true if the current tab is in the hover state The return value of the event can be: diff --git a/wezterm-gui/src/tabbar.rs b/wezterm-gui/src/tabbar.rs index 4ea9fa5f8..835371cad 100644 --- a/wezterm-gui/src/tabbar.rs +++ b/wezterm-gui/src/tabbar.rs @@ -31,6 +31,57 @@ struct TabEntry { width: usize, } +enum TitleText { + String(String), + Formatted { items: Vec, len: usize }, +} + +fn call_format_tab_title( + tab: &TabInformation, + tab_info: &[TabInformation], + pane_info: &[PaneInformation], + config: &ConfigHandle, + hover: bool, +) -> Option { + match config::run_immediate_with_lua_config(|lua| { + if let Some(lua) = lua { + let tabs = lua.create_sequence_from(tab_info.iter().cloned())?; + let panes = lua.create_sequence_from(pane_info.iter().cloned())?; + + let v = config::lua::emit_sync_callback( + &*lua, + ( + "format-tab-title".to_string(), + (tab.clone(), tabs, panes, (**config).clone(), hover), + ), + )?; + match &v { + mlua::Value::Nil => Ok(None), + mlua::Value::Table(_) => { + let items = >::from_lua(v, &*lua)?; + + let esc = format_as_escapes(items.clone())?; + let cells = parse_status_text(&esc, CellAttributes::default()); + + Ok(Some(TitleText::Formatted { + items, + len: cells.len(), + })) + } + _ => Ok(Some(TitleText::String(String::from_lua(v, &*lua)?))), + } + } else { + Ok(None) + } + }) { + Ok(s) => s, + Err(err) => { + log::warn!("format-tab-title: {}", err); + None + } + } +} + impl TabBarState { pub fn default() -> Self { Self { @@ -112,55 +163,13 @@ impl TabBarState { let mut active_tab_no = 0; - enum TitleText { - String(String), - Formatted { items: Vec, len: usize }, - } - let tab_titles: Vec = tab_info .iter() .map(|tab| { if tab.is_active { active_tab_no = tab.tab_index; } - - let title = match config::run_immediate_with_lua_config(|lua| { - if let Some(lua) = lua { - let tabs = lua.create_sequence_from(tab_info.iter().cloned())?; - let panes = lua.create_sequence_from(pane_info.iter().cloned())?; - - let v = config::lua::emit_sync_callback( - &*lua, - ( - "format-tab-title".to_string(), - (tab.clone(), tabs, panes, (**config).clone()), - ), - )?; - match &v { - mlua::Value::Nil => Ok(None), - mlua::Value::Table(_) => { - let items = >::from_lua(v, &*lua)?; - - let esc = format_as_escapes(items.clone())?; - let cells = parse_status_text(&esc, CellAttributes::default()); - - Ok(Some(TitleText::Formatted { - items, - len: cells.len(), - })) - } - _ => Ok(Some(TitleText::String(String::from_lua(v, &*lua)?))), - } - } else { - Ok(None) - } - }) { - Ok(s) => s, - Err(err) => { - log::warn!("format-tab-title: {}", err); - None - } - }; + let title = call_format_tab_title(tab, tab_info, pane_info, config, false); match title { Some(title) => title, @@ -263,6 +272,20 @@ impl TabBarState { x += 1; } + let hover_title; + let tab_title = if hover { + match call_format_tab_title(&tab_info[tab_idx], tab_info, pane_info, config, hover) + { + Some(t) => { + hover_title = t; + &hover_title + } + None => tab_title, + } + } else { + tab_title + }; + match tab_title { TitleText::String(tab_title) => { for (idx, sub) in tab_title.graphemes(true).enumerate() {