cargo_lambda_interactive/
progress.rs1use crate::is_stdout_tty;
2use indicatif::{ProgressBar, ProgressStyle};
3use std::{borrow::Cow, time::Duration};
4
5pub struct Progress {
6 bar: Option<ProgressBar>,
7}
8
9impl Progress {
10 pub fn start(msg: impl Into<Cow<'static, str>>) -> Progress {
11 let bar = if is_stdout_tty() {
12 Some(show_progress(msg))
13 } else {
14 println!("▹▹▹▹▹ {}", msg.into());
15 None
16 };
17 Progress { bar }
18 }
19
20 pub fn finish(&self, msg: &str) {
21 if let Some(bar) = &self.bar {
22 bar.finish_with_message(msg.to_string());
23 } else {
24 println!("▪▪▪▪▪ {msg}");
25 }
26 }
27
28 pub fn set_message(&self, msg: &str) {
29 if let Some(bar) = &self.bar {
30 bar.set_message(msg.to_string());
31 } else {
32 println!("▹▹▹▹▹ {msg}");
33 }
34 }
35
36 pub fn finish_and_clear(&self) {
37 if let Some(bar) = &self.bar {
38 bar.finish_and_clear();
39 }
40 }
41}
42
43fn show_progress(msg: impl Into<Cow<'static, str>>) -> ProgressBar {
44 let pb = ProgressBar::new_spinner();
45 pb.enable_steady_tick(Duration::from_millis(120));
46 pb.set_style(ProgressStyle::default_spinner().tick_strings(&[
47 "▹▹▹▹▹",
48 "▸▹▹▹▹",
49 "▹▸▹▹▹",
50 "▹▹▸▹▹",
51 "▹▹▹▸▹",
52 "▹▹▹▹▸",
53 "▪▪▪▪▪",
54 ]));
55 pb.set_message(msg);
56 pb
57}