use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;
pub use termcolor::*;
use crate::reporter::ReportingTarget;
#[derive(Clone)]
pub(crate) struct ReportWriter {
inner: Arc<Mutex<StandardStream>>,
}
impl ReportWriter {
pub fn new(target: ReportingTarget) -> Self {
let stream = match target {
ReportingTarget::Stdout => StandardStream::stdout(ColorChoice::Auto),
ReportingTarget::Stderr => StandardStream::stderr(ColorChoice::Auto),
};
Self { inner: Arc::new(Mutex::new(stream)) }
}
pub fn lock(&self) -> Gaurd<'_> {
Gaurd(self.inner.lock().expect("writer lock poisoned, this should never happen"))
}
}
pub(crate) struct Gaurd<'a>(MutexGuard<'a, StandardStream>);
impl WriteColor for Gaurd<'_> {
fn set_color(&mut self, spec: &ColorSpec) -> std::io::Result<()> {
self.0.set_color(spec)
}
fn reset(&mut self) -> std::io::Result<()> {
self.0.reset()
}
fn supports_color(&self) -> bool {
self.0.supports_color()
}
}
impl std::io::Write for Gaurd<'_> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.0.flush()
}
}