From aac93e7f0a2ae8d1cc2dcea87adece6acf2faf81 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 23 Dec 2019 14:19:48 -0800 Subject: [PATCH] Improve palette handling w/ live config reloading @fanzeyi ran into part of this issue when using multiplexing; tabs from a remote domain always used the default color scheme rather than the local configuration file color scheme. Another variation on this is that making changes to the `[colors]` section of the config only impacts tabs created after the change was loaded, rather than updating the running tabs. The root of this was that each terminal was keeping its own copy of the palette that was updated at creation time to reflect the config file. Relatively recently I introduced a new trait to allow the terminal state to reach out and obtain configuration from another source. This commit connects the terminal configuration layer to the configuration file directly. The color schemes are now pulled directly from the configuration file, except in the case where a dynamic color scheme has been applied: using escape sequences to change the colors causes the palette to be forked away from the configuration file. There's still a low pri TODO around this space: if we're connected to a remote domain and someone uses dynamic color scheme escapes, should we reflect those locally? If so then we'll need to build some plumbing to transport the palette to/from the remote system. Closes: https://github.com/wez/wezterm/pull/60 --- src/config/terminal.rs | 10 ++++++++++ src/mux/domain.rs | 7 +------ src/server/tab.rs | 8 +++++++- src/ssh.rs | 8 +------- term/src/config.rs | 3 +++ term/src/terminalstate.rs | 34 +++++++++++++++++++++++++--------- 6 files changed, 47 insertions(+), 23 deletions(-) diff --git a/src/config/terminal.rs b/src/config/terminal.rs index b55259b29..71953d37d 100644 --- a/src/config/terminal.rs +++ b/src/config/terminal.rs @@ -1,6 +1,7 @@ //! Bridge our gui config into the terminal crate configuration use crate::config::configuration; +use term::color::ColorPalette; use termwiz::hyperlink::Rule as HyperlinkRule; #[derive(Debug)] @@ -19,4 +20,13 @@ impl term::TerminalConfiguration for TermConfig { let config = configuration(); (config.generation(), config.hyperlink_rules.clone()) } + + fn color_palette(&self) -> ColorPalette { + configuration() + .colors + .as_ref() + .cloned() + .map(Into::into) + .unwrap_or_else(ColorPalette::default) + } } diff --git a/src/mux/domain.rs b/src/mux/domain.rs index 95f37bab8..6de37b8e9 100644 --- a/src/mux/domain.rs +++ b/src/mux/domain.rs @@ -95,7 +95,7 @@ impl Domain for LocalDomain { let child = pair.slave.spawn_command(cmd)?; info!("spawned: {:?}", child); - let mut terminal = term::Terminal::new( + let terminal = term::Terminal::new( size.rows as usize, size.cols as usize, size.pixel_width as usize, @@ -104,11 +104,6 @@ impl Domain for LocalDomain { ); let mux = Mux::get().unwrap(); - - if let Some(palette) = config.colors.as_ref() { - *terminal.palette_mut() = palette.clone().into(); - } - let tab: Rc = Rc::new(LocalTab::new(terminal, child, pair.master, self.id)); mux.add_tab(&tab)?; diff --git a/src/server/tab.rs b/src/server/tab.rs index 9dde3f940..1047f345a 100644 --- a/src/server/tab.rs +++ b/src/server/tab.rs @@ -1,3 +1,4 @@ +use crate::config::configuration; use crate::frontend::executor; use crate::mux::domain::DomainId; use crate::mux::renderable::Renderable; @@ -301,7 +302,12 @@ impl Tab for ClientTab { } fn palette(&self) -> ColorPalette { - Default::default() + configuration() + .colors + .as_ref() + .cloned() + .map(Into::into) + .unwrap_or_else(ColorPalette::default) } fn domain_id(&self) -> DomainId { diff --git a/src/ssh.rs b/src/ssh.rs index 8f7b7ae78..10d795426 100644 --- a/src/ssh.rs +++ b/src/ssh.rs @@ -1,4 +1,3 @@ -use crate::config::configuration; use crate::localtab::LocalTab; use crate::mux::domain::{alloc_domain_id, Domain, DomainId, DomainState}; use crate::mux::tab::Tab; @@ -339,7 +338,7 @@ impl Domain for RemoteSshDomain { let child = pair.slave.spawn_command(cmd)?; log::info!("spawned: {:?}", child); - let mut terminal = term::Terminal::new( + let terminal = term::Terminal::new( size.rows as usize, size.cols as usize, size.pixel_width as usize, @@ -348,11 +347,6 @@ impl Domain for RemoteSshDomain { ); let mux = Mux::get().unwrap(); - - if let Some(palette) = configuration().colors.as_ref() { - *terminal.palette_mut() = palette.clone().into(); - } - let tab: Rc = Rc::new(LocalTab::new(terminal, child, pair.master, self.id)); mux.add_tab(&tab)?; diff --git a/term/src/config.rs b/term/src/config.rs index c97fa211b..0607a5b7a 100644 --- a/term/src/config.rs +++ b/term/src/config.rs @@ -1,3 +1,4 @@ +use crate::color::ColorPalette; use termwiz::hyperlink::Rule as HyperlinkRule; pub trait TerminalConfiguration: std::fmt::Debug { @@ -35,4 +36,6 @@ pub trait TerminalConfiguration: std::fmt::Debug { fn hyperlink_rules(&self) -> (usize, Vec) { (self.generation(), vec![]) } + + fn color_palette(&self) -> ColorPalette; } diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index b2ea1e8a0..efafff2da 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -215,7 +215,7 @@ pub struct TerminalState { /// The terminal title string title: String, - palette: ColorPalette, + palette: Option, pixel_width: usize, pixel_height: usize, @@ -295,7 +295,7 @@ impl TerminalState { hyperlink_rules, hyperlink_rules_generation, title: "wezterm".to_string(), - palette: ColorPalette::default(), + palette: None, pixel_height, pixel_width, clipboard: None, @@ -310,12 +310,28 @@ impl TerminalState { &self.title } - pub fn palette(&self) -> &ColorPalette { - &self.palette + /// Returns a copy of the palette. + /// By default we don't keep a copy in the terminal state, + /// preferring to take the config values from the users + /// config file and updating to changes live. + /// However, if they have used dynamic color scheme escape + /// sequences we'll fork a copy of the palette at that time + /// so that we can start tracking those changes. + pub fn palette(&self) -> ColorPalette { + self.palette + .as_ref() + .cloned() + .unwrap_or_else(|| self.config.color_palette()) } + /// Called in response to dynamic color scheme escape sequences. + /// Will make a copy of the palette from the config file if this + /// is the first of these escapes we've seen. pub fn palette_mut(&mut self) -> &mut ColorPalette { - &mut self.palette + if self.palette.is_none() { + self.palette.replace(self.config.color_palette()); + } + self.palette.as_mut().unwrap() } pub fn screen(&self) -> &Screen { @@ -2254,13 +2270,13 @@ impl<'a> Performer<'a> { OperatingSystemCommand::ChangeColorNumber(vec![ChangeColorPair { palette_index: pair.palette_index, color: ColorOrQuery::Color( - self.palette.colors.0[pair.palette_index as usize], + self.palette().colors.0[pair.palette_index as usize], ), }]); write!(self.host.writer(), "{}", response).ok(); } ColorOrQuery::Color(c) => { - self.palette.colors.0[pair.palette_index as usize] = c; + self.palette_mut().colors.0[pair.palette_index as usize] = c; } } } @@ -2279,11 +2295,11 @@ impl<'a> Performer<'a> { ColorOrQuery::Query => { let response = OperatingSystemCommand::ChangeDynamicColors( which_color, - vec![ColorOrQuery::Color(self.palette.$name)], + vec![ColorOrQuery::Color(self.palette().$name)], ); write!(self.host.writer(), "{}", response).ok(); } - ColorOrQuery::Color(c) => self.palette.$name = c, + ColorOrQuery::Color(c) => self.palette_mut().$name = c, } }; }