use parking_lot::Mutex;
use rustyline::ExternalPrinter;
use std::sync::Arc;
#[derive(Clone)]
pub struct SharedPrinterSlot {
inner: Arc<Mutex<Option<Box<dyn ExternalPrinter + Send>>>>,
}
impl SharedPrinterSlot {
#[must_use]
pub fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(None)),
}
}
pub fn set(&self, printer: Box<dyn ExternalPrinter + Send>) {
*self.inner.lock() = Some(printer);
}
pub fn clear(&self) {
*self.inner.lock() = None;
}
pub fn print(&self, msg: String) -> bool {
let mut guard = self.inner.lock();
if let Some(ref mut printer) = *guard {
printer.print(msg).is_ok()
} else {
false
}
}
#[must_use]
pub fn clone_arc(&self) -> Arc<Mutex<Option<Box<dyn ExternalPrinter + Send>>>> {
Arc::clone(&self.inner)
}
}
impl Default for SharedPrinterSlot {
fn default() -> Self {
Self::new()
}
}