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
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(),
)))
};
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Stream {
Stdout,
Stderr,
}
impl Stream {
#[allow(unknown_lints, trivially_copy_pass_by_ref)]
pub(crate) fn lock_shell(&self) -> MutexGuard<RefCell<Shell>> {
match self {
Stream::Stdout => STDOUT.lock().unwrap(),
Stream::Stderr => STDERR.lock().unwrap(),
}
}
pub(crate) fn writer(self) -> Box<Write + Send> {
match self {
Stream::Stdout => Box::new(io::stdout()),
Stream::Stderr => Box::new(io::stderr()),
}
}
pub fn is_tty(self) -> bool {
match self {
Stream::Stdout => isatty::stdout_isatty(),
Stream::Stderr => isatty::stderr_isatty(),
}
}
}