Skip to content

Commit 3348364

Browse files
committed
commands: Make no arg ':theme' show name
Most commands that accept an argument show their current value if no argument is specified. The `:theme` command previously displayed an error message in the status bar if not provided with an argument: ``` Theme name not provided ``` It now shows the current theme name in the status bar if no argument is specified. Signed-off-by: James O. D. Hunt <[email protected]>
1 parent 16ce036 commit 3348364

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

helix-term/src/commands/typed.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -725,16 +725,19 @@ fn theme(
725725
};
726726
}
727727
PromptEvent::Validate => {
728-
let theme_name = args.first().with_context(|| "Theme name not provided")?;
729-
let theme = cx
730-
.editor
731-
.theme_loader
732-
.load(theme_name)
733-
.with_context(|| "Theme does not exist")?;
734-
if !(true_color || theme.is_16_color()) {
735-
bail!("Unsupported theme: theme requires true color support");
728+
if let Some(theme_name) = args.first() {
729+
let theme = cx
730+
.editor
731+
.theme_loader
732+
.load(theme_name)
733+
.with_context(|| "Theme does not exist")?;
734+
if !(true_color || theme.is_16_color()) {
735+
bail!("Unsupported theme: theme requires true color support");
736+
}
737+
cx.editor.set_theme(theme);
738+
} else {
739+
cx.editor.set_status(cx.editor.theme.name());
736740
}
737-
cx.editor.set_theme(theme);
738741
}
739742
};
740743

helix-view/src/theme.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ use toml::Value;
1212

1313
pub use crate::graphics::{Color, Modifier, Style};
1414

15-
pub static DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| {
16-
toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme")
15+
pub static DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| Theme {
16+
name: "theme".into(),
17+
..toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme")
1718
});
18-
pub static BASE16_DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| {
19-
toml::from_slice(include_bytes!("../../base16_theme.toml"))
19+
20+
pub static BASE16_DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| Theme {
21+
name: "base16_theme".into(),
22+
..toml::from_slice(include_bytes!("../../base16_theme.toml"))
2023
.expect("Failed to parse base 16 default theme")
2124
});
2225

@@ -52,7 +55,13 @@ impl Loader {
5255
};
5356

5457
let data = std::fs::read(&path)?;
55-
toml::from_slice(data.as_slice()).context("Failed to deserialize theme")
58+
59+
let theme = Theme {
60+
name: name.into(),
61+
..toml::from_slice(data.as_slice()).context("Failed to deserialize theme")?
62+
};
63+
64+
Ok(theme)
5665
}
5766

5867
pub fn read_names(path: &Path) -> Vec<String> {
@@ -96,8 +105,10 @@ impl Loader {
96105
}
97106
}
98107

99-
#[derive(Clone, Debug)]
108+
#[derive(Clone, Debug, Default)]
100109
pub struct Theme {
110+
name: String,
111+
101112
// UI styles are stored in a HashMap
102113
styles: HashMap<String, Style>,
103114
// tree-sitter highlight styles are stored in a Vec to optimize lookups
@@ -147,6 +158,7 @@ impl<'de> Deserialize<'de> for Theme {
147158
scopes,
148159
styles,
149160
highlights,
161+
..Default::default()
150162
})
151163
}
152164
}
@@ -157,6 +169,10 @@ impl Theme {
157169
self.highlights[index]
158170
}
159171

172+
pub fn name(&self) -> String {
173+
self.name.to_string()
174+
}
175+
160176
pub fn get(&self, scope: &str) -> Style {
161177
self.try_get(scope).unwrap_or_default()
162178
}

0 commit comments

Comments
 (0)