-
Notifications
You must be signed in to change notification settings - Fork 2
/
formatting.rs
71 lines (61 loc) · 2.21 KB
/
formatting.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::time::Duration;
use indicatif::{ProgressBar, ProgressStyle};
use crate::utils::{sort_update_vec, to_json};
pub fn print_updates(updates: &[(String, Option<bool>)], icons: &bool) {
let sorted_updates = sort_update_vec(updates);
let term_width: usize = termsize::get()
.unwrap_or(termsize::Size { rows: 24, cols: 80 })
.cols as usize;
for update in sorted_updates {
let description = match update.1 {
Some(true) => "Update available",
Some(false) => "Up to date",
None => "Unknown",
};
let icon = if *icons {
match update.1 {
Some(true) => "\u{f0aa} ",
Some(false) => "\u{f058} ",
None => "\u{f059} ",
}
} else {
""
};
let color = match update.1 {
Some(true) => "\u{001b}[38;5;12m",
Some(false) => "\u{001b}[38;5;2m",
None => "\u{001b}[38;5;8m",
};
let dynamic_space =
" ".repeat(term_width - description.len() - icon.len() - update.0.len());
println!(
"{}{}{}{}{}\u{001b}[0m",
color, icon, update.0, dynamic_space, description
);
}
}
pub fn print_raw_updates(updates: &[(String, Option<bool>)]) {
println!("{}", json::stringify(to_json(updates)));
}
pub struct Spinner {
spinner: ProgressBar,
}
impl Spinner {
#[allow(clippy::new_without_default)]
pub fn new() -> Spinner {
let spinner = ProgressBar::new_spinner();
let style: &[&str] = &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
let progress_style = ProgressStyle::default_spinner();
spinner.set_style(ProgressStyle::tick_strings(progress_style, style));
spinner.set_message("Checking...");
spinner.enable_steady_tick(Duration::from_millis(50));
Spinner { spinner }
}
pub fn succeed(&self) {
const CHECKMARK: &str = "\u{001b}[32;1m\u{2713}\u{001b}[0m";
let success_message = format!("{} Done!", CHECKMARK);
self.spinner
.set_style(ProgressStyle::with_template("{msg}").unwrap());
self.spinner.finish_with_message(success_message);
}
}