You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.1KB

  1. use std::time::Instant;
  2. use chrono::Duration;
  3. use term_painter::ToStyle;
  4. use term_painter::Color::*;
  5. use errors::Error;
  6. use site::Site;
  7. pub fn info(message: &str) {
  8. println!("{}", NotSet.bold().paint(message));
  9. }
  10. pub fn warn(message: &str) {
  11. println!("{}", Yellow.bold().paint(message));
  12. }
  13. pub fn success(message: &str) {
  14. println!("{}", Green.bold().paint(message));
  15. }
  16. pub fn error(message: &str) {
  17. println!("{}", Red.bold().paint(message));
  18. }
  19. /// Display in the console the number of pages/sections in the site
  20. pub fn notify_site_size(site: &Site) {
  21. println!(
  22. "-> Creating {} pages ({} orphan), {} sections, and processing {} images",
  23. site.pages.len(),
  24. site.get_all_orphan_pages().len(),
  25. site.sections.len() - 1, // -1 since we do not the index as a section
  26. site.num_img_ops(),
  27. );
  28. }
  29. /// Display a warning in the console if there are ignored pages in the site
  30. pub fn warn_about_ignored_pages(site: &Site) {
  31. let ignored_pages: Vec<_> = site.sections
  32. .values()
  33. .flat_map(|s| s.ignored_pages.iter().map(|p| p.file.path.clone()))
  34. .collect();
  35. if !ignored_pages.is_empty() {
  36. warn(&format!(
  37. "{} page(s) ignored (missing date or weight in a sorted section):",
  38. ignored_pages.len()
  39. ));
  40. for path in ignored_pages {
  41. warn(&format!("- {}", path.display()));
  42. }
  43. }
  44. }
  45. /// Print the time elapsed rounded to 1 decimal
  46. pub fn report_elapsed_time(instant: Instant) {
  47. let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64;
  48. if duration_ms < 1000.0 {
  49. success(&format!("Done in {}ms.\n", duration_ms));
  50. } else {
  51. let duration_sec = duration_ms / 1000.0;
  52. success(&format!("Done in {:.1}s.\n", ((duration_sec * 10.0).round() / 10.0)));
  53. }
  54. }
  55. /// Display an error message and the actual error(s)
  56. pub fn unravel_errors(message: &str, error: &Error) {
  57. if !message.is_empty() {
  58. self::error(message);
  59. }
  60. self::error(&format!("Error: {}", error));
  61. for e in error.iter().skip(1) {
  62. self::error(&format!("Reason: {}", e));
  63. }
  64. }