From 2835636630eb6c99974c38d4ba8da2ed5590390c Mon Sep 17 00:00:00 2001 From: Elisabeth Henry Date: Thu, 5 Oct 2017 13:56:05 +0200 Subject: [PATCH] More work on progress bars --- Cargo.lock | 1 + Cargo.toml | 1 + src/bin/helpers.rs | 2 +- src/bin/real_main.rs | 26 +++++++++++----- src/lib/book.rs | 72 +++++++++++++++++++++++++++++--------------- src/lib/lib.rs | 1 + 6 files changed, 70 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 75455b0..b0cb732 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,7 @@ version = "0.14.0-pre" dependencies = [ "caribon 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.26.2 (registry+https://github.com/rust-lang/crates.io-index)", + "console 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "crowbook-intl 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "crowbook-intl-runtime 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "crowbook-text-processing 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 3092016..d335190 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,7 @@ crowbook-intl-runtime = "0.1" numerals = "0.1" epub-builder = "0.3" log = "0.3" +console = "0.5" indicatif = "0.7" caribon = { version = "0.8", optional = true } clap = { version = "2.19", optional = true } diff --git a/src/bin/helpers.rs b/src/bin/helpers.rs index dead632..41de98f 100644 --- a/src/bin/helpers.rs +++ b/src/bin/helpers.rs @@ -1,5 +1,5 @@ use crowbook::Book; -use clap::{App, Arg, ArgMatches, AppSettings}; +use clap::{App, Arg, ArgMatches, AppSettings}; use std::io::{self, Write}; use std::process::exit; diff --git a/src/bin/real_main.rs b/src/bin/real_main.rs index cfed33a..288c2d2 100644 --- a/src/bin/real_main.rs +++ b/src/bin/real_main.rs @@ -161,16 +161,26 @@ pub fn try_main() -> Result<()> { } book.set_options(&get_book_options(&matches)); - if matches.is_present("single") { - if s != "-" { - book.load_markdown_file(s)?; + { + let res = if matches.is_present("single") { + if s != "-" { + book.load_markdown_file(s) + } else { + book.read_markdown_config(io::stdin()) + } + } else if s != "-" { + book.load_file(s) } else { - book.read_markdown_config(io::stdin())?; + book.read_config(io::stdin()) + }.map(|_| ()); + + match res { + Ok(..) => {}, + Err(err) => { + book.set_error(&format!("{}", err)); + return Err(err); + } } - } else if s != "-" { - book.load_file(s)?; - } else { - book.read_config(io::stdin())?; } set_book_options(&mut book, &matches); diff --git a/src/lib/book.rs b/src/lib/book.rs index 517e5b3..6cb83e7 100644 --- a/src/lib/book.rs +++ b/src/lib/book.rs @@ -215,6 +215,18 @@ impl Book { book } + /// Sets an error message to the progress bar, if it is set + pub fn set_error(&mut self, msg: &str) { + if let Some(ref mainbar) = self.mainbar { + let sty = ProgressStyle::default_spinner() + .tick_chars("/|\\-X") + .template("{spinner:.dim.bold.red} {wide_msg}"); + mainbar.set_style(sty); + mainbar.set_message(msg); + } + } + + /// Adds a progress bar where where info should be written. /// /// See [indicatif doc](https://docs.rs/indicatif) for more information. @@ -226,10 +238,9 @@ impl Book { .unwrap() .add(ProgressBar::new_spinner()); let sty = ProgressStyle::default_spinner() - .tick_chars("/|\\-X") - .template("{spinner:.dim.bold.yellow} {prefix} {msg}"); + .tick_chars("/|\\-V") + .template("{spinner:.dim.bold.yellow} {prefix} {wide_msg}"); b.set_style(sty); - b.set_prefix(""); b.enable_steady_tick(200); self.mainbar = Some(b); self.guard = Some(thread::spawn(move || { @@ -495,7 +506,6 @@ impl Book { } if let Some(ref bar) = self.mainbar { - bar.set_prefix(&lformat!("Parsing book:")); bar.set_message(&lformat!("setting options")); bar.tick(); } @@ -746,7 +756,7 @@ impl Book { if let Some(bar) = bar { bar.set_style(ProgressStyle::default_spinner() .tick_chars("/|\\-X") - .template(&format!("{{spinner:.dim.bold.red}} {format}: {{msg:.red}}", + .template(&format!("{{spinner:.dim.bold.red}} {format}: {{wide_msg:.red}}", format = format))); bar.finish_with_message(&format!("{}", err)); } @@ -756,7 +766,7 @@ impl Book { if let Some(bar) = bar { bar.set_style(ProgressStyle::default_spinner() .tick_chars("/|\\- ") - .template(&format!("{{spinner:.dim.bold.cyan}} {format}: {{msg:.cyan}}", + .template(&format!("{{spinner:.dim.bold.cyan}} {format}: {{wide_msg:.cyan}}", format = format))); bar.finish_with_message(&lformat!("skipped")); } @@ -825,15 +835,14 @@ impl Book { let mut bars = vec![]; if let Some(ref multibar) = self.multibar { if let Some(ref mainbar) = self.mainbar { - mainbar.set_prefix(&lformat!("Rendering...")); - mainbar.set_message(""); + mainbar.set_message(&lformat!("Rendering...")); } for key in &keys { let bar = multibar.add(ProgressBar::new_spinner()); let sty = ProgressStyle::default_spinner() .tick_chars("/|\\-X") - .template(&format!("{{spinner:.dim.bold.yellow}} {format}: {{msg:.yellow}}", + .template(&format!("{{spinner:.dim.bold.yellow}} {format}: {{wide_msg:.yellow}}", format = key)); bar.set_style(sty); bar.enable_steady_tick(200); @@ -853,6 +862,14 @@ impl Book { } }); + if let Some(ref bar) = self.mainbar { + let sty = ProgressStyle::default_spinner() + .tick_chars("/|\\-V") + .template("{spinner:.dim.bold.cyan} {wide_msg}"); + bar.set_style(sty); + bar.set_message(&lformat!("Finished")); + } + // if handles.is_empty() { // Logger::display_warning(lformat!("Crowbook generated no file because no output file was \ // specified. Add output.{{format}} to your config file.")); @@ -941,7 +958,7 @@ impl Book { if let Some(bar) = bar { let sty = ProgressStyle::default_spinner() .tick_chars("/|\\-V") - .template(&format!("{{spinner:.dim.bold.cyan}} {format}: {{msg:.cyan}}", + .template(&format!("{{spinner:.dim.bold.cyan}} {format}: {{wide_msg:.cyan}}", format = format)); bar.set_style(sty); bar.finish_with_message(&lformat!("generated {path}", @@ -1022,33 +1039,46 @@ impl Book { // If one of the renderers requires it, perform grammarcheck if cfg!(feature = "proofread") && self.is_proofread() { + let normalized = misc::normalize(file); if let Some(ref checker) = self.checker { + if let Some(ref bar) = self.mainbar { + bar.set_message(&lformat!("Running languagetool on {file}...", file = &normalized)); + } + info!("{}", lformat!("Trying to run languagetool on {file}, this might take a \ while...", - file = misc::normalize(file))); + file = &normalized)); if let Err(err) = checker.check_chapter(&mut tokens) { error!("{}", lformat!("Error running languagetool on {file}: {error}", - file = misc::normalize(file), + file = &normalized, error = err)); } } if let Some(ref checker) = self.grammalecte { + if let Some(ref bar) = self.mainbar { + bar.set_message(&lformat!("Running grammalecte on {file}...", file = &normalized)); + } + info!("{}", lformat!("Trying to run grammalecte on {file}, this might take a \ while...", - file = misc::normalize(file))); + file = &normalized)); if let Err(err) = checker.check_chapter(&mut tokens) { error!("{}", lformat!("Error running grammalecte on {file}: {error}", - file = misc::normalize(file), + file = &normalized, error = err)); } } if let Some(ref detector) = self.detector { + if let Some(ref bar) = self.mainbar { + bar.set_message(&lformat!("Detecting repetitions in {file}...", file = &normalized)); + } + info!("{}", lformat!("Trying to run repetition detector on {file}, this might take a \ while...", - file = misc::normalize(file))); + file = &normalized)); if let Err(err) = detector.check_chapter(&mut tokens) { error!("{}", lformat!("Error running repetition detector on {file}: {error}", - file = misc::normalize(file), + file = &normalized, error = err)); } } @@ -1107,7 +1137,7 @@ impl Book { /// some error parsing it. pub fn add_chapter(&mut self, number: Number, file: &str) -> Result<&mut Self> { if let Some(ref bar) = self.mainbar { - bar.set_message(&lformat!("parsing chapter: {file}", + bar.set_message(&lformat!("Parsing {file}", file = misc::normalize(file))); bar.tick(); } @@ -1502,13 +1532,7 @@ impl Book { impl Drop for Book { fn drop(&mut self) { if let Some(ref bar) = self.mainbar { - let sty = ProgressStyle::default_spinner() - .tick_chars("/|\\-X") - .template("{spinner:.dim.bold.cyan} {prefix} {msg}"); - bar.set_style(sty); - bar.set_prefix(&lformat!("Finished")); - bar.tick(); - bar.finish_with_message(""); + bar.finish(); let guard = mem::replace(&mut self.guard, None); guard.unwrap() .join() diff --git a/src/lib/lib.rs b/src/lib/lib.rs index 3c6984c..cdb681f 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -118,6 +118,7 @@ extern crate numerals; extern crate epub_builder; extern crate uuid; extern crate indicatif; +extern crate console; #[macro_use] extern crate log; #[macro_use]