logo
 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
//! Terminal handling (TTY interactions, colors, etc)

#[cfg(feature = "application")]
pub mod component;
#[macro_use]
pub mod status;
pub mod streams;

pub use self::streams::Streams;
pub use termcolor::{Color, ColorChoice, StandardStream};

use once_cell::sync::OnceCell;

/// Terminal streams
static STREAMS: OnceCell<Streams> = OnceCell::new();

/// Initialize the terminal subsystem, registering the [`Streams`] static
pub(crate) fn init(color_choice: ColorChoice) {
    STREAMS
        .set(Streams::new(color_choice))
        .unwrap_or_else(|_| panic!("terminal streams already initialized!"));
}

/// Get the terminal [`Streams`].
pub fn streams() -> &'static Streams {
    STREAMS
        .get()
        .expect("terminal streams not yet initialized!")
}

/// Get the standard output stream
pub fn stdout() -> &'static StandardStream {
    &streams().stdout
}

/// Get the standard error stream
pub fn stderr() -> &'static StandardStream {
    &streams().stderr
}