pub struct AvanceBar { /* private fields */ }Expand description
The progress bar
Implementations§
Source§impl AvanceBar
impl AvanceBar
Sourcepub fn new(total: u64) -> Self
pub fn new(total: u64) -> Self
Create a new progress bar
Examples found in repository?
6fn main() {
7 set_max_progress_bars(3);
8
9 std::thread::scope(|t| {
10 for i in 0..15 {
11 t.spawn(move || {
12 AvanceBar::new(1200)
13 .with_desc(format!("task{}", i))
14 .with_iter(0..1200)
15 .for_each(|_| thread::sleep(Duration::from_millis(3 + i % 5)));
16 });
17 }
18 });
19}More examples
6fn main() {
7 std::thread::scope(|t| {
8 t.spawn(|| {
9 AvanceBar::new(1200)
10 .with_desc("default")
11 .with_iter(0..1200)
12 .for_each(|_| thread::sleep(Duration::from_millis(3)));
13 });
14 t.spawn(|| {
15 AvanceBar::new(1000)
16 .with_style(Style::Balloon)
17 .with_desc("balloon")
18 .with_iter(0..1000)
19 .for_each(|_| thread::sleep(Duration::from_millis(5)));
20 });
21 t.spawn(|| {
22 AvanceBar::new(800)
23 .with_style_str("=>-") // user-defined style
24 .with_desc("custom")
25 .with_iter(0..800)
26 .for_each(|_| thread::sleep(Duration::from_millis(8)));
27 });
28 });
29}6fn main() {
7 let total = 1000;
8 let mut v = vec![0; total];
9
10 let pb1 = AvanceBar::new(total as u64)
11 .with_style(Style::Balloon)
12 .with_desc("8 workers");
13 std::thread::scope(|t| {
14 for chunk in v.chunks_mut(total / 8) {
15 t.spawn(|| {
16 pb1.with_iter(chunk.iter()).for_each(|_| {
17 thread::sleep(Duration::from_millis(3));
18 })
19 });
20 }
21 });
22 pb1.close();
23
24 std::thread::scope(|t| {
25 t.spawn(|| {
26 AvanceBar::with_config_of(&pb1)
27 .with_desc("1 worker")
28 .with_iter(v.iter_mut())
29 .for_each(|x| {
30 thread::sleep(Duration::from_millis(3));
31 *x = 2;
32 });
33 });
34 });
35}Sourcepub fn with_config_of(pb: &AvanceBar) -> Self
pub fn with_config_of(pb: &AvanceBar) -> Self
Build a new progress bar from the config of another progress bar. Only the configs and length of the old progress bar will be retained.
§Examples
let pb1 = AvanceBar::new(100)
.with_style(Style::Balloon)
.with_width(90)
.with_desc("task1");
// Reuse the style and width of pb1, but
// change the description and length.
let pb2 = AvanceBar::with_config_of(&pb1)
.with_total(200)
.with_desc("task2");Examples found in repository?
6fn main() {
7 let total = 1000;
8 let mut v = vec![0; total];
9
10 let pb1 = AvanceBar::new(total as u64)
11 .with_style(Style::Balloon)
12 .with_desc("8 workers");
13 std::thread::scope(|t| {
14 for chunk in v.chunks_mut(total / 8) {
15 t.spawn(|| {
16 pb1.with_iter(chunk.iter()).for_each(|_| {
17 thread::sleep(Duration::from_millis(3));
18 })
19 });
20 }
21 });
22 pb1.close();
23
24 std::thread::scope(|t| {
25 t.spawn(|| {
26 AvanceBar::with_config_of(&pb1)
27 .with_desc("1 worker")
28 .with_iter(v.iter_mut())
29 .for_each(|x| {
30 thread::sleep(Duration::from_millis(3));
31 *x = 2;
32 });
33 });
34 });
35}Sourcepub fn with_iter<Iter: Iterator>(&self, iter: Iter) -> AvanceIter<Iter> ⓘ
pub fn with_iter<Iter: Iterator>(&self, iter: Iter) -> AvanceIter<Iter> ⓘ
Wrap an iterator to display its progress.
See another way of progressing with an iterator at AvancesIterator
§Examples
let pb = AvanceBar::new(100);
for _ in pb.with_iter(0..100) {
// ...
}Examples found in repository?
6fn main() {
7 set_max_progress_bars(3);
8
9 std::thread::scope(|t| {
10 for i in 0..15 {
11 t.spawn(move || {
12 AvanceBar::new(1200)
13 .with_desc(format!("task{}", i))
14 .with_iter(0..1200)
15 .for_each(|_| thread::sleep(Duration::from_millis(3 + i % 5)));
16 });
17 }
18 });
19}More examples
6fn main() {
7 std::thread::scope(|t| {
8 t.spawn(|| {
9 AvanceBar::new(1200)
10 .with_desc("default")
11 .with_iter(0..1200)
12 .for_each(|_| thread::sleep(Duration::from_millis(3)));
13 });
14 t.spawn(|| {
15 AvanceBar::new(1000)
16 .with_style(Style::Balloon)
17 .with_desc("balloon")
18 .with_iter(0..1000)
19 .for_each(|_| thread::sleep(Duration::from_millis(5)));
20 });
21 t.spawn(|| {
22 AvanceBar::new(800)
23 .with_style_str("=>-") // user-defined style
24 .with_desc("custom")
25 .with_iter(0..800)
26 .for_each(|_| thread::sleep(Duration::from_millis(8)));
27 });
28 });
29}6fn main() {
7 let total = 1000;
8 let mut v = vec![0; total];
9
10 let pb1 = AvanceBar::new(total as u64)
11 .with_style(Style::Balloon)
12 .with_desc("8 workers");
13 std::thread::scope(|t| {
14 for chunk in v.chunks_mut(total / 8) {
15 t.spawn(|| {
16 pb1.with_iter(chunk.iter()).for_each(|_| {
17 thread::sleep(Duration::from_millis(3));
18 })
19 });
20 }
21 });
22 pb1.close();
23
24 std::thread::scope(|t| {
25 t.spawn(|| {
26 AvanceBar::with_config_of(&pb1)
27 .with_desc("1 worker")
28 .with_iter(v.iter_mut())
29 .for_each(|x| {
30 thread::sleep(Duration::from_millis(3));
31 *x = 2;
32 });
33 });
34 });
35}Sourcepub fn with_style(self, style: Style) -> Self
pub fn with_style(self, style: Style) -> Self
Builder-like function for a progress bar with a given style
(default: Style::ASCII).
See available styles in Style
§Examples
let pb = AvanceBar::new(1000).with_style(Style::Block);Examples found in repository?
6fn main() {
7 std::thread::scope(|t| {
8 t.spawn(|| {
9 AvanceBar::new(1200)
10 .with_desc("default")
11 .with_iter(0..1200)
12 .for_each(|_| thread::sleep(Duration::from_millis(3)));
13 });
14 t.spawn(|| {
15 AvanceBar::new(1000)
16 .with_style(Style::Balloon)
17 .with_desc("balloon")
18 .with_iter(0..1000)
19 .for_each(|_| thread::sleep(Duration::from_millis(5)));
20 });
21 t.spawn(|| {
22 AvanceBar::new(800)
23 .with_style_str("=>-") // user-defined style
24 .with_desc("custom")
25 .with_iter(0..800)
26 .for_each(|_| thread::sleep(Duration::from_millis(8)));
27 });
28 });
29}More examples
6fn main() {
7 let total = 1000;
8 let mut v = vec![0; total];
9
10 let pb1 = AvanceBar::new(total as u64)
11 .with_style(Style::Balloon)
12 .with_desc("8 workers");
13 std::thread::scope(|t| {
14 for chunk in v.chunks_mut(total / 8) {
15 t.spawn(|| {
16 pb1.with_iter(chunk.iter()).for_each(|_| {
17 thread::sleep(Duration::from_millis(3));
18 })
19 });
20 }
21 });
22 pb1.close();
23
24 std::thread::scope(|t| {
25 t.spawn(|| {
26 AvanceBar::with_config_of(&pb1)
27 .with_desc("1 worker")
28 .with_iter(v.iter_mut())
29 .for_each(|x| {
30 thread::sleep(Duration::from_millis(3));
31 *x = 2;
32 });
33 });
34 });
35}Sourcepub fn with_style_str(self, s: &'static str) -> Self
pub fn with_style_str(self, s: &'static str) -> Self
Builder-like function for a progress bar with user custom style
A custom style string is like |{Finished}{Current}{ToDo}|:
- Finished & ToDo: One character
- Current: One to many characters
Take "#0123456789 " as an example, the presentation of the bar will be like:
|######3 |
§Examples
let pb = AvanceBar::new(1000).with_style_str("=>-");Examples found in repository?
6fn main() {
7 std::thread::scope(|t| {
8 t.spawn(|| {
9 AvanceBar::new(1200)
10 .with_desc("default")
11 .with_iter(0..1200)
12 .for_each(|_| thread::sleep(Duration::from_millis(3)));
13 });
14 t.spawn(|| {
15 AvanceBar::new(1000)
16 .with_style(Style::Balloon)
17 .with_desc("balloon")
18 .with_iter(0..1000)
19 .for_each(|_| thread::sleep(Duration::from_millis(5)));
20 });
21 t.spawn(|| {
22 AvanceBar::new(800)
23 .with_style_str("=>-") // user-defined style
24 .with_desc("custom")
25 .with_iter(0..800)
26 .for_each(|_| thread::sleep(Duration::from_millis(8)));
27 });
28 });
29}Sourcepub fn with_width(self, width: u16) -> Self
pub fn with_width(self, width: u16) -> Self
Builder-like function for a progress bar with width
If width is larger than terminal width, progress bar will adjust to the terminal width.
§Examples
let pb = AvanceBar::new(1000).with_width(80);Sourcepub fn with_desc(self, desc: impl Into<Cow<'static, str>>) -> Self
pub fn with_desc(self, desc: impl Into<Cow<'static, str>>) -> Self
Builder-like function for a progress bar with description
§Examples
let pb = AvanceBar::new(1000).with_desc("task name");Examples found in repository?
6fn main() {
7 set_max_progress_bars(3);
8
9 std::thread::scope(|t| {
10 for i in 0..15 {
11 t.spawn(move || {
12 AvanceBar::new(1200)
13 .with_desc(format!("task{}", i))
14 .with_iter(0..1200)
15 .for_each(|_| thread::sleep(Duration::from_millis(3 + i % 5)));
16 });
17 }
18 });
19}More examples
6fn main() {
7 std::thread::scope(|t| {
8 t.spawn(|| {
9 AvanceBar::new(1200)
10 .with_desc("default")
11 .with_iter(0..1200)
12 .for_each(|_| thread::sleep(Duration::from_millis(3)));
13 });
14 t.spawn(|| {
15 AvanceBar::new(1000)
16 .with_style(Style::Balloon)
17 .with_desc("balloon")
18 .with_iter(0..1000)
19 .for_each(|_| thread::sleep(Duration::from_millis(5)));
20 });
21 t.spawn(|| {
22 AvanceBar::new(800)
23 .with_style_str("=>-") // user-defined style
24 .with_desc("custom")
25 .with_iter(0..800)
26 .for_each(|_| thread::sleep(Duration::from_millis(8)));
27 });
28 });
29}6fn main() {
7 let total = 1000;
8 let mut v = vec![0; total];
9
10 let pb1 = AvanceBar::new(total as u64)
11 .with_style(Style::Balloon)
12 .with_desc("8 workers");
13 std::thread::scope(|t| {
14 for chunk in v.chunks_mut(total / 8) {
15 t.spawn(|| {
16 pb1.with_iter(chunk.iter()).for_each(|_| {
17 thread::sleep(Duration::from_millis(3));
18 })
19 });
20 }
21 });
22 pb1.close();
23
24 std::thread::scope(|t| {
25 t.spawn(|| {
26 AvanceBar::with_config_of(&pb1)
27 .with_desc("1 worker")
28 .with_iter(v.iter_mut())
29 .for_each(|x| {
30 thread::sleep(Duration::from_millis(3));
31 *x = 2;
32 });
33 });
34 });
35}Sourcepub fn with_total(self, total: u64) -> Self
pub fn with_total(self, total: u64) -> Self
Builder-like function for a progress bar with length
Useful when you reuse some configs of another progress bar, but want to change the length.
§Examples
let pb1 = AvanceBar::new(100)
.with_style(Style::Balloon)
.with_width(90);
// Reuse pb1's config, but override the length.
let pb2 = AvanceBar::with_config_of(&pb1).with_total(200);Sourcepub fn with_unit_scale(self, unit_scale: bool) -> Self
pub fn with_unit_scale(self, unit_scale: bool) -> Self
Builder-like function for displaying human readable numbers in a progress bar.
If unit_scale (default: false) is set true, prints the number of iterations with an appropriate SI metric prefix (k = 10^3, M = 10^6, etc.)
Sourcepub fn set_postfix(&self, postfix: impl Into<Cow<'static, str>>)
pub fn set_postfix(&self, postfix: impl Into<Cow<'static, str>>)
Override the postfix of a progress bar.
Postfix is usually used for dynamically displaying some additional information, such as the accuracy when training a model.
See AvanceIter::with_pb if you want to change the postfix when
progressing with an iterator.
Examples found in repository?
6fn main() {
7 // Suppose we're training a model and here's 200 epoches
8 let epoch = 200;
9 let mut accuracy = 30.0;
10
11 (0..epoch)
12 .avance()
13 .with_style(Style::Block)
14 .with_pb()
15 .for_each(|(_, pb)| {
16 thread::sleep(Duration::from_millis(20));
17
18 accuracy += 0.34;
19
20 // Display the accuracy through the postfix
21 pb.set_postfix(format!("acc={:.2}", accuracy));
22 });
23}Sourcepub fn close(&self)
pub fn close(&self)
Manually stop the progress bar, and leave the current progress on terminal. Usually users don’t have to call this method directly, as a progress bar will be closed automatically when dropped.
Users should close a bar manually when they want to preserve the rendering order of progress bars, otherwise, progress bars will be closed in the order of being dropped (Closing order is the same as the rendering order).
Examples found in repository?
6fn main() {
7 let total = 1000;
8 let mut v = vec![0; total];
9
10 let pb1 = AvanceBar::new(total as u64)
11 .with_style(Style::Balloon)
12 .with_desc("8 workers");
13 std::thread::scope(|t| {
14 for chunk in v.chunks_mut(total / 8) {
15 t.spawn(|| {
16 pb1.with_iter(chunk.iter()).for_each(|_| {
17 thread::sleep(Duration::from_millis(3));
18 })
19 });
20 }
21 });
22 pb1.close();
23
24 std::thread::scope(|t| {
25 t.spawn(|| {
26 AvanceBar::with_config_of(&pb1)
27 .with_desc("1 worker")
28 .with_iter(v.iter_mut())
29 .for_each(|x| {
30 thread::sleep(Duration::from_millis(3));
31 *x = 2;
32 });
33 });
34 });
35}Sourcepub fn set_style(&self, style: Style)
pub fn set_style(&self, style: Style)
Set the style (default: Style::ASCII) of a progress bar.
Sourcepub fn set_style_str(&self, s: impl Into<Cow<'static, str>>)
pub fn set_style_str(&self, s: impl Into<Cow<'static, str>>)
Set the user-custom style of a progress bar.
Sourcepub fn set_desc(&self, desc: impl Into<Cow<'static, str>>)
pub fn set_desc(&self, desc: impl Into<Cow<'static, str>>)
Set the description (prefix) of a progress bar.
Sourcepub fn set_unit_scale(&self, unit_scale: bool)
pub fn set_unit_scale(&self, unit_scale: bool)
If unit_scale (default: false) is set true, prints the number of iterations with an appropriate SI metric prefix.