mz_deploy/cli/
progress.rs1use crate::info;
17use owo_colors::{OwoColorize, Stream, Style};
18use std::time::Duration;
19
20pub fn stage_start(name: &str) {
22 info!(
23 "{} {}...",
24 "→".if_supports_color(Stream::Stderr, |t| t.yellow()),
25 name
26 );
27}
28
29pub fn stage_success(message: &str, duration: Duration) {
31 let seconds = duration.as_secs_f64();
32 let suffix = format!("({}s)", format_duration(seconds));
33 info!(
34 " {} {} {}",
35 "✓".if_supports_color(Stream::Stderr, |t| t.green()),
36 message,
37 suffix.if_supports_color(Stream::Stderr, |t| t.dimmed()),
38 );
39}
40
41pub fn success(message: &str) {
43 info!(
44 " {} {}",
45 "✓".if_supports_color(Stream::Stderr, |t| t.green()),
46 message
47 );
48}
49
50pub fn warn(message: &str) {
52 info!(
53 " {} {}",
54 "⚠".if_supports_color(Stream::Stderr, |t| t.yellow()),
55 message
56 );
57}
58
59pub fn error(message: &str) {
61 info!(
62 " {} {}",
63 "✗".if_supports_color(Stream::Stderr, |t| t.red()),
64 message
65 );
66}
67
68pub fn action(verb: &str, message: &str) {
71 let label = format!("{:>12}", verb);
72 let style = Style::new().bright_green().bold();
73 info!(
74 "{} {}",
75 label.if_supports_color(Stream::Stderr, |t| style.style(t)),
76 message
77 );
78}
79
80pub fn finished(action_name: &str, duration: Duration) {
88 let seconds = duration.as_secs_f64();
89 action(
90 "Finished",
91 &format!("{} in {}s", action_name, format_duration(seconds)),
92 );
93}
94
95fn format_duration(seconds: f64) -> String {
99 if seconds < 1.0 {
100 format!("{:.2}", seconds)
101 } else {
102 format!("{:.1}", seconds)
103 }
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 #[mz_ore::test]
111 fn test_format_duration() {
112 assert_eq!(format_duration(0.05), "0.05");
113 assert_eq!(format_duration(0.123), "0.12");
114 assert_eq!(format_duration(1.0), "1.0");
115 assert_eq!(format_duration(2.567), "2.6");
116 assert_eq!(format_duration(10.12), "10.1");
117 }
118}