use indicatif::MultiProgress;
use std::io::{self, Write};
pub struct ProgressWriter<'a> {
multi: &'a MultiProgress,
}
impl<'a> ProgressWriter<'a> {
pub(crate) fn new(multi: &'a MultiProgress) -> Self {
Self { multi }
}
}
impl Write for ProgressWriter<'_> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.multi
.suspend(|| io::stderr().lock().write_all(buf))
.map(|()| buf.len())
}
fn flush(&mut self) -> io::Result<()> {
self.multi.suspend(|| io::stderr().lock().flush())
}
}
#[cfg(all(test, feature = "server"))]
mod tests {
use super::*;
use indicatif::ProgressDrawTarget;
#[test]
fn progress_writer_write() {
let multi = MultiProgress::with_draw_target(ProgressDrawTarget::hidden());
let mut writer = ProgressWriter::new(&multi);
let count = writer.write(b"x").expect("write should succeed");
assert_eq!(count, 1);
}
}