Simple concurrent progress bars.
rayon
, etc.linya
is designed around the multi-bar case, and unlike other progress bar
libraries, has no separate type for individual bars. Instead, we use the
Progress
type, a "bar coordinator".
Progress
does not implement Clone
and must be wrapped in the usual
concurrent sharing types before being passed between threads:
```rust use std::sync::{Arc, Mutex}; use linya::{Bar, Progress}; use rayon::prelude::*;
let progress = Arc::new(Mutex::new(Progress::new()));
// into_par_iter()
is from rayon
, and lets us parallelize some
// operation over a collection "for free".
(0..10).intopariter().foreachwith(progress, |p, n| {
let bar: Bar = p.lock().unwrap().bar(50, format!("Downloading {}", n));
// ... Your logic ...
// Increment the bar and draw it immediately. // This is likely called in some inner loop or other closure. p.lock().unwrap().incanddraw(&bar, 10); }); ```
Notice that new bars are added on-the-fly from within forked threads. We
call Progress::bar
to obtain a new "bar handle", and then pass that
handle back to the parent Progress
when incrementing/drawing.
See Progress::inc_and_draw
and Progress::set_and_draw
to advance and
render the bars.
Progress
can also be used in a single-threaded context for individual
bars. The usage is the same, except that no locking is required:
```rust use linya::{Bar, Progress};
let mut progress = Progress::new(); let bar: Bar = progress.bar(50, "Downloading");
// Use in a loop, etc. progress.setanddraw(&bar, 10); ```
In this way, you could even have multi-bars in a single-threaded context.
Some of the points below may be fixed in future releases.
If you need more customizable progress bars and are willing to accept heavier dependencies, please consider [indicatif].
Note also that using more than one Progress
at the same time leads to
unspecified behaviour.
Linya is the Quenya word for "pool", as in a beautiful mountain pool.