Skip to content

Commit 94bc504

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 fae9203 commit 94bc504

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

book/src/generated/typable-cmd.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
| `:quit-all!`, `:qa!` | Force close all views ignoring unsaved changes. |
2929
| `:cquit`, `:cq` | Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2). |
3030
| `:cquit!`, `:cq!` | Force quit with exit code (default 1) ignoring unsaved changes. Accepts an optional integer exit code (:cq! 2). |
31-
| `:theme` | Change the editor theme. |
31+
| `:theme` | Change the editor theme (show current theme if no name specified). |
3232
| `:clipboard-yank` | Yank main selection into system clipboard. |
3333
| `:clipboard-yank-join` | Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline. |
3434
| `:primary-clipboard-yank` | Yank main selection into system primary clipboard. |

helix-term/src/commands/typed.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -725,16 +725,21 @@ 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+
let name = cx.editor.theme.name().to_string();
740+
741+
cx.editor.set_status(name);
736742
}
737-
cx.editor.set_theme(theme);
738743
}
739744
};
740745

@@ -1720,7 +1725,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
17201725
TypableCommand {
17211726
name: "theme",
17221727
aliases: &[],
1723-
doc: "Change the editor theme.",
1728+
doc: "Change the editor theme (show current theme if no name specified).",
17241729
fun: theme,
17251730
completer: Some(completers::theme),
17261731
},

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: "default".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) -> &str {
173+
&self.name
174+
}
175+
160176
pub fn get(&self, scope: &str) -> Style {
161177
self.try_get(scope).unwrap_or_default()
162178
}

0 commit comments

Comments
 (0)