Skip to content

Commit 8fcf4a4

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 8fcf4a4

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-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: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
borrow::Cow,
23
collections::HashMap,
34
path::{Path, PathBuf},
45
};
@@ -12,11 +13,14 @@ use toml::Value;
1213

1314
pub use crate::graphics::{Color, Modifier, Style};
1415

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

@@ -52,7 +56,13 @@ impl Loader {
5256
};
5357

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

5868
pub fn read_names(path: &Path) -> Vec<String> {
@@ -96,8 +106,10 @@ impl Loader {
96106
}
97107
}
98108

99-
#[derive(Clone, Debug)]
109+
#[derive(Clone, Debug, Default)]
100110
pub struct Theme {
111+
name: String,
112+
101113
// UI styles are stored in a HashMap
102114
styles: HashMap<String, Style>,
103115
// tree-sitter highlight styles are stored in a Vec to optimize lookups
@@ -147,6 +159,7 @@ impl<'de> Deserialize<'de> for Theme {
147159
scopes,
148160
styles,
149161
highlights,
162+
..Default::default()
150163
})
151164
}
152165
}
@@ -157,6 +170,10 @@ impl Theme {
157170
self.highlights[index]
158171
}
159172

173+
pub fn name(&self) -> Cow<str> {
174+
Cow::Borrowed(self.name.as_ref())
175+
}
176+
160177
pub fn get(&self, scope: &str) -> Style {
161178
self.try_get(scope).unwrap_or_default()
162179
}

0 commit comments

Comments
 (0)