Skip to content

Commit

Permalink
commands: Make no arg ':theme' show name (#3740)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jamesodhunt authored Oct 26, 2022
1 parent ba9e50e commit ac0fe29
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
2 changes: 1 addition & 1 deletion book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
| `:quit-all!`, `:qa!` | Force close all views ignoring unsaved changes. |
| `:cquit`, `:cq` | Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2). |
| `:cquit!`, `:cq!` | Force quit with exit code (default 1) ignoring unsaved changes. Accepts an optional integer exit code (:cq! 2). |
| `:theme` | Change the editor theme. |
| `:theme` | Change the editor theme (show current theme if no name specified). |
| `:clipboard-yank` | Yank main selection into system clipboard. |
| `:clipboard-yank-join` | Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline. |
| `:primary-clipboard-yank` | Yank main selection into system primary clipboard. |
Expand Down
25 changes: 15 additions & 10 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,16 +772,21 @@ fn theme(
};
}
PromptEvent::Validate => {
let theme_name = args.first().with_context(|| "Theme name not provided")?;
let theme = cx
.editor
.theme_loader
.load(theme_name)
.with_context(|| "Theme does not exist")?;
if !(true_color || theme.is_16_color()) {
bail!("Unsupported theme: theme requires true color support");
if let Some(theme_name) = args.first() {
let theme = cx
.editor
.theme_loader
.load(theme_name)
.with_context(|| "Theme does not exist")?;
if !(true_color || theme.is_16_color()) {
bail!("Unsupported theme: theme requires true color support");
}
cx.editor.set_theme(theme);
} else {
let name = cx.editor.theme.name().to_string();

cx.editor.set_status(name);
}
cx.editor.set_theme(theme);
}
};

Expand Down Expand Up @@ -1866,7 +1871,7 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "theme",
aliases: &[],
doc: "Change the editor theme.",
doc: "Change the editor theme (show current theme if no name specified).",
fun: theme,
completer: Some(completers::theme),
},
Expand Down
34 changes: 21 additions & 13 deletions helix-view/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ use toml::{map::Map, Value};
use crate::graphics::UnderlineStyle;
pub use crate::graphics::{Color, Modifier, Style};

pub static DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| {
// let raw_theme: Value = toml::from_slice(include_bytes!("../../theme.toml"))
// .expect("Failed to parse default theme");
// Theme::from(raw_theme)

toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme")
pub static DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| Theme {
name: "default".into(),
..toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme")
});
pub static BASE16_DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| {
// let raw_theme: Value = toml::from_slice(include_bytes!("../../base16_theme.toml"))
// .expect("Failed to parse base 16 default theme");
// Theme::from(raw_theme)

toml::from_slice(include_bytes!("../../base16_theme.toml"))
pub static BASE16_DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| Theme {
name: "base16_theme".into(),
..toml::from_slice(include_bytes!("../../base16_theme.toml"))
.expect("Failed to parse base 16 default theme")
});

Expand All @@ -53,7 +48,12 @@ impl Loader {
return Ok(self.base16_default());
}

self.load_theme(name, name, false).map(Theme::from)
let theme = self.load_theme(name, name, false).map(Theme::from)?;

Ok(Theme {
name: name.into(),
..theme
})
}

// load the theme and its parent recursively and merge them
Expand Down Expand Up @@ -180,8 +180,10 @@ impl Loader {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct Theme {
name: String,

// UI styles are stored in a HashMap
styles: HashMap<String, Style>,
// tree-sitter highlight styles are stored in a Vec to optimize lookups
Expand All @@ -200,6 +202,7 @@ impl From<Value> for Theme {
styles,
scopes,
highlights,
..Default::default()
}
}
}
Expand All @@ -217,6 +220,7 @@ impl<'de> Deserialize<'de> for Theme {
styles,
scopes,
highlights,
..Default::default()
})
}
}
Expand Down Expand Up @@ -266,6 +270,10 @@ impl Theme {
self.highlights[index]
}

pub fn name(&self) -> &str {
&self.name
}

pub fn get(&self, scope: &str) -> Style {
self.try_get(scope).unwrap_or_default()
}
Expand Down

0 comments on commit ac0fe29

Please sign in to comment.