2018-09-22 14:05:07 +00:00
|
|
|
use std::env;
|
|
|
|
use std::io::Write;
|
2017-05-12 14:10:21 +00:00
|
|
|
use std::time::Instant;
|
|
|
|
|
2018-09-22 14:05:07 +00:00
|
|
|
use atty;
|
2017-05-12 14:10:21 +00:00
|
|
|
use chrono::Duration;
|
2018-09-22 14:05:07 +00:00
|
|
|
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
|
2017-03-25 06:52:51 +00:00
|
|
|
|
2017-07-01 07:47:41 +00:00
|
|
|
use errors::Error;
|
|
|
|
use site::Site;
|
2017-05-12 14:10:21 +00:00
|
|
|
|
2018-09-22 14:05:07 +00:00
|
|
|
lazy_static! {
|
|
|
|
/// Termcolor color choice.
|
|
|
|
/// We do not rely on ColorChoice::Auto behavior
|
|
|
|
/// as the check is already performed by has_color.
|
2018-10-02 14:42:34 +00:00
|
|
|
static ref COLOR_CHOICE: ColorChoice =
|
2018-09-22 14:05:07 +00:00
|
|
|
if has_color() {
|
|
|
|
ColorChoice::Always
|
|
|
|
} else {
|
|
|
|
ColorChoice::Never
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-25 06:52:51 +00:00
|
|
|
|
|
|
|
pub fn info(message: &str) {
|
2018-09-22 14:05:07 +00:00
|
|
|
colorize(message, ColorSpec::new().set_bold(true));
|
2017-03-25 06:52:51 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 10:29:37 +00:00
|
|
|
pub fn warn(message: &str) {
|
2018-09-22 14:05:07 +00:00
|
|
|
colorize(message, ColorSpec::new().set_bold(true).set_fg(Some(Color::Yellow)));
|
2017-05-08 10:29:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 06:52:51 +00:00
|
|
|
pub fn success(message: &str) {
|
2018-09-22 14:05:07 +00:00
|
|
|
colorize(message, ColorSpec::new().set_bold(true).set_fg(Some(Color::Green)));
|
2017-03-25 06:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn error(message: &str) {
|
2018-09-22 14:05:07 +00:00
|
|
|
colorize(message, ColorSpec::new().set_bold(true).set_fg(Some(Color::Red)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print a colorized message to stdout
|
|
|
|
fn colorize(message: &str, color: &ColorSpec) {
|
|
|
|
let mut stdout = StandardStream::stdout(*COLOR_CHOICE);
|
|
|
|
stdout.set_color(color).unwrap();
|
|
|
|
writeln!(&mut stdout, "{}", message).unwrap();
|
2017-03-25 06:52:51 +00:00
|
|
|
}
|
2017-05-12 14:10:21 +00:00
|
|
|
|
|
|
|
/// Display in the console the number of pages/sections in the site
|
|
|
|
pub fn notify_site_size(site: &Site) {
|
|
|
|
println!(
|
2018-02-02 20:35:04 +00:00
|
|
|
"-> Creating {} pages ({} orphan), {} sections, and processing {} images",
|
2018-10-02 14:42:34 +00:00
|
|
|
site.library.pages().len(),
|
2017-05-12 14:10:21 +00:00
|
|
|
site.get_all_orphan_pages().len(),
|
2018-10-02 14:42:34 +00:00
|
|
|
site.library.sections().len() - 1, // -1 since we do not the index as a section
|
2018-02-02 20:35:04 +00:00
|
|
|
site.num_img_ops(),
|
2017-05-12 14:10:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Display a warning in the console if there are ignored pages in the site
|
|
|
|
pub fn warn_about_ignored_pages(site: &Site) {
|
2018-10-02 14:42:34 +00:00
|
|
|
let ignored_pages: Vec<_> = site.library
|
|
|
|
.sections_values()
|
|
|
|
.iter()
|
|
|
|
.flat_map(|s| {
|
|
|
|
s.ignored_pages
|
|
|
|
.iter()
|
|
|
|
.map(|k| site.library.get_page_by_key(*k).file.path.clone())
|
|
|
|
})
|
2017-05-16 04:37:00 +00:00
|
|
|
.collect();
|
|
|
|
|
2017-05-12 14:10:21 +00:00
|
|
|
if !ignored_pages.is_empty() {
|
|
|
|
warn(&format!(
|
2018-07-31 13:39:10 +00:00
|
|
|
"{} page(s) ignored (missing date or weight in a sorted section):",
|
2017-05-12 14:10:21 +00:00
|
|
|
ignored_pages.len()
|
|
|
|
));
|
2017-05-16 04:37:00 +00:00
|
|
|
for path in ignored_pages {
|
2017-05-12 14:10:21 +00:00
|
|
|
warn(&format!("- {}", path.display()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print the time elapsed rounded to 1 decimal
|
|
|
|
pub fn report_elapsed_time(instant: Instant) {
|
|
|
|
let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64;
|
|
|
|
|
|
|
|
if duration_ms < 1000.0 {
|
|
|
|
success(&format!("Done in {}ms.\n", duration_ms));
|
|
|
|
} else {
|
|
|
|
let duration_sec = duration_ms / 1000.0;
|
|
|
|
success(&format!("Done in {:.1}s.\n", ((duration_sec * 10.0).round() / 10.0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Display an error message and the actual error(s)
|
|
|
|
pub fn unravel_errors(message: &str, error: &Error) {
|
2017-05-22 11:58:28 +00:00
|
|
|
if !message.is_empty() {
|
2017-05-12 14:10:21 +00:00
|
|
|
self::error(message);
|
2017-05-20 15:00:41 +00:00
|
|
|
}
|
|
|
|
self::error(&format!("Error: {}", error));
|
|
|
|
for e in error.iter().skip(1) {
|
|
|
|
self::error(&format!("Reason: {}", e));
|
|
|
|
}
|
2017-05-12 14:10:21 +00:00
|
|
|
}
|
2018-09-22 14:05:07 +00:00
|
|
|
|
|
|
|
/// Check whether to output colors
|
|
|
|
fn has_color() -> bool {
|
2018-09-30 19:15:09 +00:00
|
|
|
let use_colors = env::var("CLICOLOR").unwrap_or_else(|_| "1".to_string()) != "0" && env::var("NO_COLOR").is_err();
|
|
|
|
let force_colors = env::var("CLICOLOR_FORCE").unwrap_or_else(|_|"0".to_string()) != "0";
|
2018-09-22 14:05:07 +00:00
|
|
|
|
|
|
|
force_colors || use_colors && atty::is(atty::Stream::Stdout)
|
|
|
|
}
|