1use colored::Colorize;
2use indicatif::{ProgressBar, ProgressStyle};
3use std::future::Future;
4use std::time::Duration;
5use supports_color::Stream;
6
7const SPINNER_SETS: &[&[&str]] = &[
8 &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
9 &["◐", "◓", "◑", "◒"],
10 &["▖", "▘", "▝", "▗"],
11 &["∙∙∙", "●∙∙", "∙●∙", "∙∙●", "∙●∙"],
12];
13
14pub struct Loader {
15 pb: ProgressBar,
16 label: String,
17}
18
19impl Loader {
20 pub fn start(label: &str) -> Self {
21 let spinner_frames = select_spinner_set(label);
22 let pb = ProgressBar::new_spinner();
23 let style = ProgressStyle::with_template("{spinner:.cyan} {msg}")
24 .unwrap_or_else(|_| ProgressStyle::default_spinner())
25 .tick_strings(spinner_frames);
26 pb.set_style(style);
27 pb.set_message(format!("{}", label.bright_cyan()));
28 pb.enable_steady_tick(Duration::from_millis(95));
29 Self {
30 pb,
31 label: label.to_string(),
32 }
33 }
34
35 pub fn success(&self) {
36 self.pb
37 .finish_with_message(format!("{} {}", "OK".bright_green().bold(), self.label));
38 }
39
40 pub fn success_with(&self, message: &str) {
41 self.pb
42 .finish_with_message(format!("{} {}", "OK".bright_green().bold(), message));
43 }
44
45 pub fn fail(&self, details: &str) {
46 self.pb.finish_with_message(format!(
47 "{} {} {}",
48 "ERR".bright_red().bold(),
49 self.label,
50 format!("({})", details).bright_black()
51 ));
52 }
53
54 pub fn update(&self, message: &str) {
55 self.pb.set_message(format!("{}", message.bright_cyan()));
56 }
57
58 pub fn log(&self, message: &str) {
59 self.pb.println(message);
60 }
61
62 pub fn suspend<T, F>(&self, op: F) -> T
63 where
64 F: FnOnce() -> T,
65 {
66 self.pb.suspend(op)
67 }
68}
69
70pub async fn with_loader<T, E, F>(label: &str, op: F) -> Result<T, E>
71where
72 E: std::fmt::Display,
73 F: Future<Output = Result<T, E>>,
74{
75 let loader = Loader::start(label);
76 let result = op.await;
77 match &result {
78 Ok(_) => loader.success(),
79 Err(err) => loader.fail(&err.to_string()),
80 }
81 result
82}
83
84pub fn print_cli_header(command: &str, debug: bool) {
85 let mode = if debug {
86 "DEBUG".bright_yellow().bold().to_string()
87 } else {
88 "NORMAL".bright_blue().bold().to_string()
89 };
90 println!(
91 "{} {} {} {}",
92 "xbp".bright_magenta().bold(),
93 "→".bright_black(),
94 command.bright_white().bold(),
95 format!("[{}]", mode).bright_black()
96 );
97}
98
99pub fn configure_color_output() {
100 let disable_via_clicolor = std::env::var("CLICOLOR")
102 .map(|value| value == "0")
103 .unwrap_or(false);
104 if std::env::var_os("NO_COLOR").is_some() || disable_via_clicolor {
105 colored::control::set_override(false);
106 return;
107 }
108 if std::env::var_os("FORCE_COLOR").is_some() || std::env::var_os("CLICOLOR_FORCE").is_some() {
109 colored::control::set_override(true);
110 return;
111 }
112
113 let stdout_color = supports_color::on(Stream::Stdout).is_some();
114 let stderr_color = supports_color::on(Stream::Stderr).is_some();
115 colored::control::set_override(stdout_color || stderr_color);
116}
117
118pub fn section(title: &str) {
119 println!(
120 "\n{} {}",
121 "◆".bright_blue().bold(),
122 title.bright_blue().bold()
123 );
124}
125
126pub fn divider(width: usize) {
127 println!("{}", "─".repeat(width).bright_black());
128}
129
130pub fn status_line(label: &str, status: &str, ok: bool) {
131 let icon = if ok {
132 "✓".bright_green().bold()
133 } else {
134 "✗".bright_red().bold()
135 };
136 let status = if ok {
137 status.bright_green().to_string()
138 } else {
139 status.bright_red().to_string()
140 };
141 println!(" {} {} {}", icon, label.bright_white(), status);
142}
143
144pub fn tip(message: &str) {
145 println!("{} {}", "Hint:".bright_yellow().bold(), message);
146}
147
148fn select_spinner_set(label: &str) -> &'static [&'static str] {
149 let hash = label
150 .bytes()
151 .fold(0_u64, |acc, b| acc.wrapping_mul(16777619) ^ u64::from(b));
152 let idx = (hash as usize) % SPINNER_SETS.len();
153 SPINNER_SETS[idx]
154}