From 915dd9fbf8d6acf5e56828e9ce4352e255ee5ad5 Mon Sep 17 00:00:00 2001 From: "Ethan P." Date: Sun, 11 Feb 2024 19:27:04 -0800 Subject: [PATCH 1/2] Fix incorrect categorization of SGR sequences Specifically, prevent other attributes from leaking into the bold/dim/italic/underline attributes, and ensure that bright backgrounds are put into the background attribute instead of the foreground attribute. --- CHANGELOG.md | 1 + src/vscreen.rs | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6e13469..f0d81642 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Fix long file name wrapping in header, see #2835 (@FilipRazek) - Fix `NO_COLOR` support, see #2767 (@acuteenvy) - Fix handling of inputs with OSC ANSI escape sequences, see #2541 and #2544 (@eth-p) +- Fix handling of inputs with combined ANSI color and attribute sequences, see #2185 and #2856 (@eth-p) ## Other diff --git a/src/vscreen.rs b/src/vscreen.rs index c902d42b..f883e655 100644 --- a/src/vscreen.rs +++ b/src/vscreen.rs @@ -169,10 +169,10 @@ impl Attributes { while let Some(p) = iter.next() { match p { 0 => self.sgr_reset(), - 1 => self.bold = format!("\x1B[{}m", parameters), - 2 => self.dim = format!("\x1B[{}m", parameters), - 3 => self.italic = format!("\x1B[{}m", parameters), - 4 => self.underline = format!("\x1B[{}m", parameters), + 1 => self.bold = "\x1B[1m".to_owned(), + 2 => self.dim = "\x1B[2m".to_owned(), + 3 => self.italic = "\x1B[3m".to_owned(), + 4 => self.underline = "\x1B[4m".to_owned(), 23 => self.italic.clear(), 24 => self.underline.clear(), 22 => { @@ -183,7 +183,7 @@ impl Attributes { 40..=49 => self.background = Self::parse_color(p, &mut iter), 58..=59 => self.underlined = Self::parse_color(p, &mut iter), 90..=97 => self.foreground = Self::parse_color(p, &mut iter), - 100..=107 => self.foreground = Self::parse_color(p, &mut iter), + 100..=107 => self.background = Self::parse_color(p, &mut iter), _ => { // Unsupported SGR sequence. // Be compatible and pretend one just wasn't was provided. From 84d80eebd0b224cd799282121e817fa5f0d57d5e Mon Sep 17 00:00:00 2001 From: "Ethan P." Date: Sun, 11 Feb 2024 19:29:09 -0800 Subject: [PATCH 2/2] Test for correct categorization of SGR sequences This adds a regression test for the fix in the previous commit. --- src/vscreen.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/vscreen.rs b/src/vscreen.rs index f883e655..78f6ad4e 100644 --- a/src/vscreen.rs +++ b/src/vscreen.rs @@ -890,4 +890,37 @@ mod tests { ); assert_eq!(iter.next(), None); } + + #[test] + fn test_sgr_attributes_do_not_leak_into_wrong_field() { + let mut attrs = crate::vscreen::Attributes::new(); + + // Bold, Dim, Italic, Underline, Foreground, Background + attrs.update(EscapeSequence::CSI { + raw_sequence: "\x1B[1;2;3;4;31;41m", + parameters: "1;2;3;4;31;41", + intermediates: "", + final_byte: "m", + }); + + assert_eq!(attrs.bold, "\x1B[1m"); + assert_eq!(attrs.dim, "\x1B[2m"); + assert_eq!(attrs.italic, "\x1B[3m"); + assert_eq!(attrs.underline, "\x1B[4m"); + assert_eq!(attrs.foreground, "\x1B[31m"); + assert_eq!(attrs.background, "\x1B[41m"); + + // Bold, Bright Foreground, Bright Background + attrs.sgr_reset(); + attrs.update(EscapeSequence::CSI { + raw_sequence: "\x1B[1;94;103m", + parameters: "1;94;103", + intermediates: "", + final_byte: "m", + }); + + assert_eq!(attrs.bold, "\x1B[1m"); + assert_eq!(attrs.foreground, "\x1B[94m"); + assert_eq!(attrs.background, "\x1B[103m"); + } }