From b307df6add3c8da266d117654414ca666c1b8fa8 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 21 Jun 2020 10:14:27 -0700 Subject: [PATCH] term+termwiz: handle OSC L and OSC l These are equivalent to OSC 2 and OSC 1 respectively. We now also respond to requests to fetch the window title --- term/src/terminalstate.rs | 12 ++++++- termwiz/src/escape/osc.rs | 67 +++++++++++++++++++++++++++++++++------ 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index d6f4fcd5f..c2d9f6388 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -1810,6 +1810,15 @@ impl TerminalState { write!(self.writer, "{}", CSI::Window(response)).ok(); } + Window::ReportWindowTitle => { + write!( + self.writer, + "{}", + OperatingSystemCommand::SetWindowTitleSun(self.title.clone()) + ) + .ok(); + } + Window::ChecksumRectangularArea { request_id, top, @@ -2777,10 +2786,11 @@ impl<'a> Performer<'a> { self.flush_print(); match osc { OperatingSystemCommand::SetIconNameAndWindowTitle(title) + | OperatingSystemCommand::SetWindowTitleSun(title) | OperatingSystemCommand::SetWindowTitle(title) => { self.title = title.clone(); } - OperatingSystemCommand::SetIconName(_) => {} + OperatingSystemCommand::SetIconNameSun(_) | OperatingSystemCommand::SetIconName(_) => {} OperatingSystemCommand::SetHyperlink(link) => { self.set_hyperlink(link); } diff --git a/termwiz/src/escape/osc.rs b/termwiz/src/escape/osc.rs index 06e9244ad..1313f9d2d 100644 --- a/termwiz/src/escape/osc.rs +++ b/termwiz/src/escape/osc.rs @@ -29,7 +29,9 @@ impl Display for ColorOrQuery { pub enum OperatingSystemCommand { SetIconNameAndWindowTitle(String), SetWindowTitle(String), + SetWindowTitleSun(String), SetIconName(String), + SetIconNameSun(String), SetHyperlink(Option), ClearSelection(Selection), QuerySelection(Selection), @@ -246,8 +248,24 @@ impl OperatingSystemCommand { fn internal_parse(osc: &[&[u8]]) -> anyhow::Result { ensure!(!osc.is_empty(), "no params"); let p1str = String::from_utf8_lossy(osc[0]); - let osc_code = - OperatingSystemCommandCode::from_code(&p1str).ok_or_else(|| anyhow!("unknown code"))?; + + if p1str.is_empty() { + bail!("zero length osc"); + } + + // Ugh, this is to handle "OSC ltitle" which is a legacyish + // OSC for encoding a window title change request. These days + // OSC 2 is preferred for this purpose, but we need to support + // generating and parsing the legacy form because it is the + // response for the CSI ReportWindowTitle. + // So, for non-numeric OSCs, we look up the prefix and use that. + // This only works if the non-numeric OSC code has length == 1. + let osc_code = if !p1str.chars().nth(0).unwrap().is_ascii_digit() && osc.len() == 1 { + OperatingSystemCommandCode::from_code(&p1str[0..1]) + } else { + OperatingSystemCommandCode::from_code(&p1str) + } + .ok_or_else(|| anyhow!("unknown code"))?; macro_rules! single_string { ($variant:ident) => {{ @@ -264,7 +282,14 @@ impl OperatingSystemCommand { match osc_code { SetIconNameAndWindowTitle => single_string!(SetIconNameAndWindowTitle), SetWindowTitle => single_string!(SetWindowTitle), + SetWindowTitleSun => Ok(OperatingSystemCommand::SetWindowTitleSun( + p1str[1..].to_owned(), + )), + SetIconName => single_string!(SetIconName), + SetIconNameSun => Ok(OperatingSystemCommand::SetIconNameSun( + p1str[1..].to_owned(), + )), SetHyperlink => Ok(OperatingSystemCommand::SetHyperlink(Hyperlink::parse(osc)?)), ManipulateSelectionData => Self::parse_selection(osc), SystemNotification => single_string!(SystemNotification), @@ -384,6 +409,11 @@ osc_entries!( ResetHighlightForegroundColor = "119", RxvtProprietary = "777", ITermProprietary = "1337", + /// Here the "Sun" suffix comes from the table in + /// + /// that lays out various window related escape sequences. + SetWindowTitleSun = "l", + SetIconNameSun = "L", ); struct OscMap { @@ -410,21 +440,31 @@ impl Display for OperatingSystemCommand { write!(f, "\x1b]")?; macro_rules! single_string { - ($variant:ident, $s:expr) => { - write!( - f, - "{};{}", - OperatingSystemCommandCode::$variant.as_code(), - $s - )? - }; + ($variant:ident, $s:expr) => {{ + let code = OperatingSystemCommandCode::$variant.as_code(); + match OperatingSystemCommandCode::$variant { + OperatingSystemCommandCode::SetWindowTitleSun + | OperatingSystemCommandCode::SetIconNameSun => { + // For the legacy sun terminals, the `l` and `L` OSCs are + // not separated by `;`. + write!(f, "{}{}", code, $s)?; + } + _ => { + // In the common case, the OSC is numeric and is separated + // from the rest of the string + write!(f, "{};{}", code, $s)?; + } + } + }}; }; use self::OperatingSystemCommand::*; match self { SetIconNameAndWindowTitle(title) => single_string!(SetIconNameAndWindowTitle, title), SetWindowTitle(title) => single_string!(SetWindowTitle, title), + SetWindowTitleSun(title) => single_string!(SetWindowTitleSun, title), SetIconName(title) => single_string!(SetIconName, title), + SetIconNameSun(title) => single_string!(SetIconNameSun, title), SetHyperlink(Some(link)) => link.fmt(f)?, SetHyperlink(None) => write!(f, "8;;")?, Unspecified(v) => { @@ -918,6 +958,13 @@ mod test { parse(&["0", "1", "2"], "\x1b]0;1;2\x1b\\"), OperatingSystemCommand::Unspecified(vec![b"0".to_vec(), b"1".to_vec(), b"2".to_vec()]) ); + + // parsing legacy sun OSC; why bother? This format is used in response + // to the CSI ReportWindowTitle sequence + assert_eq!( + parse(&["lhello"], "\x1b]lhello\x1b\\"), + OperatingSystemCommand::SetWindowTitleSun("hello".into()) + ); } #[test]