From cf983127a08ff5b6c798d491940e4f6e9bd40944 Mon Sep 17 00:00:00 2001 From: gibbz00 Date: Thu, 16 Feb 2023 21:06:34 +0100 Subject: [PATCH] Remove Option wrapper for Config.theme Does so by assuming empty string implyies default theme in Theme::new. Paves the way to make Config::default() to be derivable. --- helix-term/src/application.rs | 5 ++--- helix-term/src/config.rs | 5 +++-- helix-term/src/main.rs | 5 +---- helix-view/src/theme.rs | 12 ++++++++---- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 4d1a453ed6130..4d8bc951e5279 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -355,9 +355,8 @@ impl Application { document.detect_language(self.editor.lang_configs_loader.clone()); } - if let Some(theme_name) = &self.config.load().theme { - self.editor.set_theme(Theme::new(theme_name)?); - } + self.editor + .set_theme(Theme::new(&self.config.load().theme)?); Ok(()) }; diff --git a/helix-term/src/config.rs b/helix-term/src/config.rs index d024d0065ffaa..30093c627b526 100644 --- a/helix-term/src/config.rs +++ b/helix-term/src/config.rs @@ -7,7 +7,8 @@ use std::collections::HashMap; #[derive(Debug, Clone, PartialEq, Deserialize)] #[serde(deny_unknown_fields)] pub struct Config { - pub theme: Option, + #[serde(default)] + pub theme: String, #[serde(default = "default::default")] pub keys: HashMap, #[serde(default)] @@ -43,7 +44,7 @@ impl Config { impl Default for Config { fn default() -> Config { Config { - theme: None, + theme: String::default(), keys: default::default(), editor: helix_view::editor::EditorConfig::default(), } diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index e507e43774cf6..ea673d35f2dea 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -64,10 +64,7 @@ async fn main_impl() -> Result { .map(|v| matches!(v.as_str(), "truecolor" | "24bit")) .unwrap_or(false), ); - let theme: Theme = match config.theme.as_deref() { - Some(theme_name) => check_config_load(Theme::new(theme_name), None, "theme"), - None => Theme::default(), - }; + let theme = check_config_load(Theme::new(&config.theme), None, "theme"); // TODO: use the thread local executor to spawn the application task separately from the work pool let mut app = Application::new(args, config, theme, language_configurations) diff --git a/helix-view/src/theme.rs b/helix-view/src/theme.rs index cc395b736e891..b270895aa6f51 100644 --- a/helix-view/src/theme.rs +++ b/helix-view/src/theme.rs @@ -61,11 +61,15 @@ impl Theme { } pub fn new(theme_name: &str) -> Result { - let theme = Self::load(theme_name)?; - if !Self::get_true_color_support() && !theme.is_16_color() { - anyhow::bail!("Unsupported theme: theme requires true color support") + if theme_name.is_empty() { + Ok(Self::default()) + } else { + let theme = Self::load(theme_name)?; + if !Self::get_true_color_support() && !theme.is_16_color() { + anyhow::bail!("Unsupported theme: true color support is required") + } + Ok(theme) } - Ok(theme) } /// Recursively load a theme, merging with any inherited parent themes.