Skip to content

Commit fd3a5ea

Browse files
committed
make true color support bool a static
paves the way for a Default implementation for Theme
1 parent 9a2257b commit fd3a5ea

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

helix-term/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ async fn main_impl() -> Result<i32> {
8383
}
8484
}
8585
};
86+
Theme::set_true_color_support(true_color_support);
8687
let theme: Theme = match config.theme.as_deref() {
87-
Some(theme_name) => Theme::new(theme_name, true_color_support).unwrap_or_else(|err| {
88+
Some(theme_name) => Theme::new(theme_name).unwrap_or_else(|err| {
8889
eprintln!("Bad theme config: {}", err);
8990
eprintln!("Press <ENTER> to continue with default theme config");
9091
let _wait_for_enter = std::io::Read::read(&mut std::io::stdin(), &mut []);
91-
Theme::default(true_color_support)
92+
Theme::default()
9293
}),
93-
None => Theme::default(true_color_support),
94+
None => Theme::default(),
9495
};
9596

9697
// TODO: use the thread local executor to spawn the application task separately from the work pool

helix-term/tests/test/helpers.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub async fn test_key_sequence_with_input_text<T: Into<TestCase>>(
123123
None => Application::new(
124124
Args::default(),
125125
test_config(),
126-
Theme::default(true),
126+
Theme::default(),
127127
test_syntax_conf(None),
128128
)?,
129129
};
@@ -178,7 +178,7 @@ pub async fn test_with_config<T: Into<TestCase>>(
178178
test_case: T,
179179
) -> anyhow::Result<()> {
180180
let test_case = test_case.into();
181-
let app = Application::new(args, config, Theme::default(true), syn_conf)?;
181+
let app = Application::new(args, config, Theme::default(), syn_conf)?;
182182

183183
test_key_sequence_with_input_text(
184184
Some(app),
@@ -309,8 +309,7 @@ impl AppBuilder {
309309
}
310310

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

315314
if let Some((text, selection)) = self.input {
316315
let (view, doc) = helix_view::current!(app.editor);

helix-view/src/theme.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ pub static BASE16_DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| Theme {
3535
..Theme::from(BASE16_DEFAULT_THEME_DATA.clone())
3636
});
3737

38+
static TRUE_COLOR_SUPPORT: once_cell::sync::OnceCell<bool> = once_cell::sync::OnceCell::new();
39+
3840
#[derive(Clone, Debug, Default)]
3941
pub struct Theme {
4042
name: String,
41-
true_color_support: bool,
4243
// UI styles are stored in a HashMap
4344
styles: HashMap<String, Style>,
4445
// tree-sitter highlight styles are stored in a Vec to optimize lookups
@@ -47,32 +48,35 @@ pub struct Theme {
4748
}
4849

4950
impl Theme {
50-
pub fn new(theme_name: &str, true_color_support: bool) -> Result<Theme> {
51+
pub fn set_true_color_support(true_color_support: bool) {
52+
TRUE_COLOR_SUPPORT
53+
.set(true_color_support)
54+
.expect("method should only be called once on program startup.");
55+
}
56+
57+
fn get_true_color_support() -> bool {
58+
*TRUE_COLOR_SUPPORT
59+
.get()
60+
.expect("true color support should have been set on program startup.")
61+
}
62+
63+
pub fn new(theme_name: &str) -> Result<Theme> {
5164
let theme = Self::load(theme_name)?;
52-
if !true_color_support && !theme.is_16_color() {
65+
if !Self::get_true_color_support() && !theme.is_16_color() {
5366
anyhow::bail!("Unsupported theme: theme requires true color support")
5467
}
55-
Ok(Self {
56-
true_color_support,
57-
..theme
58-
})
68+
Ok(theme)
5969
}
6070

6171
pub fn update(&self, theme_name: &str) -> Result<Theme> {
62-
Self::new(theme_name, self.true_color_support)
72+
Self::new(theme_name)
6373
}
6474

65-
pub fn default(true_color_support: bool) -> Theme {
66-
if true_color_support {
67-
Self {
68-
true_color_support,
69-
..DEFAULT_THEME.clone()
70-
}
75+
pub fn default() -> Theme {
76+
if Self::get_true_color_support() {
77+
DEFAULT_THEME.clone()
7178
} else {
72-
Self {
73-
true_color_support,
74-
..BASE16_DEFAULT_THEME.clone()
75-
}
79+
BASE16_DEFAULT_THEME.clone()
7680
}
7781
}
7882

0 commit comments

Comments
 (0)