From c9b957bcdfcd1cedbd8a1f3c5e16d1e4382b54c2 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 9 Mar 2021 01:46:51 -0500 Subject: fix attributes_formatted, remove attributes_diff also improve the documentation and add more tests --- CHANGELOG.md | 12 ++++++++++++ src/screen.rs | 21 +++++++-------------- tests/attr.rs | 12 ++++++++++++ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cbc4e8..fda293f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## [Unreleased] + +### Fixed + +* `Screen::attributes_formatted` now correctly resets previously set attributes + where necessary + +### Removed + +* Removed `Screen::attributes_diff`, since I can't actually think of any + situation where it does a thing that makes sense. + ## [0.11.1] - 2021-03-07 ### Changed diff --git a/src/screen.rs b/src/screen.rs index cb6b970..9affeef 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -485,6 +485,12 @@ impl Screen { /// * italic /// * underline /// * inverse + /// + /// This is not typically necessary, since `contents_formatted` will leave + /// the current active drawing attributes in the correct state, but this + /// can be useful in the case of drawing additional things on top of a + /// terminal output, since you will need to restore the terminal state + /// without the terminal contents necessarily being the same. #[must_use] pub fn attributes_formatted(&self) -> Vec { let mut contents = vec![]; @@ -493,26 +499,13 @@ impl Screen { } fn write_attributes_formatted(&self, contents: &mut Vec) { + crate::term::ClearAttrs::default().write_buf(contents); self.attrs.write_escape_code_diff( contents, &crate::attrs::Attrs::default(), ); } - /// Returns terminal escape sequences sufficient to change the previous - /// terminal's drawing attributes to the drawing attributes enabled in the - /// current terminal. - #[must_use] - pub fn attributes_diff(&self, prev: &Self) -> Vec { - let mut contents = vec![]; - self.write_attributes_diff(&mut contents, prev); - contents - } - - fn write_attributes_diff(&self, contents: &mut Vec, prev: &Self) { - self.attrs.write_escape_code_diff(contents, &prev.attrs); - } - /// Returns the `Cell` object at the given location in the terminal, if it /// exists. #[must_use] diff --git a/tests/attr.rs b/tests/attr.rs index 64f4bd4..34ac7af 100644 --- a/tests/attr.rs +++ b/tests/attr.rs @@ -9,3 +9,15 @@ fn colors() { fn attrs() { helpers::fixture("attrs"); } + +#[test] +fn attributes_formatted() { + let mut parser = vt100::Parser::default(); + assert_eq!(parser.screen().attributes_formatted(), b"\x1b[m"); + parser.process(b"\x1b[32mfoo\x1b[41mbar\x1b[33mbaz"); + assert_eq!(parser.screen().attributes_formatted(), b"\x1b[m\x1b[33;41m"); + parser.process(b"\x1b[1m\x1b[39m"); + assert_eq!(parser.screen().attributes_formatted(), b"\x1b[m\x1b[41;1m"); + parser.process(b"\x1b[m"); + assert_eq!(parser.screen().attributes_formatted(), b"\x1b[m"); +} -- cgit v1.2.3