Skip to content

Commit 6e1d670

Browse files
committed
Support mode 2031 dark/light mode detection
1 parent c531b7a commit 6e1d670

File tree

7 files changed

+144
-9
lines changed

7 files changed

+144
-9
lines changed

book/src/themes.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
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>`.
44

5+
Separate themes can be configured for light and dark modes. On terminals supporting [mode 2031 dark/light detection](https://github.com/contour-terminal/contour/blob/master/docs/vt-extensions/color-palette-update-notifications.md), the theme mode is detected from the terminal.
6+
7+
```toml
8+
[theme]
9+
dark = "catppuccin_frappe"
10+
light = "catppuccin_latte"
11+
## Optional. Used if the terminal doesn't declare a preference.
12+
## Defaults to the theme set for `dark` if not specified.
13+
# fallback = "catppuccin_frappe"
14+
```
15+
516
## Creating a theme
617

718
Create a file with the name of your theme as the file name (i.e `mytheme.toml`) and place it in your `themes` directory (i.e `~/.config/helix/themes` or `%AppData%\helix\themes` on Windows). The directory might have to be created beforehand.

helix-term/src/application.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ pub struct Application {
6767
signals: Signals,
6868
jobs: Jobs,
6969
lsp_progress: LspProgressMap,
70+
71+
theme_mode: Option<theme::Mode>,
7072
}
7173

7274
#[cfg(feature = "integration")]
@@ -109,6 +111,7 @@ impl Application {
109111
#[cfg(feature = "integration")]
110112
let backend = TestBackend::new(120, 150);
111113

114+
let theme_mode = backend.get_theme_mode();
112115
let terminal = Terminal::new(backend)?;
113116
let area = terminal.size().expect("couldn't get terminal size");
114117
let mut compositor = Compositor::new(area);
@@ -127,6 +130,7 @@ impl Application {
127130
&mut editor,
128131
&config.load(),
129132
terminal.backend().supports_true_color(),
133+
theme_mode,
130134
);
131135

132136
let keys = Box::new(Map::new(Arc::clone(&config), |config: &Config| {
@@ -246,6 +250,7 @@ impl Application {
246250
signals,
247251
jobs: Jobs::new(),
248252
lsp_progress: LspProgressMap::new(),
253+
theme_mode,
249254
};
250255

251256
Ok(app)
@@ -404,6 +409,7 @@ impl Application {
404409
&mut self.editor,
405410
&default_config,
406411
self.terminal.backend().supports_true_color(),
412+
self.theme_mode,
407413
);
408414

409415
// Re-parse any open documents with the new language config.
@@ -437,12 +443,18 @@ impl Application {
437443
}
438444

439445
/// Load the theme set in configuration
440-
fn load_configured_theme(editor: &mut Editor, config: &Config, terminal_true_color: bool) {
446+
fn load_configured_theme(
447+
editor: &mut Editor,
448+
config: &Config,
449+
terminal_true_color: bool,
450+
mode: Option<theme::Mode>,
451+
) {
441452
let true_color = terminal_true_color || config.editor.true_color || crate::true_color();
442453
let theme = config
443454
.theme
444455
.as_ref()
445-
.and_then(|theme| {
456+
.and_then(|theme_config| {
457+
let theme = theme_config.choose(mode);
446458
editor
447459
.theme_loader
448460
.load(theme)
@@ -660,6 +672,8 @@ impl Application {
660672
}
661673

662674
pub async fn handle_terminal_events(&mut self, event: std::io::Result<termina::Event>) {
675+
use termina::escape::csi;
676+
663677
let mut cx = crate::compositor::Context {
664678
editor: &mut self.editor,
665679
jobs: &mut self.jobs,
@@ -684,6 +698,15 @@ impl Application {
684698
kind: termina::event::KeyEventKind::Release,
685699
..
686700
}) => false,
701+
termina::Event::Csi(csi::Csi::Mode(csi::Mode::ReportTheme(mode))) => {
702+
Self::load_configured_theme(
703+
&mut self.editor,
704+
&self.config.load(),
705+
self.terminal.backend().supports_true_color(),
706+
Some(mode.into()),
707+
);
708+
true
709+
}
687710
event => self.compositor.handle_event(&event.into(), &mut cx),
688711
};
689712

@@ -1134,9 +1157,16 @@ impl Application {
11341157

11351158
#[cfg(not(feature = "integration"))]
11361159
pub fn event_stream(&self) -> impl Stream<Item = std::io::Result<termina::Event>> + Unpin {
1137-
use termina::Terminal as _;
1160+
use termina::{escape::csi, Terminal as _};
11381161
let reader = self.terminal.backend().terminal().event_reader();
1139-
termina::EventStream::new(reader, |event| !event.is_escape())
1162+
termina::EventStream::new(reader, |event| {
1163+
// Accept either non-escape sequences or theme mode updates.
1164+
!event.is_escape()
1165+
|| matches!(
1166+
event,
1167+
termina::Event::Csi(csi::Csi::Mode(csi::Mode::ReportTheme(_)))
1168+
)
1169+
})
11401170
}
11411171

11421172
#[cfg(feature = "integration")]

helix-term/src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::keymap;
22
use crate::keymap::{merge_keys, KeyTrie};
33
use helix_loader::merge_toml_values;
4-
use helix_view::document::Mode;
4+
use helix_view::{document::Mode, theme};
55
use serde::Deserialize;
66
use std::collections::HashMap;
77
use std::fmt::Display;
@@ -11,15 +11,15 @@ use toml::de::Error as TomlError;
1111

1212
#[derive(Debug, Clone, PartialEq)]
1313
pub struct Config {
14-
pub theme: Option<String>,
14+
pub theme: Option<theme::Config>,
1515
pub keys: HashMap<Mode, KeyTrie>,
1616
pub editor: helix_view::editor::Config,
1717
}
1818

1919
#[derive(Debug, Clone, PartialEq, Deserialize)]
2020
#[serde(deny_unknown_fields)]
2121
pub struct ConfigRaw {
22-
pub theme: Option<String>,
22+
pub theme: Option<theme::Config>,
2323
pub keys: Option<HashMap<Mode, KeyTrie>>,
2424
pub editor: Option<toml::Value>,
2525
}

helix-tui/src/backend/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ pub trait Backend {
3939
/// Flushes the terminal buffer
4040
fn flush(&mut self) -> Result<(), io::Error>;
4141
fn supports_true_color(&self) -> bool;
42+
fn get_theme_mode(&self) -> Option<helix_view::theme::Mode>;
4243
}

helix-tui/src/backend/termina.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::{self, Write as _};
33
use helix_view::{
44
editor::KittyKeyboardProtocolConfig,
55
graphics::{CursorKind, Rect, UnderlineStyle},
6-
theme::{Color, Modifier},
6+
theme::{self, Color, Modifier},
77
};
88
use termina::{
99
escape::{
@@ -52,6 +52,7 @@ struct Capabilities {
5252
synchronized_output: bool,
5353
true_color: bool,
5454
extended_underlines: bool,
55+
theme_mode: Option<theme::Mode>,
5556
}
5657

5758
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
@@ -148,11 +149,13 @@ impl TerminaBackend {
148149
// If we only receive the device attributes then we know it is not.
149150
write!(
150151
terminal,
151-
"{}{}{}{}{}{}",
152+
"{}{}{}{}{}{}{}",
152153
// Synchronized output
153154
Csi::Mode(csi::Mode::QueryDecPrivateMode(csi::DecPrivateMode::Code(
154155
csi::DecPrivateModeCode::SynchronizedOutput
155156
))),
157+
// Mode 2031 theme updates. Query the current theme.
158+
Csi::Mode(csi::Mode::QueryTheme),
156159
// True color and while we're at it, extended underlines:
157160
// <https://github.com/termstandard/colors?tab=readme-ov-file#querying-the-terminal>
158161
Csi::Sgr(csi::Sgr::Background(TEST_COLOR.into())),
@@ -184,6 +187,9 @@ impl TerminaBackend {
184187
})) => {
185188
capabilities.synchronized_output = true;
186189
}
190+
Event::Csi(Csi::Mode(csi::Mode::ReportTheme(mode))) => {
191+
capabilities.theme_mode = Some(mode.into());
192+
}
187193
Event::Dcs(dcs::Dcs::Response {
188194
value: dcs::DcsResponse::GraphicRendition(sgrs),
189195
..
@@ -320,6 +326,11 @@ impl TerminaBackend {
320326
}
321327
}
322328

329+
if self.capabilities.theme_mode.is_some() {
330+
// Enable mode 2031 theme mode notifications:
331+
write!(self.terminal, "{}", decset!(Theme))?;
332+
}
333+
323334
Ok(())
324335
}
325336

@@ -332,6 +343,11 @@ impl TerminaBackend {
332343
)?;
333344
}
334345

346+
if self.capabilities.theme_mode.is_some() {
347+
// Mode 2031 theme notifications.
348+
write!(self.terminal, "{}", decreset!(Theme))?;
349+
}
350+
335351
Ok(())
336352
}
337353

@@ -550,6 +566,10 @@ impl Backend for TerminaBackend {
550566
fn supports_true_color(&self) -> bool {
551567
self.capabilities.true_color
552568
}
569+
570+
fn get_theme_mode(&self) -> Option<theme::Mode> {
571+
self.capabilities.theme_mode
572+
}
553573
}
554574

555575
impl Drop for TerminaBackend {

helix-tui/src/backend/test.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,8 @@ impl Backend for TestBackend {
160160
fn supports_true_color(&self) -> bool {
161161
false
162162
}
163+
164+
fn get_theme_mode(&self) -> Option<helix_view::theme::Mode> {
165+
None
166+
}
163167
}

helix-view/src/theme.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,75 @@ pub static BASE16_DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| Theme {
3535
..Theme::from(BASE16_DEFAULT_THEME_DATA.clone())
3636
});
3737

38+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
39+
pub enum Mode {
40+
Dark,
41+
Light,
42+
}
43+
44+
#[cfg(feature = "term")]
45+
impl From<termina::escape::csi::ThemeMode> for Mode {
46+
fn from(mode: termina::escape::csi::ThemeMode) -> Self {
47+
match mode {
48+
termina::escape::csi::ThemeMode::Dark => Self::Dark,
49+
termina::escape::csi::ThemeMode::Light => Self::Light,
50+
}
51+
}
52+
}
53+
54+
#[derive(Debug, Clone, PartialEq, Eq)]
55+
pub struct Config {
56+
light: String,
57+
dark: String,
58+
/// A theme to choose when the terminal did not declare either light or dark mode.
59+
/// When not specified the dark theme is preferred.
60+
fallback: Option<String>,
61+
}
62+
63+
impl Config {
64+
pub fn choose(&self, preference: Option<Mode>) -> &str {
65+
match preference {
66+
Some(Mode::Light) => &self.light,
67+
Some(Mode::Dark) => &self.dark,
68+
None => self.fallback.as_ref().unwrap_or(&self.dark),
69+
}
70+
}
71+
}
72+
73+
impl<'de> Deserialize<'de> for Config {
74+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
75+
where
76+
D: serde::Deserializer<'de>,
77+
{
78+
#[derive(Deserialize)]
79+
#[serde(untagged, deny_unknown_fields, rename_all = "kebab-case")]
80+
enum InnerConfig {
81+
Constant(String),
82+
Adaptive {
83+
dark: String,
84+
light: String,
85+
fallback: Option<String>,
86+
},
87+
}
88+
89+
let inner = InnerConfig::deserialize(deserializer)?;
90+
91+
let (light, dark, fallback) = match inner {
92+
InnerConfig::Constant(theme) => (theme.clone(), theme.clone(), None),
93+
InnerConfig::Adaptive {
94+
light,
95+
dark,
96+
fallback,
97+
} => (light, dark, fallback),
98+
};
99+
100+
Ok(Self {
101+
light,
102+
dark,
103+
fallback,
104+
})
105+
}
106+
}
38107
#[derive(Clone, Debug)]
39108
pub struct Loader {
40109
/// Theme directories to search from highest to lowest priority

0 commit comments

Comments
 (0)