diff --git a/wezterm-gui/src/scripting/guiwin.rs b/wezterm-gui/src/scripting/guiwin.rs index ed354d3f9..60e6c8820 100644 --- a/wezterm-gui/src/scripting/guiwin.rs +++ b/wezterm-gui/src/scripting/guiwin.rs @@ -47,6 +47,14 @@ impl GuiWin { impl UserData for GuiWin { fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) { methods.add_method("window_id", |_, this, _: ()| Ok(this.mux_window_id)); + methods.add_async_method("set_right_status", |_, this, status: String| async move { + this.with_term_window(move |term_window, _ops| { + term_window.right_status = status.clone(); + term_window.update_title(); + Ok(()) + }) + .await + }); methods.add_async_method( "perform_action", |_, this, (assignment, pane): (KeyAssignment, PaneObject)| async move { diff --git a/wezterm-gui/src/tabbar.rs b/wezterm-gui/src/tabbar.rs index 18ce84041..ebc0499a1 100644 --- a/wezterm-gui/src/tabbar.rs +++ b/wezterm-gui/src/tabbar.rs @@ -4,6 +4,9 @@ use std::cell::Ref; use termwiz::cell::unicode_column_width; use termwiz::cell::{Cell, CellAttributes}; use termwiz::color::ColorSpec; +use termwiz::escape::csi::Sgr; +use termwiz::escape::parser::Parser; +use termwiz::escape::{Action, ControlCode, CSI}; use unicode_segmentation::UnicodeSegmentation; use wezterm_term::Line; @@ -49,6 +52,7 @@ impl TabBarState { window: &Ref, colors: Option<&TabBarColors>, config: &ConfigHandle, + right_status: &str, ) -> Self { // We ultimately want to produce a line looking like this: // ` | tab1-title x | tab2-title x | + . - X ` @@ -180,10 +184,19 @@ impl TabBarState { .set_background(ColorSpec::TrueColor(colors.background)) .clone(), ); + for idx in x..title_width { line.set_cell(idx, black_cell.clone()); } + let rhs_cells = parse_status_text(right_status, black_cell.attrs().clone()); + let rhs_len = rhs_cells.len().min(title_width.saturating_sub(x)); + let skip = rhs_cells.len() - rhs_len; + + for (idx, cell) in rhs_cells.into_iter().skip(skip).rev().enumerate() { + line.set_cell(title_width - (1 + idx), cell); + } + Self { line, items } } @@ -197,3 +210,93 @@ impl TabBarState { TabBarItem::None } } + +fn parse_status_text(text: &str, default_cell: CellAttributes) -> Vec { + let mut pen = default_cell.clone(); + let mut cells = vec![]; + let mut ignoring = false; + let mut print_buffer = String::new(); + + fn flush_print(buf: &mut String, cells: &mut Vec, pen: &CellAttributes) { + for g in unicode_segmentation::UnicodeSegmentation::graphemes(buf.as_str(), true) { + cells.push(Cell::new_grapheme(g, pen.clone())); + } + buf.clear(); + } + + let mut parser = Parser::new(); + parser.parse(text.as_bytes(), |action| { + if ignoring { + return; + } + match action { + Action::Print(c) => print_buffer.push(c), + Action::Control(c) => { + flush_print(&mut print_buffer, &mut cells, &pen); + match c { + ControlCode::CarriageReturn | ControlCode::LineFeed => { + ignoring = true; + } + _ => {} + } + } + Action::CSI(csi) => { + flush_print(&mut print_buffer, &mut cells, &pen); + match csi { + CSI::Sgr(sgr) => match sgr { + Sgr::Reset => pen = default_cell.clone(), + Sgr::Intensity(i) => { + pen.set_intensity(i); + } + Sgr::Underline(u) => { + pen.set_underline(u); + } + Sgr::Overline(o) => { + pen.set_overline(o); + } + Sgr::Blink(b) => { + pen.set_blink(b); + } + Sgr::Italic(i) => { + pen.set_italic(i); + } + Sgr::Inverse(inverse) => { + pen.set_reverse(inverse); + } + Sgr::Invisible(invis) => { + pen.set_invisible(invis); + } + Sgr::StrikeThrough(strike) => { + pen.set_strikethrough(strike); + } + Sgr::Foreground(col) => { + pen.set_foreground(col); + } + Sgr::Background(col) => { + pen.set_background(col); + } + Sgr::UnderlineColor(col) => { + pen.set_underline_color(col); + } + Sgr::Font(_) => {} + }, + _ => {} + } + } + Action::Esc(_) => { + flush_print(&mut print_buffer, &mut cells, &pen); + } + Action::Sixel(_) => { + flush_print(&mut print_buffer, &mut cells, &pen); + } + Action::DeviceControl(_) => { + flush_print(&mut print_buffer, &mut cells, &pen); + } + Action::OperatingSystemCommand(_) => { + flush_print(&mut print_buffer, &mut cells, &pen); + } + } + }); + flush_print(&mut print_buffer, &mut cells, &pen); + cells +} diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index eb4416116..0330b84ed 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -109,6 +109,7 @@ pub struct TermWindow { show_tab_bar: bool, show_scroll_bar: bool, tab_bar: TabBarState, + pub right_status: String, last_mouse_coords: (usize, i64), last_mouse_terminal_coords: (usize, StableRowIndex), scroll_drag_start: Option, @@ -253,6 +254,7 @@ impl WindowCallbacks for TermWindow { show_tab_bar: self.show_tab_bar, show_scroll_bar: self.show_scroll_bar, tab_bar: self.tab_bar.clone(), + right_status: self.right_status.clone(), last_mouse_coords: self.last_mouse_coords.clone(), last_mouse_terminal_coords: self.last_mouse_terminal_coords.clone(), scroll_drag_start: self.scroll_drag_start.clone(), @@ -465,6 +467,7 @@ impl TermWindow { show_tab_bar, show_scroll_bar: config.enable_scroll_bar, tab_bar: TabBarState::default(), + right_status: String::new(), last_mouse_coords: (0, -1), last_mouse_terminal_coords: (0, 0), scroll_drag_start: None, @@ -503,17 +506,63 @@ impl TermWindow { } fn start_periodic_maintenance(window: Window) { - Connection::get().unwrap().schedule_timer( - std::time::Duration::from_millis(35), - move || { + Connection::get() + .unwrap() + .schedule_timer(std::time::Duration::from_millis(35), { + let window = window.clone(); + move || { + window.apply(move |myself, window| { + if let Some(myself) = myself.downcast_mut::() { + myself.periodic_window_maintenance(window)?; + } + Ok(()) + }); + } + }); + Connection::get() + .unwrap() + .schedule_timer(std::time::Duration::from_secs(1), move || { window.apply(move |myself, window| { if let Some(myself) = myself.downcast_mut::() { - myself.periodic_window_maintenance(window)?; + myself.emit_status_event(window)?; } Ok(()) }); - }, - ); + }); + } + + fn emit_status_event(&mut self, _window: &dyn WindowOps) -> anyhow::Result<()> { + let pane = match self.get_active_pane_or_overlay() { + Some(pane) => pane, + None => return Ok(()), + }; + + let window = GuiWin::new(self); + let pane = PaneObject::new(&pane); + + async fn do_status( + lua: Option>, + window: GuiWin, + pane: PaneObject, + ) -> anyhow::Result<()> { + if let Some(lua) = lua { + let args = lua.pack_multi((window, pane))?; + + config::lua::emit_event(&lua, ("update-right-status".to_string(), args)) + .await + .map_err(|e| { + log::error!("while processing update-right-status event: {:#}", e); + e + })?; + } + Ok(()) + } + + promise::spawn::spawn(config::with_lua_config_on_main_thread(move |lua| { + do_status(lua, window, pane) + })) + .detach(); + Ok(()) } fn periodic_window_maintenance(&mut self, _window: &dyn WindowOps) -> anyhow::Result<()> { @@ -694,7 +743,7 @@ impl TermWindow { } } - fn update_title(&mut self) { + pub fn update_title(&mut self) { let mux = Mux::get().unwrap(); let window = match mux.get_window(self.mux_window_id) { Some(window) => window, @@ -711,6 +760,7 @@ impl TermWindow { &window, self.config.colors.as_ref().and_then(|c| c.tab_bar.as_ref()), &self.config, + &self.right_status, ); if new_tab_bar != self.tab_bar { self.tab_bar = new_tab_bar;