aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src
diff options
context:
space:
mode:
authorOmnikar2021-11-14 12:26:48 +0000
committerBlaž Hrastnik2021-12-13 15:47:18 +0000
commit98ce2a301da25152563137047377026d72fd644c (patch)
tree1e33160822106b7c84be3c7084c2b773fbece444 /helix-term/src
parent43d17c482cda07fa0dc4985d60c14391a4926940 (diff)
Load alt default theme if true color is not supported
* Move `runtime/themes/base16_default_terminal.toml` to `base16_theme.toml` alongside `theme.toml` * Use `terminfo` crate to detect whether the terminal supports true color and, if the user has no theme configured and their terminal does not support true color, load the alt default theme instead of the normal default. Remove `terminfo` dependency, use `COLORTERM` env instead Prevent user from switching to an unsupported theme Add `true-color-override` option If the terminal is wrongly detected to not support true color, `true-color-override = true` will override the detection. Rename `true-color-override` to `true-color`
Diffstat (limited to 'helix-term/src')
-rw-r--r--helix-term/src/application.rs28
-rw-r--r--helix-term/src/commands.rs14
-rw-r--r--helix-term/src/lib.rs6
-rw-r--r--helix-term/src/ui/mod.rs1
4 files changed, 38 insertions, 11 deletions
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs
index 90330751..3e0b6d59 100644
--- a/helix-term/src/application.rs
+++ b/helix-term/src/application.rs
@@ -76,17 +76,27 @@ impl Application {
None => Ok(def_lang_conf),
};
- let theme = if let Some(theme) = &config.theme {
- match theme_loader.load(theme) {
- Ok(theme) => theme,
- Err(e) => {
- log::warn!("failed to load theme `{}` - {}", theme, e);
+ let true_color = config.editor.true_color || crate::true_color();
+ let theme = config
+ .theme
+ .as_ref()
+ .and_then(|theme| {
+ theme_loader
+ .load(theme)
+ .map_err(|e| {
+ log::warn!("failed to load theme `{}` - {}", theme, e);
+ e
+ })
+ .ok()
+ .filter(|theme| (true_color || theme.is_16_color()))
+ })
+ .unwrap_or_else(|| {
+ if true_color {
theme_loader.default()
+ } else {
+ theme_loader.base16_default()
}
- }
- } else {
- theme_loader.default()
- };
+ });
let syn_loader_conf: helix_core::syntax::Configuration = lang_conf
.and_then(|conf| conf.try_into())
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 0ae225e9..22c23043 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -2366,8 +2366,18 @@ pub mod cmd {
args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
- let theme = args.first().context("theme not provided")?;
- cx.editor.set_theme_from_name(theme)
+ let theme = args.first().context("Theme not provided")?;
+ let theme = cx
+ .editor
+ .theme_loader
+ .load(theme)
+ .with_context(|| format!("Failed setting theme {}", theme))?;
+ let true_color = cx.editor.config.true_color || crate::true_color();
+ if !(true_color || theme.is_16_color()) {
+ bail!("Unsupported theme: theme requires true color support");
+ }
+ cx.editor.set_theme(theme);
+ Ok(())
}
fn yank_main_selection_to_clipboard(
diff --git a/helix-term/src/lib.rs b/helix-term/src/lib.rs
index f5e3a8cd..4fe76cd5 100644
--- a/helix-term/src/lib.rs
+++ b/helix-term/src/lib.rs
@@ -9,3 +9,9 @@ pub mod config;
pub mod job;
pub mod keymap;
pub mod ui;
+
+fn true_color() -> bool {
+ std::env::var("COLORTERM")
+ .map(|v| matches!(v.as_str(), "truecolor" | "24bit"))
+ .unwrap_or(false)
+}
diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs
index cdf42311..f57e2e2b 100644
--- a/helix-term/src/ui/mod.rs
+++ b/helix-term/src/ui/mod.rs
@@ -186,6 +186,7 @@ pub mod completers {
&helix_core::config_dir().join("themes"),
));
names.push("default".into());
+ names.push("base16_default".into());
let mut names: Vec<_> = names
.into_iter()