From f88153f62b024d1c2ef82c5ac7ae9002e4fe2967 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 27 Oct 2019 13:33:47 -0400 Subject: add terminal resizing functionality to keep the process's pty size in sync with the size of the user's terminal --- src/resize.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/resize.rs (limited to 'src/resize.rs') diff --git a/src/resize.rs b/src/resize.rs new file mode 100644 index 0000000..5b59f9e --- /dev/null +++ b/src/resize.rs @@ -0,0 +1,78 @@ +use futures::future::Future as _; +use futures::stream::Stream as _; +use snafu::futures01::StreamExt as _; + +pub struct ResizingProcess { + process: crate::process::Process, + resizer: Box< + dyn futures::stream::Stream< + Item = (u16, u16), + Error = crate::error::Error, + > + Send, + >, +} + +impl ResizingProcess { + pub fn new(process: crate::process::Process) -> Self { + Self { + process, + resizer: Box::new( + tokio_terminal_resize::resizes() + .flatten_stream() + .context(crate::error::Resize), + ), + } + } + + pub fn input(&mut self) -> &mut R { + self.process.input() + } +} + +impl ResizingProcess { + const POLL_FNS: + &'static [&'static dyn for<'a> Fn( + &'a mut Self, + ) + -> component_future::Poll< + Option, + crate::error::Error, + >] = &[&Self::poll_resize, &Self::poll_process]; + + fn poll_resize( + &mut self, + ) -> component_future::Poll< + Option, + crate::error::Error, + > { + let (rows, cols) = + component_future::try_ready!(self.resizer.poll()).unwrap(); + self.process.resize(rows, cols); + Ok(component_future::Async::Ready(Some( + crate::process::Event::Resize { size: (rows, cols) }, + ))) + } + + fn poll_process( + &mut self, + ) -> component_future::Poll< + Option, + crate::error::Error, + > { + Ok(component_future::Async::Ready( + component_future::try_ready!(self.process.poll()), + )) + } +} + +#[must_use = "streams do nothing unless polled"] +impl futures::stream::Stream + for ResizingProcess +{ + type Item = crate::process::Event; + type Error = crate::error::Error; + + fn poll(&mut self) -> futures::Poll, Self::Error> { + component_future::poll_stream(self, Self::POLL_FNS) + } +} -- cgit v1.2.3-54-g00ecf