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
//! Access to standard output and standard error

use isatty;
use std::{
    cell::RefCell,
    io::{self, Write},
    sync::{Mutex, MutexGuard},
};

use super::{ColorConfig, Shell};

lazy_static! {
    static ref STDOUT: Mutex<RefCell<Shell>> = {
        Mutex::new(RefCell::new(Shell::new(
            Stream::Stdout,
            ColorConfig::default(),
        )))
    };
    static ref STDERR: Mutex<RefCell<Shell>> = {
        Mutex::new(RefCell::new(Shell::new(
            Stream::Stderr,
            ColorConfig::default(),
        )))
    };
}

/// Terminal streams
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Stream {
    /// Standard output
    Stdout,

    /// Standard error
    Stderr,
}

impl Stream {
    /// Get a shell for this stream type
    #[allow(unknown_lints, trivially_copy_pass_by_ref)]
    pub(crate) fn lock_shell(&self) -> MutexGuard<RefCell<Shell>> {
        match self {
            // TODO: better handle `PoisonError`?
            Stream::Stdout => STDOUT.lock().unwrap(),
            Stream::Stderr => STDERR.lock().unwrap(),
        }
    }

    /// Get a boxed writer for this stream
    pub(crate) fn writer(self) -> Box<Write + Send> {
        match self {
            Stream::Stdout => Box::new(io::stdout()),
            Stream::Stderr => Box::new(io::stderr()),
        }
    }

    /// Is this stream a TTY?
    pub fn is_tty(self) -> bool {
        match self {
            Stream::Stdout => isatty::stdout_isatty(),
            Stream::Stderr => isatty::stderr_isatty(),
        }
    }
}