diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-06-18 02:52:35 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-06-18 02:53:31 +0300 |
| commit | 36b4001f4526b60f1c9b67a4a34596e5ad2bcca1 (patch) | |
| tree | 520ab28a0a3f3f798e6e655fc83529e8617720e9 /src/terminal_theme.rs | |
| parent | aca35ea76f3df15ec68fd07959d939f5e7a78a6c (diff) | |
feat: sync theme with host appearance
refs #675
Diffstat (limited to 'src/terminal_theme.rs')
| -rw-r--r-- | src/terminal_theme.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/terminal_theme.rs b/src/terminal_theme.rs index 523e3e38..5627ab1b 100644 --- a/src/terminal_theme.rs +++ b/src/terminal_theme.rs @@ -5,6 +5,23 @@ pub struct RgbColor { pub b: u8, } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum HostAppearance { + Dark, + Light, +} + +impl RgbColor { + pub fn inferred_appearance(self) -> HostAppearance { + let luminance = u32::from(self.r) * 299 + u32::from(self.g) * 587 + u32::from(self.b) * 114; + if luminance >= 128_000 { + HostAppearance::Light + } else { + HostAppearance::Dark + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub struct TerminalTheme { pub foreground: Option<RgbColor>, @@ -18,6 +35,8 @@ pub enum DefaultColorKind { } pub const HOST_COLOR_QUERY_SEQUENCE: &str = "\x1b]10;?\x1b\\\x1b]11;?\x1b\\"; +pub const HOST_COLOR_SCHEME_REPORT_ENABLE_SEQUENCE: &str = "\x1b[?2031h"; +pub const HOST_COLOR_SCHEME_REPORT_DISABLE_SEQUENCE: &str = "\x1b[?2031l"; impl TerminalTheme { pub fn with_color(mut self, kind: DefaultColorKind, color: RgbColor) -> Self { @@ -58,6 +77,13 @@ pub fn osc_set_default_color_sequence(kind: DefaultColorKind, color: RgbColor) - ) } +pub fn osc_reset_default_color_sequence(kind: DefaultColorKind) -> &'static str { + match kind { + DefaultColorKind::Foreground => "\x1b]110\x1b\\", + DefaultColorKind::Background => "\x1b]111\x1b\\", + } +} + fn parse_rgb_color(value: &str) -> Option<RgbColor> { if let Some(rgb) = value.strip_prefix("rgb:") { let mut parts = rgb.split('/'); @@ -133,6 +159,18 @@ mod tests { } #[test] + fn default_color_reset_sequences_use_xterm_osc_numbers() { + assert_eq!( + osc_reset_default_color_sequence(DefaultColorKind::Foreground), + "\x1b]110\x1b\\" + ); + assert_eq!( + osc_reset_default_color_sequence(DefaultColorKind::Background), + "\x1b]111\x1b\\" + ); + } + + #[test] fn scales_short_hex_components() { assert_eq!(parse_hex_component("f"), Some(255)); assert_eq!(parse_hex_component("80"), Some(128)); |
