From 6f768fe4add31a7ff1530f5f5e86915a094fc3bf Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 30 Apr 2018 19:44:20 +0200 Subject: [PATCH] Add a progressbar to the command Note: characters in config have to be escaped. --- bin/core/imag-diagnostics/Cargo.toml | 1 + bin/core/imag-diagnostics/src/main.rs | 46 ++++++++++++++++++++++++++- imagrc.toml | 10 ++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/bin/core/imag-diagnostics/Cargo.toml b/bin/core/imag-diagnostics/Cargo.toml index e0615a28..08c4e7c6 100644 --- a/bin/core/imag-diagnostics/Cargo.toml +++ b/bin/core/imag-diagnostics/Cargo.toml @@ -19,6 +19,7 @@ build = "../../../build.rs" log = "0.4" toml = "0.4" toml-query = "0.6" +indicatif = "0.9" libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" } libimagrt = { version = "0.9.0", path = "../../../lib/core/libimagrt" } diff --git a/bin/core/imag-diagnostics/src/main.rs b/bin/core/imag-diagnostics/src/main.rs index 88368864..d8d8482b 100644 --- a/bin/core/imag-diagnostics/src/main.rs +++ b/bin/core/imag-diagnostics/src/main.rs @@ -35,6 +35,7 @@ extern crate clap; extern crate toml; extern crate toml_query; +extern crate indicatif; #[macro_use] extern crate log; #[macro_use] extern crate libimagrt; @@ -44,6 +45,7 @@ extern crate libimagstore; use std::io::Write; +use libimagrt::runtime::Runtime; use libimagrt::setup::generate_runtime_setup; use libimagerror::trace::MapErrTrace; use libimagerror::io::ToExitCode; @@ -55,6 +57,7 @@ use libimagentrylink::internal::*; use toml::Value; use toml_query::read::TomlValueReadExt; +use indicatif::{ProgressBar, ProgressStyle}; use std::collections::BTreeMap; @@ -119,6 +122,20 @@ fn main() { ui::build_ui); let mut entries_counter = 0; + let template = get_config(&rt, "rt.progressbar_style"); + let tick_chars = get_config(&rt, "rt.progressticker_chars"); + + let style = if let Some(tick_chars) = tick_chars { + ProgressStyle::default_spinner().tick_chars(&tick_chars) + } else { + ProgressStyle::default_spinner() + }; + + let spinner = ProgressBar::new_spinner(); + spinner.enable_steady_tick(100); + spinner.set_style(style); + spinner.set_message("Accumulating data"); + let diags = rt.store() .entries() .map_err_trace_exit_unwrap(1) @@ -149,6 +166,17 @@ fn main() { .collect::, _>>() .map_err_trace_exit_unwrap(1); + spinner.finish(); + let n = diags.len(); + let progress = ProgressBar::new(n as u64); + let style = if let Some(template) = template { + ProgressStyle::default_bar().template(&template) + } else { + ProgressStyle::default_bar() + }; + progress.set_style(style); + progress.set_message("Calculating stats"); + let mut version_counts : BTreeMap = BTreeMap::new(); let mut sum_header_sections = 0; let mut sum_bytecount_content = 0; @@ -186,9 +214,11 @@ fn main() { max_internal_links = Some((diag.num_internal_links, diag.id.clone())); } } + + progress.inc(1); } - let n = diags.len(); + progress.finish(); let mut out = rt.stdout(); @@ -232,3 +262,17 @@ fn main() { } } +fn get_config(rt: &Runtime, s: &'static str) -> Option { + rt.config().and_then(|cfg| { + cfg.read(s) + .map_err_trace_exit_unwrap(1) + .map(|opt| match opt { + &Value::String(ref s) => s.to_owned(), + _ => { + error!("Config type wrong: 'rt.progressbar_style' should be a string"); + ::std::process::exit(1) + } + }) + }) +} + diff --git a/imagrc.toml b/imagrc.toml index 17f3fd7c..1371b933 100644 --- a/imagrc.toml +++ b/imagrc.toml @@ -4,6 +4,16 @@ [rt] editor = "vim" +# If the command uses a progress bar to indicate progress, this is the template +# for it. +# +# The docs for the template language can be found at +# +# https://docs.rs/indicatif +# +progressbar_style = "[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {msg}" +progressticker_chars = "|/-\\-/-\\" + # # imag supports templates when specifying formats. The templates support several # functionalities, from colorizing to underlining and such things.