on master help sign in
commands: Make no arg ':theme' show name (#3740)

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 <jamesodhunt@gmail.com> Signed-off-by: James O. D. Hunt <jamesodhunt@gmail.com>

commitac0fe29867012ba0840ee7c8893b41d976d5ab38 authorJames O. D. Hunt <jamesodhunt@gmail.com> dateWed 26 Oct 2022, 03:59 (4 years ago) parentba9e50e
3 files changed+37−24
book/src/generated/typable-cmd.md+1−1 helix-term/src/commands/typed.rs+15−10 helix-view/src/theme.rs+21−13
book/src/generated/typable-cmd.md+1−1
@@ -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+15−10
@@ -772,16 +772,21 @@ fn theme(
772772 };
773773 }
774774 PromptEvent::Validate => {
775− let theme_name = args.first().with_context(|| "Theme name not provided")?;
776− let theme = cx
777− .editor
778− .theme_loader
779− .load(theme_name)
780− .with_context(|| "Theme does not exist")?;
781− if !(true_color || theme.is_16_color()) {
782− bail!("Unsupported theme: theme requires true color support");
775+ if let Some(theme_name) = args.first() {
776+ let theme = cx
777+ .editor
778+ .theme_loader
779+ .load(theme_name)
780+ .with_context(|| "Theme does not exist")?;
781+ if !(true_color || theme.is_16_color()) {
782+ bail!("Unsupported theme: theme requires true color support");
783+ }
784+ cx.editor.set_theme(theme);
785+ } else {
786+ let name = cx.editor.theme.name().to_string();
787+
788+ cx.editor.set_status(name);
783789 }
784− cx.editor.set_theme(theme);
785790 }
786791 };
787792
@@ -1866,7 +1871,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
18661871 TypableCommand {
18671872 name: "theme",
18681873 aliases: &[],
1869− doc: "Change the editor theme.",
1874+ doc: "Change the editor theme (show current theme if no name specified).",
18701875 fun: theme,
18711876 completer: Some(completers::theme),
18721877 },
helix-view/src/theme.rs+21−13
@@ -14,19 +14,14 @@ use toml::{map::Map, Value};
1414use crate::graphics::UnderlineStyle;
1515pub use crate::graphics::{Color, Modifier, Style};
1616
17−pub static DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| {
18− // let raw_theme: Value = toml::from_slice(include_bytes!("../../theme.toml"))
19− // .expect("Failed to parse default theme");
20− // Theme::from(raw_theme)
21−
22− toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme")
17+pub static DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| Theme {
18+ name: "default".into(),
19+ ..toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme")
2320});
24−pub static BASE16_DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| {
25− // let raw_theme: Value = toml::from_slice(include_bytes!("../../base16_theme.toml"))
26− // .expect("Failed to parse base 16 default theme");
27− // Theme::from(raw_theme)
2821
29− toml::from_slice(include_bytes!("../../base16_theme.toml"))
22+pub static BASE16_DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| Theme {
23+ name: "base16_theme".into(),
24+ ..toml::from_slice(include_bytes!("../../base16_theme.toml"))
3025 .expect("Failed to parse base 16 default theme")
3126});
3227
@@ -53,7 +48,12 @@ impl Loader {
5348 return Ok(self.base16_default());
5449 }
5550
56− self.load_theme(name, name, false).map(Theme::from)
51+ let theme = self.load_theme(name, name, false).map(Theme::from)?;
52+
53+ Ok(Theme {
54+ name: name.into(),
55+ ..theme
56+ })
5757 }
5858
5959 // load the theme and its parent recursively and merge them
@@ -180,8 +180,10 @@ impl Loader {
180180 }
181181}
182182
183−#[derive(Clone, Debug)]
183+#[derive(Clone, Debug, Default)]
184184pub struct Theme {
185+ name: String,
186+
185187 // UI styles are stored in a HashMap
186188 styles: HashMap<String, Style>,
187189 // tree-sitter highlight styles are stored in a Vec to optimize lookups
@@ -200,6 +202,7 @@ impl From<Value> for Theme {
200202 styles,
201203 scopes,
202204 highlights,
205+ ..Default::default()
203206 }
204207 }
205208}
@@ -217,6 +220,7 @@ impl<'de> Deserialize<'de> for Theme {
217220 styles,
218221 scopes,
219222 highlights,
223+ ..Default::default()
220224 })
221225 }
222226}
@@ -266,6 +270,10 @@ impl Theme {
266270 self.highlights[index]
267271 }
268272
273+ pub fn name(&self) -> &str {
274+ &self.name
275+ }
276+
269277 pub fn get(&self, scope: &str) -> Style {
270278 self.try_get(scope).unwrap_or_default()
271279 }