From 2fb4e2b01dcd74d79e3251242c50192035a53b33 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 12 May 2017 23:10:21 +0900 Subject: [PATCH] Move all printing in cli to the console file --- src/bin/cmd/build.rs | 5 +++-- src/bin/cmd/mod.rs | 26 ---------------------- src/bin/cmd/serve.rs | 13 +++++------ src/bin/console.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++ src/bin/gutenberg.rs | 46 +++++++++++---------------------------- 5 files changed, 72 insertions(+), 69 deletions(-) diff --git a/src/bin/cmd/build.rs b/src/bin/cmd/build.rs index 6f65f672..69dc2c7b 100644 --- a/src/bin/cmd/build.rs +++ b/src/bin/cmd/build.rs @@ -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() } diff --git a/src/bin/cmd/mod.rs b/src/bin/cmd/mod.rs index d1f3dbfb..e3c62b5e 100644 --- a/src/bin/cmd/mod.rs +++ b/src/bin/cmd/mod.rs @@ -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())); - } - } -} diff --git a/src/bin/cmd/serve.rs b/src/bin/cmd/serve.rs index 9439ab87..9c3e822d 100644 --- a/src/bin/cmd/serve.rs +++ b/src/bin/cmd/serve.rs @@ -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); } _ => {} } diff --git a/src/bin/console.rs b/src/bin/console.rs index b758bd53..ad6b5a09 100644 --- a/src/bin/console.rs +++ b/src/bin/console.rs @@ -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)); + } +} diff --git a/src/bin/gutenberg.rs b/src/bin/gutenberg.rs index 73308b61..2524990f 100644 --- a/src/bin/gutenberg.rs +++ b/src/bin/gutenberg.rs @@ -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!(),