Move all printing in cli to the console file

This commit is contained in:
Vincent Prouillet 2017-05-12 23:10:21 +09:00
parent 2a150299f3
commit 2fb4e2b01d
5 changed files with 72 additions and 69 deletions

View file

@ -3,11 +3,12 @@ use std::env;
use gutenberg::errors::Result;
use gutenberg::Site;
use console;
pub fn build(config_file: &str) -> Result<()> {
let mut site = Site::new(env::current_dir().unwrap(), config_file)?;
site.load()?;
super::notify_site_size(&site);
super::warn_about_ignored_pages(&site);
console::notify_site_size(&site);
console::warn_about_ignored_pages(&site);
site.build()
}

View file

@ -5,29 +5,3 @@ mod serve;
pub use self::init::create_new_project;
pub use self::build::build;
pub use self::serve::serve;
use gutenberg::Site;
use console::warn;
fn notify_site_size(site: &Site) {
println!(
"-> Creating {} pages ({} orphan) and {} sections",
site.pages.len(),
site.get_all_orphan_pages().len(),
site.sections.len() - 1, // -1 since we do not the index as a section
);
}
fn warn_about_ignored_pages(site: &Site) {
let ignored_pages = site.get_ignored_pages();
if !ignored_pages.is_empty() {
warn(&format!(
"{} page(s) ignored (missing date or order in a sorted section):",
ignored_pages.len()
));
for path in site.get_ignored_pages() {
warn(&format!("- {}", path.display()));
}
}
}

View file

@ -13,11 +13,8 @@ use ws::{WebSocket, Sender, Message};
use gutenberg::Site;
use gutenberg::errors::{Result, ResultExt};
use ::{report_elapsed_time, unravel_errors};
use console;
#[derive(Debug, PartialEq)]
enum ChangeKind {
Content,
@ -47,7 +44,7 @@ fn rebuild_done_handling(broadcaster: &Sender, res: Result<()>, reload_path: &st
}}"#, reload_path)
).unwrap();
},
Err(e) => unravel_errors("Failed to build the site", &e, false)
Err(e) => console::unravel_errors("Failed to build the site", &e)
}
}
@ -67,10 +64,10 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
site.load()?;
site.enable_live_reload();
super::notify_site_size(&site);
super::warn_about_ignored_pages(&site);
console::notify_site_size(&site);
console::warn_about_ignored_pages(&site);
site.build()?;
report_elapsed_time(start);
console::report_elapsed_time(start);
// Setup watchers
let (tx, rx) = channel();
@ -154,7 +151,7 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
}
},
};
report_elapsed_time(start);
console::report_elapsed_time(start);
}
_ => {}
}

View file

@ -1,6 +1,12 @@
use std::time::Instant;
use chrono::Duration;
use term_painter::ToStyle;
use term_painter::Color::*;
use gutenberg::errors::Error;
use gutenberg::Site;
pub fn info(message: &str) {
println!("{}", NotSet.bold().paint(message));
@ -17,3 +23,48 @@ pub fn success(message: &str) {
pub fn error(message: &str) {
println!("{}", Red.bold().paint(message));
}
/// Display in the console the number of pages/sections in the site
pub fn notify_site_size(site: &Site) {
println!(
"-> Creating {} pages ({} orphan) and {} sections",
site.pages.len(),
site.get_all_orphan_pages().len(),
site.sections.len() - 1, // -1 since we do not the index as a section
);
}
/// Display a warning in the console if there are ignored pages in the site
pub fn warn_about_ignored_pages(site: &Site) {
let ignored_pages = site.get_ignored_pages();
if !ignored_pages.is_empty() {
warn(&format!(
"{} page(s) ignored (missing date or order in a sorted section):",
ignored_pages.len()
));
for path in site.get_ignored_pages() {
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) {
self::error(message);
self::error(&format!("Error: {}", error));
for e in error.iter().skip(1) {
self::error(&format!("Reason: {}", e));
}
}

View file

@ -12,41 +12,12 @@ extern crate mount;
extern crate notify;
extern crate ws;
use std::time::Instant;
use chrono::Duration;
use gutenberg::errors::Error;
mod cmd;
mod console;
/// Print the time elapsed rounded to 1 decimal
fn report_elapsed_time(instant: Instant) {
let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64;
if duration_ms < 1000.0 {
console::success(&format!("Done in {}ms.\n", duration_ms));
} else {
let duration_sec = duration_ms / 1000.0;
console::success(&format!("Done in {:.1}s.\n", ((duration_sec * 10.0).round() / 10.0)));
}
}
////Display an error message, the actual error and then exits if requested
fn unravel_errors(message: &str, error: &Error, exit: bool) {
console::error(message);
console::error(&format!("Error: {}", error));
for e in error.iter().skip(1) {
console::error(&format!("Reason: {}", e));
}
if exit {
::std::process::exit(1);
}
}
fn main() {
let matches = clap_app!(Gutenberg =>
(version: crate_version!())
@ -74,15 +45,21 @@ fn main() {
("init", Some(matches)) => {
match cmd::create_new_project(matches.value_of("name").unwrap()) {
Ok(()) => console::success("Project created"),
Err(e) => unravel_errors("Failed to create the project", &e, true),
Err(e) => {
console::unravel_errors("Failed to create the project", &e);
::std::process::exit(1);
},
};
},
("build", Some(_)) => {
console::info("Building site...");
let start = Instant::now();
match cmd::build(config_file) {
Ok(()) => report_elapsed_time(start),
Err(e) => unravel_errors("Failed to build the site", &e, true),
Ok(()) => console::report_elapsed_time(start),
Err(e) => {
console::unravel_errors("Failed to build the site", &e);
::std::process::exit(1);
},
};
},
("serve", Some(matches)) => {
@ -91,7 +68,10 @@ fn main() {
console::info("Building site...");
match cmd::serve(interface, port, config_file) {
Ok(()) => (),
Err(e) => unravel_errors("Failed to build the site", &e, true),
Err(e) => {
console::unravel_errors("Failed to build the site", &e);
::std::process::exit(1);
},
};
},
_ => unreachable!(),