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
//! Static information output, see [`page_all`].
use crate::{init, utils};

use crate::error::AlternateScreenPagingError;
use crate::Pager;
use crossterm::tty::IsTty;
use std::io::{self, Write};

#[derive(Debug, thiserror::Error)]
pub enum PageAllError {
    #[error(transparent)]
    Io(#[from] std::io::Error),

    #[error(transparent)]
    Paging(#[from] AlternateScreenPagingError),

    #[error("Failed to determine terminal size")]
    TerminalSize(crossterm::ErrorKind),
}

/// Outputs static information.
///
/// Once called, the `Pager` passed to this function can never be changed. If you
/// want dynamic information:
///
#[cfg_attr(
    feature = "async_std_lib",
    doc = "- [`async_std_updating`](crate::async_std_updating)\n"
)]
#[cfg_attr(
    feature = "tokio_lib",
    doc = "- [`tokio_updating`](crate::tokio_updating)\n"
)]
#[cfg_attr(
    not(any(feature = "async_std_lib", feature = "tokio_lib")),
    doc = "- Asynchronous features are disabled, see [here](crate#features) for more information.\n"
)]
///
/// ## Errors
///
/// Several operations can fail when outputting information to a terminal, see
/// the [`Result`] type.
///
/// ## Example
///
/// ```rust,no_run
/// use std::fmt::Write;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut output = minus::Pager::new().unwrap();
///
///     for i in 0..=30 {
///         output.push_str(format!("{}\n", i));
///     }
///
///     minus::page_all(output)?;
///     Ok(())
/// }
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "static_output")))]
pub fn page_all(mut p: Pager) -> Result<(), PageAllError> {
    // Setup terminal
    let stdout = io::stdout();
    let line_count = p.num_lines();

    // If stdout is not a tty, print all the output without paging
    // then print it and exit the function.
    {
        if !stdout.is_tty() {
            let mut out = stdout.lock();
            utils::write_lines(&mut out, &mut p)?;
            out.flush()?;
            return Ok(());
        }
    }

    {
        // If the number of lines in the output is less than the number of rows
        if !p.run_no_overflow && p.rows > line_count {
            let mut out = stdout.lock();
            utils::write_lines(&mut out, &mut p)?;
            out.flush()?;
        } else {
            init::static_paging(p)?;
        }

        Ok(())
    }
}