aboutsummaryrefslogtreecommitdiff
path: root/src/terminal_theme.rs
diff options
context:
space:
mode:
authorakbash <akbash@herdr.dev>2026-07-24 01:28:35 +0300
committerGitHub <noreply@github.com>2026-07-24 01:28:35 +0300
commit503613c8ecbea752a234daf30cb0ad5a8bf93e4e (patch)
tree34b71e4c7055244ed191c043457c572e3ccfc935 /src/terminal_theme.rs
parent58ad2c6438cf25cdf7e95528c7114d3bf12626d2 (diff)
fix: inherit host palette in pane queries (#1759)
refs #1752 Co-authored-by: Ogulcan Celik <ogulcancelik@gmail.com>
Diffstat (limited to 'src/terminal_theme.rs')
-rw-r--r--src/terminal_theme.rs58
1 files changed, 57 insertions, 1 deletions
diff --git a/src/terminal_theme.rs b/src/terminal_theme.rs
index 5627ab1b..154b36fe 100644
--- a/src/terminal_theme.rs
+++ b/src/terminal_theme.rs
@@ -22,10 +22,21 @@ impl RgbColor {
}
}
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TerminalTheme {
pub foreground: Option<RgbColor>,
pub background: Option<RgbColor>,
+ pub palette: [Option<RgbColor>; 256],
+}
+
+impl Default for TerminalTheme {
+ fn default() -> Self {
+ Self {
+ foreground: None,
+ background: None,
+ palette: [None; 256],
+ }
+ }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -47,11 +58,26 @@ impl TerminalTheme {
self
}
+ pub fn with_palette_color(mut self, index: u8, color: RgbColor) -> Self {
+ self.palette[usize::from(index)] = Some(color);
+ self
+ }
+
pub fn is_empty(self) -> bool {
self.foreground.is_none() && self.background.is_none()
}
}
+pub fn host_terminal_theme_query_sequence() -> String {
+ use std::fmt::Write as _;
+
+ let mut sequence = String::from(HOST_COLOR_QUERY_SEQUENCE);
+ for index in 0..=u8::MAX {
+ let _ = write!(sequence, "\x1b]4;{index};?\x1b\\");
+ }
+ sequence
+}
+
pub fn parse_default_color_response(sequence: &str) -> Option<(DefaultColorKind, RgbColor)> {
let body = sequence.strip_prefix("\x1b]")?;
let body = body
@@ -66,6 +92,15 @@ pub fn parse_default_color_response(sequence: &str) -> Option<(DefaultColorKind,
Some((kind, parse_rgb_color(value)?))
}
+pub fn parse_palette_color_response(sequence: &str) -> Option<(u8, RgbColor)> {
+ let body = sequence.strip_prefix("\x1b]4;")?;
+ let body = body
+ .strip_suffix("\x1b\\")
+ .or_else(|| body.strip_suffix('\u{7}'))?;
+ let (index, value) = body.split_once(';')?;
+ Some((index.parse().ok()?, parse_rgb_color(value)?))
+}
+
pub fn osc_set_default_color_sequence(kind: DefaultColorKind, color: RgbColor) -> String {
let command = match kind {
DefaultColorKind::Foreground => 10,
@@ -159,6 +194,27 @@ mod tests {
}
#[test]
+ fn parses_palette_responses_and_builds_full_query() {
+ assert_eq!(
+ parse_palette_color_response("\x1b]4;255;rgb:1111/2222/3333\x1b\\"),
+ Some((
+ 255,
+ RgbColor {
+ r: 0x11,
+ g: 0x22,
+ b: 0x33,
+ }
+ ))
+ );
+
+ let query = host_terminal_theme_query_sequence();
+ assert!(query.starts_with(HOST_COLOR_QUERY_SEQUENCE));
+ assert!(query.contains("\x1b]4;0;?\x1b\\"));
+ assert!(query.ends_with("\x1b]4;255;?\x1b\\"));
+ assert_eq!(query.matches("\x1b]4;").count(), 256);
+ }
+
+ #[test]
fn default_color_reset_sequences_use_xterm_osc_numbers() {
assert_eq!(
osc_reset_default_color_sequence(DefaultColorKind::Foreground),