From 7021de793f9945789276db2dd1006aac64f24495 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 9 Mar 2023 22:30:01 -0500 Subject: reorganize a bit --- src/lib.rs | 1 - src/parser.rs | 7 +++-- src/perform.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/state.rs | 78 ------------------------------------------------------- 4 files changed, 86 insertions(+), 81 deletions(-) delete mode 100644 src/state.rs diff --git a/src/lib.rs b/src/lib.rs index 1c5cadd..0c7e7da 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,7 +54,6 @@ mod parser; mod perform; mod row; mod screen; -mod state; mod term; pub use attrs::Color; diff --git a/src/parser.rs b/src/parser.rs index 77c95c3..2844e17 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -35,9 +35,12 @@ impl Parser { bytes: &[u8], callbacks: &mut impl crate::callbacks::Callbacks, ) { - let mut state = crate::state::State::new(&mut self.screen, callbacks); + let mut screen = crate::perform::WrappedScreenWithCallbacks::new( + &mut self.screen, + callbacks, + ); for byte in bytes { - self.parser.advance(&mut state, *byte); + self.parser.advance(&mut screen, *byte); } } diff --git a/src/perform.rs b/src/perform.rs index 1c172f1..c3c2e70 100644 --- a/src/perform.rs +++ b/src/perform.rs @@ -220,3 +220,84 @@ fn osc_param_str(params: &[&[u8]]) -> String { .collect(); strs.join(" ; ") } + +pub struct WrappedScreenWithCallbacks<'a, T: crate::callbacks::Callbacks> { + screen: &'a mut crate::perform::WrappedScreen, + callbacks: &'a mut T, +} + +impl<'a, T: crate::callbacks::Callbacks> WrappedScreenWithCallbacks<'a, T> { + pub fn new( + screen: &'a mut crate::perform::WrappedScreen, + callbacks: &'a mut T, + ) -> Self { + Self { screen, callbacks } + } +} + +impl<'a, T: crate::callbacks::Callbacks> vte::Perform + for WrappedScreenWithCallbacks<'a, T> +{ + fn print(&mut self, c: char) { + if c == '\u{fffd}' || ('\u{80}'..'\u{a0}').contains(&c) { + self.callbacks.error(&mut self.screen.0); + } + self.screen.print(c); + } + + fn execute(&mut self, b: u8) { + match b { + 7 => self.callbacks.audible_bell(&mut self.screen.0), + 8..=15 => {} + _ => { + self.callbacks.error(&mut self.screen.0); + } + } + self.screen.execute(b); + } + + fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, b: u8) { + if intermediates.is_empty() && b == b'g' { + self.callbacks.visual_bell(&mut self.screen.0); + } + self.screen.esc_dispatch(intermediates, ignore, b); + } + + fn csi_dispatch( + &mut self, + params: &vte::Params, + intermediates: &[u8], + ignore: bool, + c: char, + ) { + if intermediates.first().is_none() && c == 't' { + let mut iter = params.iter(); + let op = iter.next().and_then(|x| x.first().copied()); + if op == Some(8) { + let (screen_rows, screen_cols) = self.screen.0.size(); + let rows = iter.next().map_or(screen_rows, |x| { + *x.first().unwrap_or(&screen_rows) + }); + let cols = iter.next().map_or(screen_cols, |x| { + *x.first().unwrap_or(&screen_cols) + }); + self.callbacks.resize(&mut self.screen.0, (rows, cols)); + } + } + self.screen.csi_dispatch(params, intermediates, ignore, c); + } + + fn osc_dispatch(&mut self, params: &[&[u8]], bel_terminated: bool) { + self.screen.osc_dispatch(params, bel_terminated); + } + + fn hook( + &mut self, + params: &vte::Params, + intermediates: &[u8], + ignore: bool, + action: char, + ) { + self.screen.hook(params, intermediates, ignore, action); + } +} diff --git a/src/state.rs b/src/state.rs deleted file mode 100644 index 1268574..0000000 --- a/src/state.rs +++ /dev/null @@ -1,78 +0,0 @@ -pub struct State<'a, T: crate::callbacks::Callbacks> { - screen: &'a mut crate::perform::WrappedScreen, - callbacks: &'a mut T, -} - -impl<'a, T: crate::callbacks::Callbacks> State<'a, T> { - pub fn new( - screen: &'a mut crate::perform::WrappedScreen, - callbacks: &'a mut T, - ) -> Self { - Self { screen, callbacks } - } -} - -impl<'a, T: crate::callbacks::Callbacks> vte::Perform for State<'a, T> { - fn print(&mut self, c: char) { - if c == '\u{fffd}' || ('\u{80}'..'\u{a0}').contains(&c) { - self.callbacks.error(&mut self.screen.0); - } - self.screen.print(c); - } - - fn execute(&mut self, b: u8) { - match b { - 7 => self.callbacks.audible_bell(&mut self.screen.0), - 8..=15 => {} - _ => { - self.callbacks.error(&mut self.screen.0); - } - } - self.screen.execute(b); - } - - fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, b: u8) { - if intermediates.is_empty() && b == b'g' { - self.callbacks.visual_bell(&mut self.screen.0); - } - self.screen.esc_dispatch(intermediates, ignore, b); - } - - fn csi_dispatch( - &mut self, - params: &vte::Params, - intermediates: &[u8], - ignore: bool, - c: char, - ) { - if intermediates.first().is_none() && c == 't' { - let mut iter = params.iter(); - let op = iter.next().and_then(|x| x.first().copied()); - if op == Some(8) { - let (screen_rows, screen_cols) = self.screen.0.size(); - let rows = iter.next().map_or(screen_rows, |x| { - *x.first().unwrap_or(&screen_rows) - }); - let cols = iter.next().map_or(screen_cols, |x| { - *x.first().unwrap_or(&screen_cols) - }); - self.callbacks.resize(&mut self.screen.0, (rows, cols)); - } - } - self.screen.csi_dispatch(params, intermediates, ignore, c); - } - - fn osc_dispatch(&mut self, params: &[&[u8]], bel_terminated: bool) { - self.screen.osc_dispatch(params, bel_terminated); - } - - fn hook( - &mut self, - params: &vte::Params, - intermediates: &[u8], - ignore: bool, - action: char, - ) { - self.screen.hook(params, intermediates, ignore, action); - } -} -- cgit v1.2.3-54-g00ecf