AvanceBar

Struct AvanceBar 

Source
pub struct AvanceBar { /* private fields */ }
Expand description

The progress bar

Implementations§

Source§

impl AvanceBar

Source

pub fn new(total: u64) -> Self

Create a new progress bar

Examples found in repository?
examples/many-bars.rs (line 12)
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
Hide additional examples
examples/multi.rs (line 9)
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}
examples/concurrent.rs (line 10)
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}
Source

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?
examples/concurrent.rs (line 26)
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}
Source

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?
examples/many-bars.rs (line 14)
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
Hide additional examples
examples/multi.rs (line 11)
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}
examples/concurrent.rs (line 16)
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}
Source

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?
examples/multi.rs (line 16)
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
Hide additional examples
examples/concurrent.rs (line 11)
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}
Source

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?
examples/multi.rs (line 23)
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}
Source

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);
Source

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?
examples/many-bars.rs (line 13)
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
Hide additional examples
examples/multi.rs (line 10)
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}
examples/concurrent.rs (line 12)
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}
Source

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);
Source

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.)

Source

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?
examples/train.rs (line 21)
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}
Source

pub fn update(&self, n: u64)

Advance the progress bar by n steps.

Source

pub fn inc(&self)

Advance the progress bar by one step, with the same effect as update(1). If you don’t want to invoke inc manually, see another method at with_iter.

§Examples
let pb = AvanceBar::new(1000);
for _ in 0..1000 {
    // ...
    pb.inc();
}
Source

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?
examples/concurrent.rs (line 22)
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}
Source

pub fn set_style(&self, style: Style)

Set the style (default: Style::ASCII) of a progress bar.

Source

pub fn set_style_str(&self, s: impl Into<Cow<'static, str>>)

Set the user-custom style of a progress bar.

Source

pub fn set_width(&self, width: u16)

Set a progress bar’s width

Source

pub fn set_desc(&self, desc: impl Into<Cow<'static, str>>)

Set the description (prefix) of a progress bar.

Source

pub fn set_total(&self, total: u64)

Set the length of a progress bar.

Source

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.

Trait Implementations§

Source§

impl Clone for AvanceBar

Source§

fn clone(&self) -> AvanceBar

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AvanceBar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.