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, } }; }