From e507d4e1e7ef6a449894d005a4d1945c2cf0e869 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 21 Jun 2020 10:03:28 -0700 Subject: [PATCH] termwiz: allow OperatingSystemCommandCode to have non-numeric mappings The code assumed that OSC parameters were always numeric, but that isn't the case. In order to allow adding non-numeric OSC code we need to adjust and allow for the codes to be any string value, and that's what this commit does. It shouldn't change any other behavior. --- termwiz/src/escape/osc.rs | 152 ++++++++++++++++++++++++++------------ 1 file changed, 104 insertions(+), 48 deletions(-) diff --git a/termwiz/src/escape/osc.rs b/termwiz/src/escape/osc.rs index 6c92e14c6..06e9244ad 100644 --- a/termwiz/src/escape/osc.rs +++ b/termwiz/src/escape/osc.rs @@ -246,9 +246,8 @@ impl OperatingSystemCommand { fn internal_parse(osc: &[&[u8]]) -> anyhow::Result { ensure!(!osc.is_empty(), "no params"); let p1str = String::from_utf8_lossy(osc[0]); - let code: i64 = p1str.parse()?; - let osc_code: OperatingSystemCommandCode = - FromPrimitive::from_i64(code).ok_or_else(|| anyhow!("unknown code"))?; + let osc_code = + OperatingSystemCommandCode::from_code(&p1str).ok_or_else(|| anyhow!("unknown code"))?; macro_rules! single_string { ($variant:ident) => {{ @@ -286,9 +285,9 @@ impl OperatingSystemCommand { | ResetTektronixBackgroundColor | ResetHighlightColor | ResetTektronixCursorColor - | ResetHighlightForegroundColor => { - Self::parse_reset_dynamic_color_number((osc_code as u8).saturating_sub(100)) - } + | ResetHighlightForegroundColor => Self::parse_reset_dynamic_color_number( + p1str.parse::().unwrap().saturating_sub(100), + ), SetTextForegroundColor | SetTextBackgroundColor @@ -300,7 +299,7 @@ impl OperatingSystemCommand { | SetHighlightBackgroundColor | SetTektronixCursorColor | SetHighlightForegroundColor => { - Self::parse_change_dynamic_color_number(osc_code as u8, osc) + Self::parse_change_dynamic_color_number(p1str.parse::().unwrap(), osc) } osc_code => bail!("{:?} not impl", osc_code), @@ -308,50 +307,102 @@ impl OperatingSystemCommand { } } -#[derive(Debug, Clone, PartialEq, Eq, FromPrimitive)] -pub enum OperatingSystemCommandCode { - SetIconNameAndWindowTitle = 0, - SetIconName = 1, - SetWindowTitle = 2, - SetXWindowProperty = 3, - ChangeColorNumber = 4, - ChangeSpecialColorNumber = 5, +macro_rules! osc_entries { +($( + $( #[doc=$doc:expr] )* + $label:ident = $value:expr +),* $(,)?) => { +#[derive(Debug, Clone, PartialEq, Eq, FromPrimitive, Hash, Copy)] +pub enum OperatingSystemCommandCode { + $( + $( #[doc=$doc] )* + $label, + )* +} + +impl OscMap { + fn new() -> Self { + let mut code_to_variant = HashMap::new(); + let mut variant_to_code = HashMap::new(); + + use OperatingSystemCommandCode::*; + + $( + code_to_variant.insert($value, $label); + variant_to_code.insert($label, $value); + )* + + Self { + code_to_variant, + variant_to_code, + } + } +} + }; +} + +osc_entries!( + SetIconNameAndWindowTitle = "0", + SetIconName = "1", + SetWindowTitle = "2", + SetXWindowProperty = "3", + ChangeColorNumber = "4", + ChangeSpecialColorNumber = "5", /// iTerm2 - ChangeTitleTabColor = 6, - SetCurrentWorkingDirectory = 7, + ChangeTitleTabColor = "6", + SetCurrentWorkingDirectory = "7", /// See https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda - SetHyperlink = 8, + SetHyperlink = "8", /// iTerm2 - SystemNotification = 9, - SetTextForegroundColor = 10, - SetTextBackgroundColor = 11, - SetTextCursorColor = 12, - SetMouseForegroundColor = 13, - SetMouseBackgroundColor = 14, - SetTektronixForegroundColor = 15, - SetTektronixBackgroundColor = 16, - SetHighlightBackgroundColor = 17, - SetTektronixCursorColor = 18, - SetHighlightForegroundColor = 19, - SetLogFileName = 46, - SetFont = 50, - EmacsShell = 51, - ManipulateSelectionData = 52, - ResetColors = 104, - ResetSpecialColor = 105, - ResetTextForegroundColor = 110, - ResetTextBackgroundColor = 111, - ResetTextCursorColor = 112, - ResetMouseForegroundColor = 113, - ResetMouseBackgroundColor = 114, - ResetTektronixForegroundColor = 115, - ResetTektronixBackgroundColor = 116, - ResetHighlightColor = 117, - ResetTektronixCursorColor = 118, - ResetHighlightForegroundColor = 119, - RxvtProprietary = 777, - ITermProprietary = 1337, + SystemNotification = "9", + SetTextForegroundColor = "10", + SetTextBackgroundColor = "11", + SetTextCursorColor = "12", + SetMouseForegroundColor = "13", + SetMouseBackgroundColor = "14", + SetTektronixForegroundColor = "15", + SetTektronixBackgroundColor = "16", + SetHighlightBackgroundColor = "17", + SetTektronixCursorColor = "18", + SetHighlightForegroundColor = "19", + SetLogFileName = "46", + SetFont = "50", + EmacsShell = "51", + ManipulateSelectionData = "52", + ResetColors = "104", + ResetSpecialColor = "105", + ResetTextForegroundColor = "110", + ResetTextBackgroundColor = "111", + ResetTextCursorColor = "112", + ResetMouseForegroundColor = "113", + ResetMouseBackgroundColor = "114", + ResetTektronixForegroundColor = "115", + ResetTektronixBackgroundColor = "116", + ResetHighlightColor = "117", + ResetTektronixCursorColor = "118", + ResetHighlightForegroundColor = "119", + RxvtProprietary = "777", + ITermProprietary = "1337", +); + +struct OscMap { + code_to_variant: HashMap<&'static str, OperatingSystemCommandCode>, + variant_to_code: HashMap, +} + +lazy_static::lazy_static! { + static ref OSC_MAP: OscMap = OscMap::new(); +} + +impl OperatingSystemCommandCode { + fn from_code(code: &str) -> Option { + OSC_MAP.code_to_variant.get(code).copied() + } + + fn as_code(self) -> &'static str { + OSC_MAP.variant_to_code.get(&self).unwrap() + } } impl Display for OperatingSystemCommand { @@ -360,7 +411,12 @@ impl Display for OperatingSystemCommand { macro_rules! single_string { ($variant:ident, $s:expr) => { - write!(f, "{};{}", OperatingSystemCommandCode::$variant as u8, $s)? + write!( + f, + "{};{}", + OperatingSystemCommandCode::$variant.as_code(), + $s + )? }; };