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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::fmt;
use std::time::Duration;
use isatty;
pub use super::report::{Counts, Report, Sizes};
pub mod color;
pub mod plain;
pub trait UI: fmt::Debug {
fn show_progress(&mut self, report: &Report);
fn print(&mut self, s: &str);
fn problem(&mut self, s: &str);
fn finish(&mut self);
}
impl UI {
pub fn by_name(ui_name: &str, progress_bar: bool) -> Option<Box<UI + Send>> {
if ui_name == "color" || (ui_name == "auto" && isatty::stdout_isatty()) {
if let Some(ui) = color::ColorUI::new(progress_bar) {
return Some(Box::new(ui));
}
}
Some(Box::new(plain::PlainUI::new()))
}
}
pub fn compression_percent(s: &Sizes) -> i64 {
if s.uncompressed > 0 {
100i64 - (100 * s.compressed / s.uncompressed) as i64
} else {
0
}
}
pub fn duration_to_hms(d: Duration) -> String {
let elapsed_secs = d.as_secs();
if elapsed_secs >= 3600 {
format!(
"{:2}:{:02}:{:02}",
elapsed_secs / 3600,
(elapsed_secs / 60) % 60,
elapsed_secs % 60
)
} else {
format!(" {:2}:{:02}", (elapsed_secs / 60) % 60, elapsed_secs % 60)
}
}
pub fn mbps_rate(bytes: u64, elapsed: Duration) -> f64 {
let secs = elapsed.as_secs() as f64 + f64::from(elapsed.subsec_millis()) / 1000.0;
if secs > 0.0 {
bytes as f64 / secs / 1e6
} else {
0f64
}
}
pub fn compression_ratio(s: &Sizes) -> f64 {
if s.compressed > 0 {
s.uncompressed as f64 / s.compressed as f64
} else {
0f64
}
}
#[cfg(test)]
mod tests {
use super::UI;
use crate::report::Sizes;
#[test]
pub fn by_name() {
assert!(UI::by_name("auto", true).is_some());
assert!(UI::by_name("plain", true).is_some());
}
#[test]
pub fn test_compression_ratio() {
let ratio = super::compression_ratio(&Sizes {
compressed: 2000,
uncompressed: 4000,
});
assert_eq!(format!("{:3.1}x", ratio), "2.0x");
}
}