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 fail(&self, details: &str) {
41 self.pb.finish_with_message(format!(
42 "{} {} {}",
43 "ERR".bright_red().bold(),
44 self.label,
45 format!("({})", details).bright_black()
46 ));
47 }
48}
49
50pub async fn with_loader<T, E, F>(label: &str, op: F) -> Result<T, E>
51where
52 E: std::fmt::Display,
53 F: Future<Output = Result<T, E>>,
54{
55 let loader = Loader::start(label);
56 let result = op.await;
57 match &result {
58 Ok(_) => loader.success(),
59 Err(err) => loader.fail(&err.to_string()),
60 }
61 result
62}
63
64pub fn print_cli_header(command: &str, debug: bool) {
65 let mode = if debug {
66 "DEBUG".bright_yellow().bold().to_string()
67 } else {
68 "NORMAL".bright_blue().bold().to_string()
69 };
70 println!(
71 "{} {} {} {}",
72 "xbp".bright_magenta().bold(),
73 "→".bright_black(),
74 command.bright_white().bold(),
75 format!("[{}]", mode).bright_black()
76 );
77}
78
79pub fn configure_color_output() {
80 let disable_via_clicolor = std::env::var("CLICOLOR")
82 .map(|value| value == "0")
83 .unwrap_or(false);
84 if std::env::var_os("NO_COLOR").is_some() || disable_via_clicolor {
85 colored::control::set_override(false);
86 return;
87 }
88 if std::env::var_os("FORCE_COLOR").is_some() || std::env::var_os("CLICOLOR_FORCE").is_some() {
89 colored::control::set_override(true);
90 return;
91 }
92
93 let stdout_color = supports_color::on(Stream::Stdout).is_some();
94 let stderr_color = supports_color::on(Stream::Stderr).is_some();
95 colored::control::set_override(stdout_color || stderr_color);
96}
97
98pub fn section(title: &str) {
99 println!(
100 "\n{} {}",
101 "◆".bright_blue().bold(),
102 title.bright_blue().bold()
103 );
104}
105
106pub fn divider(width: usize) {
107 println!("{}", "─".repeat(width).bright_black());
108}
109
110pub fn status_line(label: &str, status: &str, ok: bool) {
111 let icon = if ok {
112 "✓".bright_green().bold()
113 } else {
114 "✗".bright_red().bold()
115 };
116 let status = if ok {
117 status.bright_green().to_string()
118 } else {
119 status.bright_red().to_string()
120 };
121 println!(" {} {} {}", icon, label.bright_white(), status);
122}
123
124pub fn tip(message: &str) {
125 println!("{} {}", "Hint:".bright_yellow().bold(), message);
126}
127
128fn select_spinner_set(label: &str) -> &'static [&'static str] {
129 let hash = label
130 .bytes()
131 .fold(0_u64, |acc, b| acc.wrapping_mul(16777619) ^ u64::from(b));
132 let idx = (hash as usize) % SPINNER_SETS.len();
133 SPINNER_SETS[idx]
134}