pavex_cli_shell/
lib.rs

1use cargo_like_utils::shell::Shell;
2use std::{
3    fmt::Display,
4    sync::{Arc, Mutex, OnceLock},
5};
6
7/// A global shell instance used by Pavex CLIs to print messages to the user.
8pub static SHELL: OnceLock<Arc<Mutex<Shell>>> = OnceLock::new();
9
10/// Customize how messages are shown in the shell.
11pub mod style {
12    pub use cargo_like_utils::shell::style::*;
13}
14
15/// Initialize the global shell instance, if it hasn't been initialized yet.
16pub fn try_init_shell(shell: Shell) {
17    SHELL.get_or_init(|| Arc::new(Mutex::new(shell)));
18}
19
20pub trait ShellExt {
21    fn status<T, U>(&self, status: T, message: U)
22    where
23        T: Display,
24        U: Display;
25    fn status_with_color<T, U>(&self, status: T, message: U, color: &style::Style)
26    where
27        T: Display,
28        U: Display;
29    fn note<T: Display>(&self, message: T);
30    fn warn<T: Display>(&self, message: T);
31    fn error<T: Display>(&self, message: T);
32}
33
34impl ShellExt for OnceLock<Arc<Mutex<Shell>>> {
35    fn status<T, U>(&self, status: T, message: U)
36    where
37        T: Display,
38        U: Display,
39    {
40        if let Some(s) = self.get() {
41            let _ = s.lock().unwrap().status(status, message);
42        }
43    }
44
45    fn status_with_color<T, U>(&self, status: T, message: U, color: &style::Style)
46    where
47        T: Display,
48        U: Display,
49    {
50        if let Some(s) = self.get() {
51            let _ = s.lock().unwrap().status_with_color(status, message, color);
52        }
53    }
54
55    fn note<T: Display>(&self, message: T) {
56        if let Some(s) = self.get() {
57            let _ = s.lock().unwrap().note(message);
58        }
59    }
60
61    fn warn<T: Display>(&self, message: T) {
62        if let Some(s) = self.get() {
63            let _ = s.lock().unwrap().warn(message);
64        }
65    }
66
67    fn error<T: Display>(&self, message: T) {
68        if let Some(s) = self.get() {
69            let _ = s.lock().unwrap().error(message);
70        }
71    }
72}