Skip to content

Commit

Permalink
add Theme::default
Browse files Browse the repository at this point in the history
A step in making it more in line to how the other configs are loaded.
  • Loading branch information
gibbz00 committed Feb 16, 2023
1 parent 40a4e3d commit 9a2257b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
16 changes: 9 additions & 7 deletions helix-term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ async fn main_impl() -> Result<i32> {
}
}
};

let theme = Theme::new(config.theme.as_deref(), true_color_support).unwrap_or_else(|err| {
eprintln!("Bad theme config: {}", err);
eprintln!("Press <ENTER> to continue with default theme config");
let _wait_for_enter = std::io::Read::read(&mut std::io::stdin(), &mut []);
Theme::new(None, true_color_support).expect("default themes must be correct")
});
let theme: Theme = match config.theme.as_deref() {
Some(theme_name) => Theme::new(theme_name, true_color_support).unwrap_or_else(|err| {
eprintln!("Bad theme config: {}", err);
eprintln!("Press <ENTER> to continue with default theme config");
let _wait_for_enter = std::io::Read::read(&mut std::io::stdin(), &mut []);
Theme::default(true_color_support)
}),
None => Theme::default(true_color_support),
};

// 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)
Expand Down
12 changes: 4 additions & 8 deletions helix-term/tests/test/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
None => Application::new(
Args::default(),
test_config(),
Theme::new(None, true)?,
Theme::default(true),
test_syntax_conf(None),
)?,
};
Expand Down Expand Up @@ -178,7 +178,7 @@ pub async fn test_with_config<T: Into<TestCase>>(
test_case: T,
) -> anyhow::Result<()> {
let test_case = test_case.into();
let app = Application::new(args, config, Theme::new(None, true)?, syn_conf)?;
let app = Application::new(args, config, Theme::default(true), syn_conf)?;

test_key_sequence_with_input_text(
Some(app),
Expand Down Expand Up @@ -309,12 +309,8 @@ impl AppBuilder {
}

pub fn build(self) -> anyhow::Result<Application> {
let mut app = Application::new(
self.args,
self.config,
Theme::new(None, true)?,
self.syn_conf,
)?;
let mut app =
Application::new(self.args, self.config, Theme::default(true), self.syn_conf)?;

if let Some((text, selection)) = self.input {
let (view, doc) = helix_view::current!(app.editor);
Expand Down
40 changes: 21 additions & 19 deletions helix-view/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,35 @@ pub struct Theme {
}

impl Theme {
pub fn new(theme_name: Option<&str>, true_color_support: bool) -> Result<Theme> {
if let Some(theme_name) = theme_name {
let theme = Self::load(theme_name)?;
if !true_color_support && !theme.is_16_color() {
anyhow::bail!("Unsupported theme: theme requires true color support")
}
Ok(Self {
true_color_support,
..theme
})
} else if true_color_support {
Ok(Self {
pub fn new(theme_name: &str, true_color_support: bool) -> Result<Theme> {
let theme = Self::load(theme_name)?;
if !true_color_support && !theme.is_16_color() {
anyhow::bail!("Unsupported theme: theme requires true color support")
}
Ok(Self {
true_color_support,
..theme
})
}

pub fn update(&self, theme_name: &str) -> Result<Theme> {
Self::new(theme_name, self.true_color_support)
}

pub fn default(true_color_support: bool) -> Theme {
if true_color_support {
Self {
true_color_support,
..DEFAULT_THEME.clone()
})
}
} else {
Ok(Self {
Self {
true_color_support,
..BASE16_DEFAULT_THEME.clone()
})
}
}
}

pub fn update(&self, theme_name: &str) -> Result<Theme> {
Self::new(Some(theme_name), self.true_color_support)
}

/// Recursively load a theme, merging with any inherited parent themes.
///
/// The paths that have been visited in the inheritance hierarchy are tracked
Expand Down

0 comments on commit 9a2257b

Please sign in to comment.