intermodal/src/output_stream.rs
Casey Rodarmor 1532113782
Print individual file torrent verification errors
If torrent verification fails, print all errors with individual files.

type: changed
2020-04-07 19:01:26 -07:00

66 lines
1.3 KiB
Rust

use crate::common::*;
pub(crate) struct OutputStream {
stream: Box<dyn Write>,
style: bool,
term: bool,
}
impl OutputStream {
pub(crate) fn stdout(style: bool) -> OutputStream {
let term = atty::is(atty::Stream::Stdout);
Self {
stream: Box::new(io::stdout()),
style: style && term,
term,
}
}
pub(crate) fn stderr(style: bool) -> OutputStream {
Self {
term: style && atty::is(atty::Stream::Stderr),
stream: Box::new(io::stderr()),
style,
}
}
#[cfg(test)]
pub(crate) fn new(stream: Box<dyn Write>, style: bool, term: bool) -> OutputStream {
Self {
stream,
style,
term,
}
}
pub(crate) fn set_use_color(&mut self, use_color: UseColor) {
match use_color {
UseColor::Always => self.style = true,
UseColor::Auto => {}
UseColor::Never => self.style = false,
}
}
pub(crate) fn is_term(&self) -> bool {
self.term
}
pub(crate) fn is_styled(&self) -> bool {
self.style
}
pub(crate) fn style(&self) -> Style {
Style::from_active(self.style)
}
}
impl Write for OutputStream {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.stream.write(data)
}
fn flush(&mut self) -> io::Result<()> {
self.stream.flush()
}
}