Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adaptive Theme Selection Based on the Terminal Color Scheme #12098

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Example config:

```toml
theme = "onedark"
# Adaptive Theme
# theme = { light = "onelight", dark = "onedark" }

[editor]
line-number = "relative"
Expand Down
1 change: 1 addition & 0 deletions book/src/themes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Themes

To use a theme add `theme = "<name>"` to the top of your [`config.toml`](./configuration.md) file, or select it during runtime using `:theme <name>`.
Alternatively, `theme = { light = "<name>", dark = "<name>" }` tries to select a theme based on the terminal color scheme. The detection should work with all major terminals including Windows Terminal (starting with v1.22).

## Creating a theme

Expand Down
2 changes: 2 additions & 0 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl Application {
let theme = config
.theme
.as_ref()
.map(helix_view::theme::Config::get_active_theme)
.and_then(|theme| {
theme_loader
.load(theme)
Expand Down Expand Up @@ -428,6 +429,7 @@ impl Application {
let theme = config
.theme
.as_ref()
.map(helix_view::theme::Config::get_active_theme)
.and_then(|theme| {
self.theme_loader
.load(theme)
Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use toml::de::Error as TomlError;

#[derive(Debug, Clone, PartialEq)]
pub struct Config {
pub theme: Option<String>,
pub theme: Option<helix_view::theme::Config>,
pub keys: HashMap<Mode, KeyTrie>,
pub editor: helix_view::editor::Config,
}

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConfigRaw {
pub theme: Option<String>,
pub theme: Option<helix_view::theme::Config>,
pub keys: Option<HashMap<Mode, KeyTrie>>,
pub editor: Option<toml::Value>,
}
Expand Down
1 change: 1 addition & 0 deletions helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ helix-vcs = { path = "../helix-vcs" }
bitflags = "2.6"
anyhow = "1"
crossterm = { version = "0.28", optional = true }
terminal-colorsaurus = "0.4"

tempfile = "3.14"

Expand Down
24 changes: 23 additions & 1 deletion helix-view/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use helix_core::hashmap;
use helix_loader::merge_toml_values;
use log::warn;
use once_cell::sync::Lazy;
use serde::{Deserialize, Deserializer};
use serde::{Deserialize, Deserializer, Serialize};
use toml::{map::Map, Value};

use crate::graphics::UnderlineStyle;
Expand Down Expand Up @@ -540,6 +540,28 @@ impl TryFrom<Value> for ThemePalette {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged, deny_unknown_fields)]
pub enum Config {
Static(String),
Adaptive { light: String, dark: String },
}

impl Config {
pub fn get_active_theme(&self) -> &str {
match self {
Config::Static(x) => x,
Config::Adaptive { light, dark } => {
use terminal_colorsaurus::{color_scheme, ColorScheme, QueryOptions};
match color_scheme(QueryOptions::default()).unwrap_or(ColorScheme::Dark) {
ColorScheme::Light => light,
ColorScheme::Dark => dark,
}
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down