use std::io::{self, Write};
use std::sync::{Arc, Mutex, PoisonError};
#[derive(Clone, Default)]
pub(crate) struct Printed(Arc<Mutex<Vec<u8>>>);
impl Write for Printed {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.0.lock().unwrap_or_else(PoisonError::into_inner).extend_from_slice(bytes);
Ok(bytes.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl Printed {
pub(crate) fn lines(&self) -> Vec<String> {
let printed = self.0.lock().unwrap_or_else(PoisonError::into_inner).clone();
String::from_utf8(printed).unwrap().lines().map(str::to_owned).collect()
}
pub(crate) fn untimed(&self) -> Vec<String> {
let untimed = |line: &String| line.split_once("Z\",").map(|(_, rest)| format!("{{{rest}")).unwrap();
self.lines().iter().map(untimed).collect()
}
}