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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::borrow::Cow;
use std::cell::RefCell;
use std::iter::repeat;
use crate::format::{BinaryBytes, DecimalBytes, FormattedDuration, HumanBytes, HumanDuration};
use crate::progress::ProgressState;
use crate::utils::{expand_template, pad_str};
use console::{measure_text_width, Style};
#[derive(Clone, Debug)]
pub struct ProgressStyle {
tick_chars: Vec<char>,
progress_chars: Vec<char>,
template: Cow<'static, str>,
}
impl ProgressStyle {
pub fn default_bar() -> ProgressStyle {
ProgressStyle {
tick_chars: "⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈ "
.chars()
.collect(),
progress_chars: "█░".chars().collect(),
template: Cow::Borrowed("{wide_bar} {pos}/{len}"),
}
}
pub fn default_spinner() -> ProgressStyle {
ProgressStyle {
tick_chars: "⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈ "
.chars()
.collect(),
progress_chars: "█░".chars().collect(),
template: Cow::Borrowed("{spinner} {msg}"),
}
}
pub fn tick_chars(mut self, s: &str) -> ProgressStyle {
self.tick_chars = s.chars().collect();
self
}
pub fn progress_chars(mut self, s: &str) -> ProgressStyle {
self.progress_chars = s.chars().collect();
self
}
pub fn template(mut self, s: &str) -> ProgressStyle {
self.template = Cow::Owned(s.into());
self
}
pub fn get_tick_char(&self, idx: u64) -> char {
self.tick_chars[(idx as usize) % (self.tick_chars.len() - 1)]
}
pub fn get_final_tick_char(&self) -> char {
self.tick_chars[self.tick_chars.len() - 1]
}
pub(crate) fn format_bar(
&self,
state: &ProgressState,
width: usize,
alt_style: Option<&Style>,
) -> String {
let pct = state.fraction();
let fill = pct * width as f32;
let head = if pct > 0.0 && (fill as usize) < width {
1
} else {
0
};
let pb = repeat(state.style.progress_chars[0])
.take(fill as usize)
.collect::<String>();
let cur = if head == 1 {
let n = state.style.progress_chars.len().saturating_sub(2);
let cur_char = if n == 0 {
1
} else {
n.saturating_sub((fill * n as f32) as usize % n)
};
state.style.progress_chars[cur_char].to_string()
} else {
"".into()
};
let bg = width.saturating_sub(fill as usize).saturating_sub(head);
let rest = repeat(state.style.progress_chars.last().unwrap())
.take(bg)
.collect::<String>();
format!(
"{}{}{}",
pb,
cur,
alt_style.unwrap_or(&Style::new()).apply_to(rest)
)
}
pub(crate) fn format_state(&self, state: &ProgressState) -> Vec<String> {
let (pos, len) = state.position();
let mut rv = vec![];
for line in self.template.lines() {
let wide_element = RefCell::new(None);
let s = expand_template(line, |var| {
let key = var.key;
match key {
"wide_bar" => {
*wide_element.borrow_mut() = Some(var.duplicate_for_key("bar"));
"\x00".into()
}
"bar" => {
self.format_bar(state, var.width.unwrap_or(20), var.alt_style.as_ref())
}
"spinner" => state.current_tick_char().to_string(),
"wide_msg" => {
*wide_element.borrow_mut() = Some(var.duplicate_for_key("msg"));
"\x00".into()
}
"msg" => state.message().to_string(),
"prefix" => state.prefix().to_string(),
"pos" => pos.to_string(),
"len" => len.to_string(),
"percent" => format!("{:.*}", 0, state.fraction() * 100f32),
"bytes" => format!("{}", HumanBytes(state.pos)),
"total_bytes" => format!("{}", HumanBytes(state.len)),
"decimal_bytes" => format!("{}", DecimalBytes(state.pos)),
"decimal_total_bytes" => format!("{}", DecimalBytes(state.len)),
"binary_bytes" => format!("{}", BinaryBytes(state.pos)),
"binary_total_bytes" => format!("{}", BinaryBytes(state.len)),
"elapsed_precise" => format!("{}", FormattedDuration(state.started.elapsed())),
"elapsed" => format!("{:#}", HumanDuration(state.started.elapsed())),
"per_sec" => format!("{}/s", state.per_sec()),
"bytes_per_sec" => format!("{}/s", HumanBytes(state.per_sec())),
"eta_precise" => format!("{}", FormattedDuration(state.eta())),
"eta" => format!("{:#}", HumanDuration(state.eta())),
_ => "".into(),
}
});
rv.push(if let Some(ref var) = *wide_element.borrow() {
let total_width = state.width();
if var.key == "bar" {
let bar_width = total_width.saturating_sub(measure_text_width(&s));
s.replace(
"\x00",
&self.format_bar(state, bar_width, var.alt_style.as_ref()),
)
} else if var.key == "msg" {
let msg_width = total_width.saturating_sub(measure_text_width(&s));
let msg = pad_str(state.message(), msg_width, var.align, true);
s.replace(
"\x00",
if var.last_element {
msg.trim_end()
} else {
&msg
},
)
} else {
unreachable!()
}
} else {
s.to_string()
});
}
rv
}
}