diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-03-09 01:58:27 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-03-09 01:58:27 -0500 |
commit | f8555701c71847a953bc21c602b6a070b757a107 (patch) | |
tree | 57238e2402d9e5e0ac171f0c297a88e40d8092a5 | |
parent | c9b957bcdfcd1cedbd8a1f3c5e16d1e4382b54c2 (diff) | |
download | vt100-rust-f8555701c71847a953bc21c602b6a070b757a107.tar.gz vt100-rust-f8555701c71847a953bc21c602b6a070b757a107.zip |
add Screen::state_formatted and Screen::state_diff
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | src/screen.rs | 26 | ||||
-rw-r--r-- | tests/helpers/mod.rs | 2 |
3 files changed, 32 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index fda293f..82507dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Added + +* `Screen::state_formatted` and `Screen::state_diff` convenience wrappers + ### Fixed * `Screen::attributes_formatted` now correctly resets previously set attributes diff --git a/src/screen.rs b/src/screen.rs index 9affeef..300e05d 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -226,6 +226,32 @@ impl Screen { } } + /// Return escape codes sufficient to reproduce the entire contents of the + /// current terminal state. This is a convenience wrapper around + /// `contents_formatted`, `input_mode_formatted`, and `title_formatted`. + #[must_use] + pub fn state_formatted(&self) -> Vec<u8> { + let mut contents = vec![]; + self.write_contents_formatted(&mut contents); + self.write_input_mode_formatted(&mut contents); + self.write_title_formatted(&mut contents); + contents + } + + /// Return escape codes sufficient to turn the terminal state of the + /// screen `prev` into the current terminal state. This is a convenience + /// wrapper around `contents_diff`, `input_mode_diff`, `title_diff`, and + /// `bells_diff`. + #[must_use] + pub fn state_diff(&self, prev: &Self) -> Vec<u8> { + let mut contents = vec![]; + self.write_contents_diff(&mut contents, prev); + self.write_input_mode_diff(&mut contents, prev); + self.write_title_diff(&mut contents, prev); + self.write_bells_diff(&mut contents, prev); + contents + } + /// Returns the formatted visible contents of the terminal. /// /// Formatting information will be included inline as terminal escape diff --git a/tests/helpers/mod.rs b/tests/helpers/mod.rs index 8ec0ac3..bbf1114 100644 --- a/tests/helpers/mod.rs +++ b/tests/helpers/mod.rs @@ -86,6 +86,7 @@ pub fn contents_formatted_reproduces_screen(screen: &vt100::Screen) -> bool { let mut new_input = screen.contents_formatted(); new_input.extend(screen.input_mode_formatted()); new_input.extend(screen.title_formatted()); + assert_eq!(new_input, screen.state_formatted()); new_input.extend(screen.bells_diff(&empty_screen)); let mut new_parser = vt100::Parser::default(); new_parser.process(&new_input); @@ -123,6 +124,7 @@ pub fn contents_diff_reproduces_state_from_screens( diff_input.extend(screen.input_mode_diff(&prev_screen)); diff_input.extend(screen.title_diff(&prev_screen)); diff_input.extend(screen.bells_diff(&prev_screen)); + assert_eq!(diff_input, screen.state_diff(&prev_screen)); let mut diff_prev_input = prev_screen.contents_formatted(); diff_prev_input.extend(screen.input_mode_formatted()); |