-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui.rs
More file actions
43 lines (39 loc) · 1.39 KB
/
ui.rs
File metadata and controls
43 lines (39 loc) · 1.39 KB
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
use console::{StyledObject, style};
use indicatif::{ProgressBar, ProgressStyle};
use super::types::Size;
/// Creates and returns a configured progress bar for use in the application.
pub fn create_progress_bar(total: u64) -> ProgressBar {
let pb = ProgressBar::new(total);
let template = "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} {msg}";
let bar_style = ProgressStyle::default_bar()
.template(template)
.unwrap()
.progress_chars("#>-");
pb.set_style(bar_style);
pb
}
/// Creates and returns a spinner for showing progress on a single operation.
pub fn create_spinner(message: &str) -> ProgressBar {
let sp = ProgressBar::new_spinner();
let spinner_style =
ProgressStyle::with_template("{prefix:.bold.dim} {spinner} {wide_msg}")
.unwrap()
.tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈");
sp.set_style(spinner_style);
sp.set_message(message.to_string());
sp.enable_steady_tick(std::time::Duration::from_millis(120));
sp
}
pub fn style_size(size: &Size) -> StyledObject<String> {
let mb = 1024 * 1024;
match size {
Size::UnknownSize => style(format!("{}", size)).red(),
Size::KnownSize(bytes) if (*bytes < 5 * mb) => {
style(format!("{}", size)).green()
}
Size::KnownSize(bytes) if (*bytes < 250 * mb) => {
style(format!("{}", size)).yellow()
}
Size::KnownSize(_) => style(format!("{}", size)).red(),
}
}