use cargo_like_utils::shell::Shell;
use std::{
fmt::Display,
sync::{Arc, Mutex, OnceLock},
};
pub static SHELL: OnceLock<Arc<Mutex<Shell>>> = OnceLock::new();
pub mod style {
pub use cargo_like_utils::shell::style::*;
}
pub fn try_init_shell(shell: Shell) {
SHELL.get_or_init(|| Arc::new(Mutex::new(shell)));
}
pub trait ShellExt {
fn status<T, U>(&self, status: T, message: U)
where
T: Display,
U: Display;
fn status_with_color<T, U>(&self, status: T, message: U, color: &style::Style)
where
T: Display,
U: Display;
fn note<T: Display>(&self, message: T);
fn warn<T: Display>(&self, message: T);
fn error<T: Display>(&self, message: T);
}
impl ShellExt for OnceLock<Arc<Mutex<Shell>>> {
fn status<T, U>(&self, status: T, message: U)
where
T: Display,
U: Display,
{
if let Some(s) = self.get() {
let _ = s.lock().unwrap().status(status, message);
}
}
fn status_with_color<T, U>(&self, status: T, message: U, color: &style::Style)
where
T: Display,
U: Display,
{
if let Some(s) = self.get() {
let _ = s.lock().unwrap().status_with_color(status, message, color);
}
}
fn note<T: Display>(&self, message: T) {
if let Some(s) = self.get() {
let _ = s.lock().unwrap().note(message);
}
}
fn warn<T: Display>(&self, message: T) {
if let Some(s) = self.get() {
let _ = s.lock().unwrap().warn(message);
}
}
fn error<T: Display>(&self, message: T) {
if let Some(s) = self.get() {
let _ = s.lock().unwrap().error(message);
}
}
}