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
This commit is contained in:
Wez Furlong committed 2019-12-23 14:19:48 -08:00
1 parent 1ff76e1c3c
commit aac93e7f0a
6 files changed
+47 -23

No files matched your search

+10
View File
@@ -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)
}
}
+1 -6
View File
@@ -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<dyn Tab> = Rc::new(LocalTab::new(terminal, child, pair.master, self.id));
mux.add_tab(&tab)?;
+7 -1
View File
@@ -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 {
+1 -7
View File
@@ -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<dyn Tab> = Rc::new(LocalTab::new(terminal, child, pair.master, self.id));
mux.add_tab(&tab)?;
+3
View File
@@ -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<HyperlinkRule>) {
(self.generation(), vec![])
}
fn color_palette(&self) -> ColorPalette;
}
+25 -9
View File
@@ -215,7 +215,7 @@ pub struct TerminalState {
/// The terminal title string
title: String,
palette: ColorPalette,
palette: Option<ColorPalette>,
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,
}
};
}