| 1 |
use std::time::Duration; |
| 2 |
|
| 3 |
use indicatif::ProgressBar; |
| 4 |
use tokio::runtime; |
| 5 |
use tokio::time::interval; |
| 6 |
|
| 7 |
fn main() { |
| 8 |
// Plain progress bar, totaling 1024 steps. |
| 9 |
let steps = 1024; |
| 10 |
let pb = ProgressBar::new(steps); |
| 11 |
|
| 12 |
// Stream of events, triggering every 5ms. |
| 13 |
let rt = runtime::Builder::new_current_thread() |
| 14 |
.enable_time() |
| 15 |
.build() |
| 16 |
.expect("failed to create runtime"); |
| 17 |
|
| 18 |
// Future computation which runs for `steps` interval events, |
| 19 |
// incrementing one step of the progress bar each time. |
| 20 |
let future = async { |
| 21 |
let mut intv = interval(Duration::from_millis(5)); |
| 22 |
|
| 23 |
for _ in 0..steps { |
| 24 |
intv.tick().await; |
| 25 |
pb.inc(1); |
| 26 |
} |
| 27 |
}; |
| 28 |
|
| 29 |
// Drive the future to completion, blocking until done. |
| 30 |
rt.block_on(future); |
| 31 |
|
| 32 |
// Mark the progress bar as finished. |
| 33 |
pb.finish(); |
| 34 |
} |