From d18dbe9ff79f8a3d736f5f49882c62090060014a Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 1 Jun 2019 15:39:46 -0700 Subject: [PATCH] parse OSC 4 This code is used to change the color palette at runtime. We can parse it but not do anything useful with it yet. --- term/src/terminalstate.rs | 7 +++++++ termwiz/src/color.rs | 13 +++++++++++-- termwiz/src/escape/osc.rs | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index b5c369db2..72c13bafa 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -2100,6 +2100,13 @@ impl<'a> Performer<'a> { OperatingSystemCommand::SystemNotification(message) => { eprintln!("Application sends SystemNotification: {}", message); } + OperatingSystemCommand::ChangeColorNumber(palette_index, colorspec) => { + eprintln!( + "ChangeColorNumber {} to {}", + palette_index, + colorspec.to_rgb_string() + ); + } } } } diff --git a/termwiz/src/color.rs b/termwiz/src/color.rs index 5f0bd0217..5b46d1379 100644 --- a/termwiz/src/color.rs +++ b/termwiz/src/color.rs @@ -128,6 +128,16 @@ impl RgbColor { None } } + + /// Construct a color from an SVG/CSS3 color name. + /// or from a string of the form `#RRGGBB` where + /// R, G and B are all hex digits. + /// Returns None if the supplied name is not recognized. + /// The list of names can be found here: + /// + pub fn from_named_or_rgb_string(s: &str) -> Option { + RgbColor::from_rgb_str(&s).or_else(|| RgbColor::from_named(&s)) + } } /// This is mildly unfortunate: in order to round trip RgbColor with serde @@ -153,8 +163,7 @@ impl<'de> Deserialize<'de> for RgbColor { D: Deserializer<'de>, { let s = String::deserialize(deserializer)?; - RgbColor::from_rgb_str(&s) - .or_else(|| RgbColor::from_named(&s)) + RgbColor::from_named_or_rgb_string(&s) .ok_or_else(|| format!("unknown color name: {}", s)) .map_err(serde::de::Error::custom) } diff --git a/termwiz/src/escape/osc.rs b/termwiz/src/escape/osc.rs index af9f18f0f..4499c70c0 100644 --- a/termwiz/src/escape/osc.rs +++ b/termwiz/src/escape/osc.rs @@ -1,3 +1,4 @@ +use crate::color::RgbColor; pub use crate::hyperlink::Hyperlink; use base64; use bitflags::bitflags; @@ -20,6 +21,7 @@ pub enum OperatingSystemCommand { SetSelection(Selection, String), SystemNotification(String), ITermProprietary(ITermProprietary), + ChangeColorNumber(usize, RgbColor), Unspecified(Vec>), } @@ -125,6 +127,17 @@ impl OperatingSystemCommand { } } + fn parse_change_color_number(osc: &[&[u8]]) -> Fallible { + ensure!(osc.len() == 3, "wrong number of params"); + + let index: usize = str::from_utf8(osc[1])?.parse()?; + let spec = str::from_utf8(osc[2])?; + let spec = RgbColor::from_named_or_rgb_string(spec) + .ok_or_else(|| err_msg("invalid color spec"))?; + + Ok(OperatingSystemCommand::ChangeColorNumber(index, spec)) + } + fn internal_parse(osc: &[&[u8]]) -> Fallible { ensure!(!osc.is_empty(), "no params"); let p1str = String::from_utf8_lossy(osc[0]); @@ -154,6 +167,7 @@ impl OperatingSystemCommand { ITermProprietary => { self::ITermProprietary::parse(osc).map(OperatingSystemCommand::ITermProprietary) } + ChangeColorNumber => Self::parse_change_color_number(osc), _ => bail!("not impl"), } @@ -221,6 +235,7 @@ impl Display for OperatingSystemCommand { SetSelection(s, val) => write!(f, "52;{};{}", s, base64::encode(val))?, SystemNotification(s) => write!(f, "9;{}", s)?, ITermProprietary(i) => i.fmt(f)?, + ChangeColorNumber(index, spec) => write!(f, "4;{};{}", index, spec.to_rgb_string())?, }; write!(f, "\x07")?; Ok(())