Skip to content

Commit

Permalink
make true color support bool a static
Browse files Browse the repository at this point in the history
paves the way for a Default implementation for Theme
  • Loading branch information
gibbz00 committed Feb 16, 2023
1 parent 9a2257b commit fd3a5ea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
7 changes: 4 additions & 3 deletions helix-term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ async fn main_impl() -> Result<i32> {
}
}
};
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 <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)
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
Expand Down
7 changes: 3 additions & 4 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::default(true),
Theme::default(),
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::default(true), syn_conf)?;
let app = Application::new(args, config, Theme::default(), syn_conf)?;

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

pub fn build(self) -> anyhow::Result<Application> {
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);
Expand Down
40 changes: 22 additions & 18 deletions helix-view/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ pub static BASE16_DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| Theme {
..Theme::from(BASE16_DEFAULT_THEME_DATA.clone())
});

static TRUE_COLOR_SUPPORT: once_cell::sync::OnceCell<bool> = 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<String, Style>,
// tree-sitter highlight styles are stored in a Vec to optimize lookups
Expand All @@ -47,32 +48,35 @@ pub struct Theme {
}

impl Theme {
pub fn new(theme_name: &str, true_color_support: bool) -> Result<Theme> {
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<Theme> {
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<Theme> {
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()
}
}

Expand Down

0 comments on commit fd3a5ea

Please sign in to comment.