-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathstyle.rs
556 lines (505 loc) · 19.7 KB
/
style.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
use std::borrow::Cow;
use std::collections::HashMap;
use console::{measure_text_width, Style};
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
#[cfg(feature = "improved_unicode")]
use unicode_segmentation::UnicodeSegmentation;
use crate::format::{BinaryBytes, DecimalBytes, FormattedDuration, HumanBytes, HumanDuration};
use crate::state::ProgressState;
/// Controls the rendering style of progress bars
#[derive(Clone)]
pub struct ProgressStyle {
tick_strings: Vec<Box<str>>,
progress_chars: Vec<Box<str>>,
template: Box<str>,
on_finish: ProgressFinish,
// how unicode-big each char in progress_chars is
char_width: usize,
format_map: FormatMap,
}
#[cfg(feature = "improved_unicode")]
fn segment(s: &str) -> Vec<Box<str>> {
UnicodeSegmentation::graphemes(s, true)
.map(|s| s.into())
.collect()
}
#[cfg(not(feature = "improved_unicode"))]
fn segment(s: &str) -> Vec<Box<str>> {
s.chars().map(|x| x.to_string().into()).collect()
}
#[cfg(feature = "improved_unicode")]
fn measure(s: &str) -> usize {
unicode_width::UnicodeWidthStr::width(s)
}
#[cfg(not(feature = "improved_unicode"))]
fn measure(s: &str) -> usize {
s.chars().count()
}
/// finds the unicode-aware width of the passed grapheme cluters
/// panics on an empty parameter, or if the characters are not equal-width
fn width(c: &[Box<str>]) -> usize {
c.iter()
.map(|s| measure(s.as_ref()))
.fold(None, |acc, new| {
match acc {
None => return Some(new),
Some(old) => assert_eq!(old, new, "got passed un-equal width progress characters"),
}
acc
})
.unwrap()
}
impl ProgressStyle {
/// Returns the default progress bar style for bars
pub fn default_bar() -> ProgressStyle {
let progress_chars = segment("█░");
let char_width = width(&progress_chars);
ProgressStyle {
tick_strings: "⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈ "
.chars()
.map(|c| c.to_string().into())
.collect(),
progress_chars,
char_width,
template: "{wide_bar} {pos}/{len}".into(),
on_finish: ProgressFinish::default(),
format_map: FormatMap::default(),
}
}
/// Returns the default progress bar style for spinners
pub fn default_spinner() -> ProgressStyle {
let progress_chars = segment("█░");
let char_width = width(&progress_chars);
ProgressStyle {
tick_strings: "⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈ "
.chars()
.map(|c| c.to_string().into())
.collect(),
progress_chars,
char_width,
template: "{spinner} {msg}".into(),
on_finish: ProgressFinish::default(),
format_map: FormatMap::default(),
}
}
/// Sets the tick character sequence for spinners
pub fn tick_chars(mut self, s: &str) -> ProgressStyle {
self.tick_strings = s.chars().map(|c| c.to_string().into()).collect();
// Format bar will panic with some potentially confusing message, better to panic here
// with a message explicitly informing of the problem
assert!(
self.tick_strings.len() >= 2,
"at least 2 tick chars required"
);
self
}
/// Sets the tick string sequence for spinners
pub fn tick_strings(mut self, s: &[&str]) -> ProgressStyle {
self.tick_strings = s.iter().map(|s| s.to_string().into()).collect();
// Format bar will panic with some potentially confusing message, better to panic here
// with a message explicitly informing of the problem
assert!(
self.progress_chars.len() >= 2,
"at least 2 tick strings required"
);
self
}
/// Sets the progress characters `(filled, current, to do)`
///
/// You can pass more than three for a more detailed display.
/// All passed grapheme clusters need to be of equal width.
pub fn progress_chars(mut self, s: &str) -> ProgressStyle {
self.progress_chars = segment(s);
// Format bar will panic with some potentially confusing message, better to panic here
// with a message explicitly informing of the problem
assert!(
self.progress_chars.len() >= 2,
"at least 2 progress chars required"
);
self.char_width = width(&self.progress_chars);
self
}
/// Adds a custom key that references a `&ProgressState` to the template
pub fn with_key(mut self, key: &'static str, f: Format) -> ProgressStyle {
self.format_map.0.insert(key, f);
self
}
/// Sets the template string for the progress bar
///
/// Review the [list of template keys](./index.html#templates) for more information.
pub fn template(mut self, s: &str) -> ProgressStyle {
self.template = s.into();
self
}
/// Sets the finish behavior for the progress bar
///
/// This behavior is invoked when [`ProgressBar`] or
/// [`ProgressBarIter`] completes and
/// [`ProgressBar::is_finished()`] is false.
/// If you don't want the progress bar to be automatically finished then
/// call `on_finish(None)`.
///
/// [`ProgressBar`]: crate::ProgressBar
/// [`ProgressBarIter`]: crate::ProgressBarIter
/// [`ProgressBar::is_finished()`]: crate::ProgressBar::is_finished
pub fn on_finish(mut self, finish: ProgressFinish) -> ProgressStyle {
self.on_finish = finish;
self
}
/// Returns the tick char for a given number
#[deprecated(since = "0.13.0", note = "Deprecated in favor of get_tick_str")]
pub fn get_tick_char(&self, idx: u64) -> char {
self.get_tick_str(idx).chars().next().unwrap_or(' ')
}
/// Returns the tick string for a given number
pub fn get_tick_str(&self, idx: u64) -> &str {
&self.tick_strings[(idx as usize) % (self.tick_strings.len() - 1)]
}
/// Returns the tick char for the finished state
#[deprecated(since = "0.13.0", note = "Deprecated in favor of get_final_tick_str")]
pub fn get_final_tick_char(&self) -> char {
self.get_final_tick_str().chars().next().unwrap_or(' ')
}
/// Returns the tick string for the finished state
pub fn get_final_tick_str(&self) -> &str {
&self.tick_strings[self.tick_strings.len() - 1]
}
/// Returns the finish behavior
pub fn get_on_finish(&self) -> &ProgressFinish {
&self.on_finish
}
pub(crate) fn format_bar(&self, fract: f32, width: usize, alt_style: Option<&Style>) -> String {
// The number of clusters from progress_chars to write (rounding down).
let width = width / self.char_width;
// The number of full clusters (including a fractional component for a partially-full one).
let fill = fract * width as f32;
// The number of entirely full clusters (by truncating `fill`).
let entirely_filled = fill as usize;
// 1 if the bar is not entirely empty or full (meaning we need to draw the "current"
// character between the filled and "to do" segment), 0 otherwise.
let head = if fill > 0.0 && entirely_filled < width {
1
} else {
0
};
let pb = self.progress_chars[0].repeat(entirely_filled);
let cur = if head == 1 {
// Number of fine-grained progress entries in progress_chars.
let n = self.progress_chars.len().saturating_sub(2);
let cur_char = if n <= 1 {
// No fine-grained entries. 1 is the single "current" entry if we have one, the "to
// do" entry if not.
1
} else {
// Pick a fine-grained entry, ranging from the last one (n) if the fractional part
// of fill is 0 to the first one (1) if the fractional part of fill is almost 1.
n.saturating_sub((fill.fract() * n as f32) as usize)
};
self.progress_chars[cur_char].to_string()
} else {
"".into()
};
// Number of entirely empty clusters needed to fill the bar up to `width`.
let bg = width.saturating_sub(entirely_filled).saturating_sub(head);
let rest = self.progress_chars[self.progress_chars.len() - 1].repeat(bg);
format!(
"{}{}{}",
pb,
cur,
alt_style.unwrap_or(&Style::new()).apply_to(rest)
)
}
pub(crate) fn format_state(&self, state: &ProgressState) -> Vec<String> {
let mut rv = vec![];
for line in self.template.lines() {
let mut wide_element = None;
let s = expand_template(line, |var| {
if let Some(formatter) = self.format_map.0.get(var.key) {
return formatter(state);
}
match var.key {
"wide_bar" => {
wide_element = Some(var.duplicate_for_key("bar"));
"\x00".into()
}
"bar" => self.format_bar(
state.fraction(),
var.width.unwrap_or(20),
var.alt_style.as_ref(),
),
"spinner" => state.current_tick_str().to_string(),
"wide_msg" => {
wide_element = Some(var.duplicate_for_key("msg"));
"\x00".into()
}
"msg" => state.message().to_string(),
"prefix" => state.prefix().to_string(),
"pos" => state.pos.to_string(),
"len" => state.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!("{:.4}/s", state.per_sec()),
"bytes_per_sec" => format!("{}/s", HumanBytes(state.per_sec() as u64)),
"binary_bytes_per_sec" => {
format!("{}/s", BinaryBytes(state.per_sec() as u64))
}
"eta_precise" => format!("{}", FormattedDuration(state.eta())),
"eta" => format!("{:#}", HumanDuration(state.eta())),
"duration_precise" => format!("{}", FormattedDuration(state.duration())),
"duration" => format!("{:#}", HumanDuration(state.duration())),
_ => "".into(),
}
});
rv.push(if let Some(ref var) = wide_element {
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.fraction(), 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
}
}
fn expand_template<F: FnMut(&TemplateVar<'_>) -> String>(s: &str, mut f: F) -> Cow<'_, str> {
static VAR_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\}\})|\{(\{|[^{}}]+\})").unwrap());
static KEY_RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
([^:]+)
(?:
:
([<^>])?
([0-9]+)?
(!)?
(?:\.([0-9a-z_]+(?:\.[0-9a-z_]+)*))?
(?:/([a-z_]+(?:\.[a-z_]+)*))?
)?
",
)
.unwrap()
});
VAR_RE.replace_all(s, |caps: &Captures<'_>| {
if caps.get(1).is_some() {
return "}".into();
}
let key = &caps[2];
if key == "{" {
return "{".into();
}
let mut var = TemplateVar {
key,
align: Alignment::Left,
truncate: false,
width: None,
style: None,
alt_style: None,
last_element: caps.get(0).unwrap().end() >= s.len(),
};
if let Some(opt_caps) = KEY_RE.captures(&key[..key.len() - 1]) {
if let Some(short_key) = opt_caps.get(1) {
var.key = short_key.as_str();
}
var.align = match opt_caps.get(2).map(|x| x.as_str()) {
Some("<") => Alignment::Left,
Some("^") => Alignment::Center,
Some(">") => Alignment::Right,
_ => Alignment::Left,
};
if let Some(width) = opt_caps.get(3) {
var.width = Some(width.as_str().parse().unwrap());
}
if opt_caps.get(4).is_some() {
var.truncate = true;
}
if let Some(style) = opt_caps.get(5) {
var.style = Some(Style::from_dotted_str(style.as_str()));
}
if let Some(alt_style) = opt_caps.get(6) {
var.alt_style = Some(Style::from_dotted_str(alt_style.as_str()));
}
}
let mut rv = f(&var);
if let Some(width) = var.width {
rv = pad_str(&rv, width, var.align, var.truncate).to_string()
}
if let Some(s) = var.style {
rv = s.apply_to(rv).to_string();
}
rv
})
}
fn pad_str(s: &str, width: usize, align: Alignment, truncate: bool) -> Cow<'_, str> {
let cols = measure_text_width(s);
if cols >= width {
return if truncate {
Cow::Borrowed(s.get(..width).unwrap_or(s))
} else {
Cow::Borrowed(s)
};
}
let diff = width.saturating_sub(cols);
let (left_pad, right_pad) = match align {
Alignment::Left => (0, diff),
Alignment::Right => (diff, 0),
Alignment::Center => (diff / 2, diff.saturating_sub(diff / 2)),
};
let mut rv = String::new();
for _ in 0..left_pad {
rv.push(' ');
}
rv.push_str(s);
for _ in 0..right_pad {
rv.push(' ');
}
Cow::Owned(rv)
}
#[derive(Clone, Default)]
struct FormatMap(HashMap<&'static str, Format>);
pub type Format = fn(&ProgressState) -> String;
/// Behavior of a progress bar when it is finished
///
/// This is invoked when a [`ProgressBar`] or [`ProgressBarIter`] completes and
/// [`ProgressBar::is_finished`] is false.
///
/// [`ProgressBar`]: crate::ProgressBar
/// [`ProgressBarIter`]: crate::ProgressBarIter
/// [`ProgressBar::is_finished`]: crate::ProgressBar::is_finished
#[derive(Clone, Debug)]
pub enum ProgressFinish {
/// Finishes the progress bar and leaves the current message
///
/// Same behavior as calling [`ProgressBar::finish()`](crate::ProgressBar::finish).
AndLeave,
/// Finishes the progress bar at current position and leaves the current message
///
/// Same behavior as calling [`ProgressBar::finish_at_current_pos()`](crate::ProgressBar::finish_at_current_pos).
AtCurrentPos,
/// Finishes the progress bar and sets a message
///
/// Same behavior as calling [`ProgressBar::finish_with_message()`](crate::ProgressBar::finish_with_message).
WithMessage(Cow<'static, str>),
/// Finishes the progress bar and completely clears it (this is the default)
///
/// Same behavior as calling [`ProgressBar::finish_and_clear()`](crate::ProgressBar::finish_and_clear).
AndClear,
/// Finishes the progress bar and leaves the current message and progress
///
/// Same behavior as calling [`ProgressBar::abandon()`](crate::ProgressBar::abandon).
Abandon,
/// Finishes the progress bar and sets a message, and leaves the current progress
///
/// Same behavior as calling [`ProgressBar::abandon_with_message()`](crate::ProgressBar::abandon_with_message).
AbandonWithMessage(Cow<'static, str>),
}
impl Default for ProgressFinish {
fn default() -> Self {
Self::AndClear
}
}
#[derive(Debug)]
struct TemplateVar<'a> {
pub key: &'a str,
pub align: Alignment,
pub truncate: bool,
pub width: Option<usize>,
pub style: Option<Style>,
pub alt_style: Option<Style>,
pub last_element: bool,
}
impl<'a> TemplateVar<'a> {
fn duplicate_for_key<'b>(&self, key: &'b str) -> TemplateVar<'b> {
TemplateVar {
key,
align: self.align,
truncate: self.truncate,
width: self.width,
style: self.style.clone(),
alt_style: self.alt_style.clone(),
last_element: self.last_element,
}
}
}
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
enum Alignment {
Left,
Center,
Right,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_expand_template() {
let rv = expand_template("{{ {foo} {bar} }}", |var| var.key.to_uppercase());
assert_eq!(&rv, "{ FOO BAR }");
let rv = expand_template(r#"{ "foo": "{foo}", "bar": {bar} }"#, |var| {
var.key.to_uppercase()
});
assert_eq!(&rv, r#"{ "foo": "FOO", "bar": BAR }"#);
}
#[test]
fn test_expand_template_flags() {
use console::set_colors_enabled;
set_colors_enabled(true);
let rv = expand_template("{foo:5}", |var| {
assert_eq!(var.key, "foo");
assert_eq!(var.width, Some(5));
"XXX".into()
});
assert_eq!(&rv, "XXX ");
let rv = expand_template("{foo:.red.on_blue}", |var| {
assert_eq!(var.key, "foo");
assert_eq!(var.width, None);
assert_eq!(var.align, Alignment::Left);
assert_eq!(var.style, Some(Style::new().red().on_blue()));
"XXX".into()
});
assert_eq!(&rv, "\u{1b}[31m\u{1b}[44mXXX\u{1b}[0m");
let rv = expand_template("{foo:^5.red.on_blue}", |var| {
assert_eq!(var.key, "foo");
assert_eq!(var.width, Some(5));
assert_eq!(var.align, Alignment::Center);
assert_eq!(var.style, Some(Style::new().red().on_blue()));
"XXX".into()
});
assert_eq!(&rv, "\u{1b}[31m\u{1b}[44m XXX \u{1b}[0m");
let rv = expand_template("{foo:^5.red.on_blue/green.on_cyan}", |var| {
assert_eq!(var.key, "foo");
assert_eq!(var.width, Some(5));
assert_eq!(var.align, Alignment::Center);
assert_eq!(var.style, Some(Style::new().red().on_blue()));
assert_eq!(var.alt_style, Some(Style::new().green().on_cyan()));
"XXX".into()
});
assert_eq!(&rv, "\u{1b}[31m\u{1b}[44m XXX \u{1b}[0m");
}
}