From 41e009f9b7b46d335d41b365e41f9ad57d571ecb Mon Sep 17 00:00:00 2001 From: Ogulcan Celik Date: Tue, 31 Mar 2026 20:49:05 +0300 Subject: fix alt-screen scrollback handling --- vendor/vt100/src/parser.rs | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 vendor/vt100/src/parser.rs (limited to 'vendor/vt100/src/parser.rs') diff --git a/vendor/vt100/src/parser.rs b/vendor/vt100/src/parser.rs new file mode 100644 index 00000000..c81495b8 --- /dev/null +++ b/vendor/vt100/src/parser.rs @@ -0,0 +1,96 @@ +/// A parser for terminal output which produces an in-memory representation of +/// the terminal contents. +pub struct Parser { + parser: vte::Parser, + screen: crate::perform::WrappedScreen, +} + +impl Parser { + /// Creates a new terminal parser of the given size and with the given + /// amount of scrollback. + #[must_use] + pub fn new(rows: u16, cols: u16, scrollback_len: usize) -> Self { + Self { + parser: vte::Parser::new(), + screen: crate::perform::WrappedScreen::new( + rows, + cols, + scrollback_len, + ), + } + } +} + +impl Parser { + /// Creates a new terminal parser of the given size and with the given + /// amount of scrollback. Terminal events will be reported via method + /// calls on the provided [`Callbacks`](crate::callbacks::Callbacks) + /// implementation. + pub fn new_with_callbacks( + rows: u16, + cols: u16, + scrollback_len: usize, + callbacks: CB, + ) -> Self { + Self { + parser: vte::Parser::new(), + screen: crate::perform::WrappedScreen::new_with_callbacks( + rows, + cols, + scrollback_len, + callbacks, + ), + } + } + + /// Processes the contents of the given byte string, and updates the + /// in-memory terminal state. + pub fn process(&mut self, bytes: &[u8]) { + self.parser.advance(&mut self.screen, bytes); + } + + /// Returns a reference to a [`Screen`](crate::Screen) object containing + /// the terminal state. + #[must_use] + pub fn screen(&self) -> &crate::Screen { + &self.screen.screen + } + + /// Returns a mutable reference to a [`Screen`](crate::Screen) object + /// containing the terminal state. + #[must_use] + pub fn screen_mut(&mut self) -> &mut crate::Screen { + &mut self.screen.screen + } + + /// Returns a reference to the [`Callbacks`](crate::callbacks::Callbacks) + /// state object passed into the constructor. + pub fn callbacks(&self) -> &CB { + &self.screen.callbacks + } + + /// Returns a mutable reference to the + /// [`Callbacks`](crate::callbacks::Callbacks) state object passed into + /// the constructor. + pub fn callbacks_mut(&mut self) -> &mut CB { + &mut self.screen.callbacks + } +} + +impl Default for Parser { + /// Returns a parser with dimensions 80x24 and no scrollback. + fn default() -> Self { + Self::new(24, 80, 0) + } +} + +impl std::io::Write for Parser { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.process(buf); + Ok(buf.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } +} -- cgit v1.2.3