spinoff/
streams.rs

1use std::io::{stderr, stdout, Write};
2/// Simplified type for a stream.
3/// By default, `spinoff` uses `Streams::Stdout`.
4#[derive(Default, Copy, Clone, Debug)]
5pub enum Streams {
6    #[default]
7    Stdout,
8    Stderr,
9}
10
11impl Streams {
12    // Returns the stream to use.
13    #[must_use = "Stream must be retrieved"]
14    pub fn get_stream(self) -> Box<dyn Write + Send + Sync> {
15        match self {
16            Self::Stdout => Box::new(stdout()),
17            Self::Stderr => Box::new(stderr()),
18        }
19    }
20    // Clever implementation that allows us to automatically get the stream when `write!` is called.
21    pub fn write_fmt<T>(self, fmt: T)
22    where
23        T: std::fmt::Display,
24    {
25        write!(self.get_stream(), "{fmt}").expect("error: failed to write to stream");
26    }
27
28}