From d6e3c3c07140c8fbb69ad4cc3b4369456458c613 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Mon, 6 Dec 2021 21:02:41 -0500 Subject: add Screen::errors --- CHANGELOG.md | 6 ++++++ src/screen.rs | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd60dab..a375680 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Added + +* `Screen::errors` to track the number of parsing errors seen so far + ## [0.14.0] - 2021-12-06 ### Changed diff --git a/src/screen.rs b/src/screen.rs index 558c41f..cec1888 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -78,6 +78,8 @@ pub struct Screen { audible_bell_count: usize, visual_bell_count: usize, + + errors: usize, } impl Screen { @@ -103,6 +105,8 @@ impl Screen { audible_bell_count: 0, visual_bell_count: 0, + + errors: 0, } } @@ -634,6 +638,16 @@ impl Screen { self.visual_bell_count } + /// Returns the number of parsing errors seen so far. + /// + /// Currently this only tracks invalid UTF-8 and control characters other + /// than `0x07`-`0x0f`. This can give an idea of whether the input stream + /// being fed to the parser is reasonable or not. + #[must_use] + pub fn errors(&self) -> usize { + self.errors + } + /// Returns whether the alternate screen is currently in use. #[must_use] pub fn alternate_screen(&self) -> bool { @@ -1017,6 +1031,7 @@ impl Screen { let icon_name = self.icon_name.clone(); let audible_bell_count = self.audible_bell_count; let visual_bell_count = self.visual_bell_count; + let errors = self.errors; *self = Self::new(self.grid.size(), self.grid.scrollback_len()); @@ -1024,6 +1039,7 @@ impl Screen { self.icon_name = icon_name; self.audible_bell_count = audible_bell_count; self.visual_bell_count = visual_bell_count; + self.errors = errors; } // ESC g @@ -1429,6 +1445,9 @@ impl Screen { impl vte::Perform for Screen { fn print(&mut self, c: char) { + if c == '\u{fffd}' || ('\u{80}'..'\u{a0}').contains(&c) { + self.errors = self.errors.saturating_add(1); + } self.text(c); } @@ -1441,7 +1460,11 @@ impl vte::Perform for Screen { 11 => self.vt(), 12 => self.ff(), 13 => self.cr(), + // we don't implement shift in/out alternate character sets, but + // it shouldn't count as an "error" + 14 | 15 => {} _ => { + self.errors = self.errors.saturating_add(1); log::debug!("unhandled control character: {}", b); } } -- cgit v1.2.3-54-g00ecf