aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 0a58daa42c1db09026ec7bc78710db34993a7d88 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Implements a stream of terminal resize events.
//!
//! # Overview
//!
//! Whenever the user resizes their terminal, a notification is sent to the
//! application running in it. This crate provides those notifications in the
//! form of a stream.
//!
//! # Synopsis
//!
//! ```
//! # use futures::future::Future as _;
//! # use futures::stream::Stream as _;
//! let stream = tokio_terminal_resize::resizes().flatten_stream();
//! let prog = stream
//!     .for_each(|(rows, cols)| {
//!         println!("terminal is now {}x{}", cols, rows);
//!         Ok(())
//!     })
//!     .map_err(|e| eprintln!("error: {}", e));
//! tokio::run(prog);
//! ```

#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]

use futures::future::Future as _;
use futures::stream::Stream as _;
use snafu::futures01::FutureExt as _;
use snafu::futures01::StreamExt as _;
use snafu::ResultExt as _;
use std::convert::TryInto as _;

/// Errors returned by this crate.
#[derive(Debug, snafu::Snafu)]
pub enum Error {
    /// failed to get terminal size
    #[snafu(display("failed to get terminal size"))]
    GetTerminalSize,

    /// invalid terminal size found
    #[snafu(display("invalid terminal size found: {}", source))]
    InvalidTerminalSize { source: std::num::TryFromIntError },

    /// SIGWINCH handler failed
    #[snafu(display("SIGWINCH handler failed: {}", source))]
    SigWinchHandler { source: std::io::Error },
}

/// Creates a stream which receives the new terminal size every time the
/// user's terminal is resized.
pub fn resizes() -> ResizeFuture {
    ResizeFuture::default()
}

/// Future which sets up the terminal size stream
pub struct ResizeFuture {
    stream_fut: Box<
        dyn futures::future::Future<Item = ResizeStream, Error = Error>
            + Send,
    >,
}

impl Default for ResizeFuture {
    fn default() -> Self {
        let stream_fut = tokio_signal::unix::Signal::new(
            tokio_signal::unix::libc::SIGWINCH,
        )
        .context(SigWinchHandler)
        .and_then(|stream| {
            futures::future::ok(ResizeStream {
                winches: Box::new(
                    stream.map(|_| ()).context(SigWinchHandler),
                ),
                sent_initial_size: false,
            })
        });
        Self {
            stream_fut: Box::new(stream_fut),
        }
    }
}

#[must_use = "streams do nothing unless polled"]
impl futures::future::Future for ResizeFuture {
    type Item = ResizeStream;
    type Error = Error;

    fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
        self.stream_fut.poll()
    }
}

/// Stream which returns the new terminal size every time it changes
pub struct ResizeStream {
    winches:
        Box<dyn futures::stream::Stream<Item = (), Error = Error> + Send>,
    sent_initial_size: bool,
}

#[must_use = "streams do nothing unless polled"]
impl futures::stream::Stream for ResizeStream {
    type Item = (u16, u16);
    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(term_size()?)));
        }
        futures::try_ready!(self.winches.poll());
        Ok(futures::Async::Ready(Some(term_size()?)))
    }
}

fn term_size() -> Result<(u16, u16), Error> {
    if let Some((cols, rows)) = term_size::dimensions() {
        Ok((
            rows.try_into().context(InvalidTerminalSize)?,
            cols.try_into().context(InvalidTerminalSize)?,
        ))
    } else {
        Err(Error::GetTerminalSize)
    }
}