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
//! > Polyfill for `is_terminal` stdlib feature for use with older MSRVs

#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]

/// Trait to determine if a descriptor/handle refers to a terminal/tty.
pub trait IsTerminal: sealed::Sealed {
    /// Returns `true` if the descriptor/handle refers to a terminal/tty.
    ///
    /// On platforms where Rust does not know how to detect a terminal yet, this will return
    /// `false`. This will also return `false` if an unexpected error occurred, such as from
    /// passing an invalid file descriptor.
    ///
    /// # Platform-specific behavior
    ///
    /// On Windows, in addition to detecting consoles, this currently uses some heuristics to
    /// detect older msys/cygwin/mingw pseudo-terminals based on device name: devices with names
    /// starting with `msys-` or `cygwin-` and ending in `-pty` will be considered terminals.
    /// Note that this [may change in the future][changes].
    ///
    /// [changes]: std::io#platform-specific-behavior
    fn is_terminal(&self) -> bool;
}

mod sealed {
    pub trait Sealed {}
}

macro_rules! impl_is_terminal {
    ($($t:ty),*$(,)?) => {$(
        impl sealed::Sealed for $t {}

        impl IsTerminal for $t {
            #[inline]
            fn is_terminal(&self) -> bool {
                std::io::IsTerminal::is_terminal(self)
            }
        }
    )*}
}

impl_is_terminal!(
    std::fs::File,
    std::io::Stdin,
    std::io::StdinLock<'_>,
    std::io::Stdout,
    std::io::StdoutLock<'_>,
    std::io::Stderr,
    std::io::StderrLock<'_>
);