1
0
mirror of https://github.com/lise-henry/crowbook synced 2024-09-22 13:21:37 +02:00

More work on progress bars

This commit is contained in:
Elisabeth Henry 2017-10-05 13:56:05 +02:00
parent 3ad6bc213c
commit 2835636630
6 changed files with 70 additions and 33 deletions

1
Cargo.lock generated
View File

@ -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)",

View File

@ -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 }

View File

@ -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;

View File

@ -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);

View File

@ -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()

View File

@ -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]