From 66b3d096dbe7b96ba396e08c33e567641d9e7900 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Thu, 30 Jan 2020 21:08:16 +0800 Subject: [PATCH] add indicatif --- indicatif/Cargo.toml | 10 ++++++++++ indicatif/src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 indicatif/Cargo.toml create mode 100644 indicatif/src/main.rs diff --git a/indicatif/Cargo.toml b/indicatif/Cargo.toml new file mode 100644 index 0000000..142f420 --- /dev/null +++ b/indicatif/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "indicatif" +version = "0.1.0" +authors = ["Hatter Jiang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +indicatif = "0.13.0" diff --git a/indicatif/src/main.rs b/indicatif/src/main.rs new file mode 100644 index 0000000..e5b928c --- /dev/null +++ b/indicatif/src/main.rs @@ -0,0 +1,38 @@ +use std::{ + cmp::min, + thread, + time::Duration +}; +use indicatif::{ + ProgressBar, + ProgressStyle +}; + +// https://crates.io/crates/indicatif +fn main() { + let mut downloaded = 0; + let total_size = 231231231; + + let pb = ProgressBar::new(total_size); + pb.set_style(ProgressStyle::default_bar() + .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})") + .progress_chars("#>-")); + + while downloaded < total_size { + let new = min(downloaded + 223211, total_size); + downloaded = new; + pb.set_position(new); + thread::sleep(Duration::from_millis(5)); + } + + pb.finish(); + // pb.finish_with_message("downloaded"); + + let pb2 = ProgressBar::new(1024); + for _ in 0..1024 { + pb2.inc(1); + thread::sleep(Duration::from_millis(5)); + } + pb2.finish(); + // pb2.finish_with_message("done"); +}