diff --git a/termwiz/src/escape/csi.rs b/termwiz/src/escape/csi.rs index 3cd9fb072..482f75bcd 100644 --- a/termwiz/src/escape/csi.rs +++ b/termwiz/src/escape/csi.rs @@ -2446,7 +2446,10 @@ impl<'a> CSIParser<'a> { } else { for p in params { match p { - CsiParam::P(b';') | CsiParam::P(b':') | CsiParam::Integer(_) => {} + CsiParam::P(b';') + | CsiParam::P(b':') + | CsiParam::P(b'?') + | CsiParam::Integer(_) => {} _ => return Err(()), } } @@ -2457,12 +2460,45 @@ impl<'a> CSIParser<'a> { Ok(self.advance_by(1, params, $t)) }; } + // Consume two parameters and return the parsed result + macro_rules! two { + ($t:expr) => { + Ok(self.advance_by(2, params, $t)) + }; + } match ¶ms[0] { CsiParam::P(b';') => { // Starting with an empty item is equivalent to a reset self.advance_by(1, params, Ok(Sgr::Reset)) } + + // There are a small number of DEC private SGR parameters that + // have equivalents in the normal SGR space. + // We're simply inlining recognizing them here, and mapping them + // to those SGR equivalents. That makes parsing "lossy" in the + // sense that the original sequence is lost, but semantically, + // the result is the same. + // These codes are taken from the "SGR" section of + // "Digital ANSI-Compliant Printing Protocol + // Level 2 Programming Reference Manual" + // on page 7-78. + // + CsiParam::P(b'?') if params.len() > 1 => match ¶ms[1] { + CsiParam::Integer(i) => match FromPrimitive::from_i64(*i) { + None => Err(()), + Some(code) => match code { + 0 => two!(Sgr::Reset), + 4 => two!(Sgr::VerticalAlign(VerticalAlign::SuperScript)), + 5 => two!(Sgr::VerticalAlign(VerticalAlign::SubScript)), + 6 => two!(Sgr::Overline(true)), + 24 => two!(Sgr::VerticalAlign(VerticalAlign::BaseLine)), + 26 => two!(Sgr::Overline(false)), + _ => Err(()), + }, + }, + _ => Err(()), + }, CsiParam::P(_) => Err(()), CsiParam::Integer(i) => match FromPrimitive::from_i64(*i) { None => Err(()), diff --git a/termwiz/src/escape/parser/mod.rs b/termwiz/src/escape/parser/mod.rs index d297a92f6..693d368f1 100644 --- a/termwiz/src/escape/parser/mod.rs +++ b/termwiz/src/escape/parser/mod.rs @@ -338,7 +338,7 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> { #[cfg(test)] mod test { use super::*; - use crate::cell::{Intensity, Underline}; + use crate::cell::{Intensity, Underline, VerticalAlign}; use crate::color::ColorSpec; use crate::escape::csi::{ CharacterPath, DecPrivateMode, DecPrivateModeCode, Device, Mode, Sgr, Window, XtSmGraphics, @@ -942,6 +942,40 @@ mod test { ); } + #[test] + fn dec_private_sgr() { + assert_eq!( + parse_as("\x1b[?0m", "\x1b[0m"), + vec![Action::CSI(CSI::Sgr(Sgr::Reset))] + ); + assert_eq!( + parse_as("\x1b[?4m", "\x1b[73m"), + vec![Action::CSI(CSI::Sgr(Sgr::VerticalAlign( + VerticalAlign::SuperScript + )))] + ); + assert_eq!( + parse_as("\x1b[?5m", "\x1b[74m"), + vec![Action::CSI(CSI::Sgr(Sgr::VerticalAlign( + VerticalAlign::SubScript + )))] + ); + assert_eq!( + parse_as("\x1b[?24m", "\x1b[75m"), + vec![Action::CSI(CSI::Sgr(Sgr::VerticalAlign( + VerticalAlign::BaseLine + )))] + ); + assert_eq!( + parse_as("\x1b[?6m", "\x1b[53m"), + vec![Action::CSI(CSI::Sgr(Sgr::Overline(true)))] + ); + assert_eq!( + parse_as("\x1b[?26m", "\x1b[55m"), + vec![Action::CSI(CSI::Sgr(Sgr::Overline(false)))] + ); + } + #[test] fn decset() { assert_eq!(