aboutsummaryrefslogtreecommitdiffstats
path: root/src/resize.rs
blob: 9e357afd9da3d4678270773e72d83d601dbe473f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use crate::prelude::*;

pub struct Resizer {
    winches:
        Box<dyn futures::stream::Stream<Item = (), Error = Error> + Send>,
    sent_initial_size: bool,
}

impl Resizer {
    pub fn new() -> Self {
        let winches = tokio_signal::unix::Signal::new(
            tokio_signal::unix::libc::SIGWINCH,
        )
        .flatten_stream()
        .map(|_| ())
        .context(crate::error::SigWinchHandler);
        Self {
            winches: Box::new(winches),
            sent_initial_size: false,
        }
    }
}

#[must_use = "streams do nothing unless polled"]
impl futures::stream::Stream for Resizer {
    type Item = crate::term::Size;
    type Error = Error;

    fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
        if !self.sent_initial_size {
            self.sent_initial_size = true;
            return Ok(futures::Async::Ready(
                Some(crate::term::Size::get()?),
            ));
        }
        let _ = futures::try_ready!(self.winches.poll());
        Ok(futures::Async::Ready(Some(crate::term::Size::get()?)))
    }
}

pub enum Event<R: tokio::io::AsyncRead + 'static> {
    Process(<crate::process::Process<R> as futures::stream::Stream>::Item),
    Resize(crate::term::Size),
}

pub struct ResizingProcess<R: tokio::io::AsyncRead + 'static> {
    process: crate::process::Process<R>,
    resizer: Resizer,
}

impl<R: tokio::io::AsyncRead + 'static> ResizingProcess<R> {
    pub fn new(process: crate::process::Process<R>) -> Self {
        Self {
            process,
            resizer: Resizer::new(),
        }
    }
}

impl<R: tokio::io::AsyncRead + 'static> ResizingProcess<R> {
    const POLL_FNS:
        &'static [&'static dyn for<'a> Fn(
            &'a mut Self,
        )
            -> crate::component_future::Poll<
            Option<Event<R>>,
            Error,
        >] = &[&Self::poll_resize, &Self::poll_process];

    fn poll_resize(
        &mut self,
    ) -> crate::component_future::Poll<Option<Event<R>>, Error> {
        let size = try_ready!(self.resizer.poll()).unwrap();
        self.process.resize(size.clone());
        Ok(crate::component_future::Async::Ready(Some(Event::Resize(
            size,
        ))))
    }

    fn poll_process(
        &mut self,
    ) -> crate::component_future::Poll<Option<Event<R>>, Error> {
        Ok(crate::component_future::Async::Ready(
            try_ready!(self.process.poll()).map(Event::Process),
        ))
    }
}

#[must_use = "streams do nothing unless polled"]
impl<R: tokio::io::AsyncRead + 'static> futures::stream::Stream
    for ResizingProcess<R>
{
    type Item = Event<R>;
    type Error =
        <crate::process::Process<R> as futures::stream::Stream>::Error;

    fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
        crate::component_future::poll_stream(self, Self::POLL_FNS)
    }
}