From fd3a5ead148e7dff810ae529489cb166989a6803 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Thu, 16 Feb 2023 17:15:38 +0100 Subject: [PATCH] make true color support bool a static paves the way for a Default implementation for Theme --- helix-term/src/main.rs | 7 +++--- helix-term/tests/test/helpers.rs | 7 +++--- helix-view/src/theme.rs | 40 ++++++++++++++++++-------------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index e776557ed3728..231d3efee4f10 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -83,14 +83,15 @@ async fn main_impl() -> Result { } } }; + Theme::set_true_color_support(true_color_support); let theme: Theme = match config.theme.as_deref() { - Some(theme_name) => Theme::new(theme_name, true_color_support).unwrap_or_else(|err| { + Some(theme_name) => Theme::new(theme_name).unwrap_or_else(|err| { eprintln!("Bad theme config: {}", err); eprintln!("Press to continue with default theme config"); let _wait_for_enter = std::io::Read::read(&mut std::io::stdin(), &mut []); - Theme::default(true_color_support) + Theme::default() }), - None => Theme::default(true_color_support), + None => Theme::default(), }; // TODO: use the thread local executor to spawn the application task separately from the work pool diff --git a/helix-term/tests/test/helpers.rs b/helix-term/tests/test/helpers.rs index 3aa2778cdf9c4..083d696b5d1bd 100644 --- a/helix-term/tests/test/helpers.rs +++ b/helix-term/tests/test/helpers.rs @@ -123,7 +123,7 @@ pub async fn test_key_sequence_with_input_text>( None => Application::new( Args::default(), test_config(), - Theme::default(true), + Theme::default(), test_syntax_conf(None), )?, }; @@ -178,7 +178,7 @@ pub async fn test_with_config>( test_case: T, ) -> anyhow::Result<()> { let test_case = test_case.into(); - let app = Application::new(args, config, Theme::default(true), syn_conf)?; + let app = Application::new(args, config, Theme::default(), syn_conf)?; test_key_sequence_with_input_text( Some(app), @@ -309,8 +309,7 @@ impl AppBuilder { } pub fn build(self) -> anyhow::Result { - let mut app = - Application::new(self.args, self.config, Theme::default(true), self.syn_conf)?; + let mut app = Application::new(self.args, self.config, Theme::default(), self.syn_conf)?; if let Some((text, selection)) = self.input { let (view, doc) = helix_view::current!(app.editor); diff --git a/helix-view/src/theme.rs b/helix-view/src/theme.rs index 7dbde57167795..1251ef85f3b65 100644 --- a/helix-view/src/theme.rs +++ b/helix-view/src/theme.rs @@ -35,10 +35,11 @@ pub static BASE16_DEFAULT_THEME: Lazy = Lazy::new(|| Theme { ..Theme::from(BASE16_DEFAULT_THEME_DATA.clone()) }); +static TRUE_COLOR_SUPPORT: once_cell::sync::OnceCell = once_cell::sync::OnceCell::new(); + #[derive(Clone, Debug, Default)] pub struct Theme { name: String, - true_color_support: bool, // UI styles are stored in a HashMap styles: HashMap, // tree-sitter highlight styles are stored in a Vec to optimize lookups @@ -47,32 +48,35 @@ pub struct Theme { } impl Theme { - pub fn new(theme_name: &str, true_color_support: bool) -> Result { + pub fn set_true_color_support(true_color_support: bool) { + TRUE_COLOR_SUPPORT + .set(true_color_support) + .expect("method should only be called once on program startup."); + } + + fn get_true_color_support() -> bool { + *TRUE_COLOR_SUPPORT + .get() + .expect("true color support should have been set on program startup.") + } + + pub fn new(theme_name: &str) -> Result { let theme = Self::load(theme_name)?; - if !true_color_support && !theme.is_16_color() { + if !Self::get_true_color_support() && !theme.is_16_color() { anyhow::bail!("Unsupported theme: theme requires true color support") } - Ok(Self { - true_color_support, - ..theme - }) + Ok(theme) } pub fn update(&self, theme_name: &str) -> Result { - Self::new(theme_name, self.true_color_support) + Self::new(theme_name) } - pub fn default(true_color_support: bool) -> Theme { - if true_color_support { - Self { - true_color_support, - ..DEFAULT_THEME.clone() - } + pub fn default() -> Theme { + if Self::get_true_color_support() { + DEFAULT_THEME.clone() } else { - Self { - true_color_support, - ..BASE16_DEFAULT_THEME.clone() - } + BASE16_DEFAULT_THEME.clone() } }