diff --git a/CHANGELOG.md b/CHANGELOG.md index 60a2053..2684eb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,12 @@ reads that marker. program that set its modes in one sequence could never have an empty record. A list with a mode nobody honours, such as `CSI ? 25 ; 12 h`, is still listed. (#829) +- `Unsupported::overflow` counts distinct shapes, as its rustdoc says. Past + the 32 retained it added one for every report, so a shape sent once a + frame drove it into the thousands, and an SGR or DEC mode list vt100 + declines parameter by parameter counted once per parameter. It now + remembers up to 256 shapes past the 32 by hash and counts each once; + past those, a shape counts each time it is sent. (#828) - Both published crates, `termlens` and `termlens-cli`, ship `LICENSE-MIT` and `LICENSE-APACHE`. They are `MIT OR Apache-2.0`, and both licences ask for their text in every copy, but the texts sat only at the repository diff --git a/crates/termlens/src/emu/unhandled.rs b/crates/termlens/src/emu/unhandled.rs index e4eb174..ffc6b33 100644 --- a/crates/termlens/src/emu/unhandled.rs +++ b/crates/termlens/src/emu/unhandled.rs @@ -26,6 +26,8 @@ //! main screen's history, which is the one thing its history cannot tell //! afterwards: at the retention bound the length stops changing (#671). +use std::collections::HashSet; +use std::hash::{Hash, Hasher}; use std::sync::Arc; use vt100::Callbacks; @@ -33,6 +35,11 @@ use vt100::Callbacks; /// Distinct shapes kept before the record only counts. pub(super) const MAX_UNSUPPORTED: usize = 32; +/// Distinct shapes past [`MAX_UNSUPPORTED`] the record remembers by hash, +/// so the overflow counts each of them once however often it is reported +/// (#828). Past this many, a shape it does not remember counts each time. +pub(super) const MAX_OVERFLOW_TRACKED: usize = 256; + /// Bytes of an OSC string's content an entry keeps: enough for the command /// number and its first parameters, which is what names the sequence, and /// an entry ends in `…` where it was cut. @@ -54,6 +61,9 @@ pub(super) struct Unhandled { /// refcount rather than a copy. shared: Arc<[Arc]>, overflow: u64, + /// Hashes of the shapes [`overflow`](Self::overflow) has counted, at + /// most [`MAX_OVERFLOW_TRACKED`] of them. + overflow_seen: HashSet, visual_bells: u64, /// Rows dropped off the front of the main screen's history since the /// emulator last took the count — see [`take_evicted`](Self::take_evicted). @@ -84,6 +94,7 @@ impl Default for Unhandled { seen: Vec::new(), shared: Arc::from([] as [Arc; 0]), overflow: 0, + overflow_seen: HashSet::new(), visual_bells: 0, evicted: 0, icon_name: false, @@ -102,7 +113,8 @@ impl Unhandled { Arc::clone(&self.shared) } - /// Distinct shapes beyond [`MAX_UNSUPPORTED`], counted only. + /// Distinct shapes beyond [`MAX_UNSUPPORTED`], counted only: exact up + /// to [`MAX_OVERFLOW_TRACKED`] of them. pub(super) fn overflow(&self) -> u64 { self.overflow } @@ -196,6 +208,19 @@ impl Unhandled { return; } if self.seen.len() >= MAX_UNSUPPORTED { + // vt100 reports an SGR or a DEC mode list once for each + // parameter it declines, and a program may send one shape every + // frame: count a shape once, as the retained ones are listed + // once. + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + shape.hash(&mut hasher); + let hash = hasher.finish(); + if self.overflow_seen.contains(&hash) { + return; + } + if self.overflow_seen.len() < MAX_OVERFLOW_TRACKED { + self.overflow_seen.insert(hash); + } self.overflow = self.overflow.saturating_add(1); return; } @@ -1034,6 +1059,51 @@ mod tests { assert_eq!(parser.callbacks().overflow(), 5); } + /// The overflow counts distinct shapes, as its rustdoc says: a shape + /// reported again, or reported once per declined parameter, counts + /// once (#828). + #[test] + fn the_overflow_counts_each_shape_once() { + let mut stream = Vec::new(); + for mode in 20..(20 + MAX_UNSUPPORTED as u16) { + stream.extend_from_slice(format!("\x1b[{mode}h").as_bytes()); + } + stream.extend_from_slice(b"\x1b[99h\x1b[99h\x1b[99h\x1b[53;59m"); + let parser = fed(&stream); + assert_eq!(parser.callbacks().shapes().len(), MAX_UNSUPPORTED); + assert_eq!(parser.callbacks().overflow(), 2); + } + + /// Past the shapes it remembers, a shape counts each time, so the + /// memory the record keeps stays bounded. + #[test] + fn the_overflow_remembers_a_bounded_number_of_shapes() { + let mut stream = Vec::new(); + let distinct = MAX_UNSUPPORTED + MAX_OVERFLOW_TRACKED + 1; + for mode in 0..distinct { + stream.extend_from_slice(format!("\x1b[{}h", 1000 + mode).as_bytes()); + } + let parser = fed(&stream); + assert_eq!(parser.callbacks().overflow_seen.len(), MAX_OVERFLOW_TRACKED); + assert_eq!( + parser.callbacks().overflow(), + (MAX_OVERFLOW_TRACKED + 1) as u64 + ); + // A shape remembered still counts once; one past the bound, again. + let mut parser = parser; + parser.process(b"\x1b[1032h"); + assert_eq!( + parser.callbacks().overflow(), + (MAX_OVERFLOW_TRACKED + 1) as u64 + ); + let last = format!("\x1b[{}h", 1000 + distinct - 1); + parser.process(last.as_bytes()); + assert_eq!( + parser.callbacks().overflow(), + (MAX_OVERFLOW_TRACKED + 2) as u64 + ); + } + #[test] fn a_visual_bell_and_a_resize_request_are_seen() { let parser = fed(b"\x1bg\x1b[8;10;40t"); diff --git a/crates/termlens/src/screen.rs b/crates/termlens/src/screen.rs index 5e748e0..9bed282 100644 --- a/crates/termlens/src/screen.rs +++ b/crates/termlens/src/screen.rs @@ -291,7 +291,8 @@ pub enum CursorShape { /// /// Distinct sequence shapes, first seen first, in the form the timeout /// messages use (`^[[20h` for `CSI 20 h`), at most 32 retained; -/// [`overflow`](Self::overflow) counts the distinct shapes beyond those, so +/// [`overflow`](Self::overflow) counts the distinct shapes beyond those (each +/// once, however often it is sent, exact up to 256 of them), so /// a stream that invents thousands cannot grow a snapshot and "nothing was /// dropped" is one check, [`is_empty`](Self::is_empty). /// @@ -334,7 +335,9 @@ impl<'a> Unsupported<'a> { self.retained.iter().any(|shape| &**shape == sequence) } - /// Distinct shapes seen beyond the 32 retained, counted only. + /// Distinct shapes seen beyond the 32 retained, counted only. A shape + /// counts once however often it is sent, for the first 256 such shapes; + /// past those, a shape not among them counts each time it is sent. #[must_use] pub fn overflow(&self) -> u64 { self.overflow diff --git a/crates/termlens/tests/unsupported.rs b/crates/termlens/tests/unsupported.rs index 523c48b..a48715f 100644 --- a/crates/termlens/tests/unsupported.rs +++ b/crates/termlens/tests/unsupported.rs @@ -135,6 +135,27 @@ fn a_list_of_honoured_modes_is_not_listed() -> termlens::Result<()> { Ok(()) } +/// The overflow counts distinct shapes: past the 32 retained, a shape sent +/// three times counts once, and so does one SGR with two parameters nobody +/// models, which vt100 reports once for each (#828). +#[test] +#[cfg_attr( + windows, + ignore = "ConPTY renders what it implements and drops the rest before termlens sees a byte, so the record there is the console's, not the application's (#149)" +)] +fn the_overflow_counts_distinct_shapes() -> termlens::Result<()> { + let mut stream: String = (20..52u16).map(|mode| format!(r"\e[{mode}h")).collect(); + stream.push_str(r"\e[99h\e[99h\e[99h\e[53;59m"); + let mut t = emit(&["--raw", &stream, "DONE", "--wait"])?; + t.wait_until(|s| s.contains("DONE"))?; + let s = t.screen(); + assert_eq!(s.unsupported().len(), 32, "{s}"); + assert_eq!(s.unsupported().overflow(), 2, "{s}"); + t.send(Key::Enter)?; + assert!(t.wait_exit()?.success()); + Ok(()) +} + /// A visual bell is a different event from an audible one, and a request to /// resize the window is recorded rather than honoured. #[test]