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.

77 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) and {} sections",
  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. );
  27. }
  28. /// Display a warning in the console if there are ignored pages in the site
  29. pub fn warn_about_ignored_pages(site: &Site) {
  30. let ignored_pages: Vec<_> = site.sections
  31. .values()
  32. .flat_map(|s| s.ignored_pages.iter().map(|p| p.file.path.clone()))
  33. .collect();
  34. if !ignored_pages.is_empty() {
  35. warn(&format!(
  36. "{} page(s) ignored (missing date or order in a sorted section):",
  37. ignored_pages.len()
  38. ));
  39. for path in ignored_pages {
  40. warn(&format!("- {}", path.display()));
  41. }
  42. }
  43. }
  44. /// Print the time elapsed rounded to 1 decimal
  45. pub fn report_elapsed_time(instant: Instant) {
  46. let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64;
  47. if duration_ms < 1000.0 {
  48. success(&format!("Done in {}ms.\n", duration_ms));
  49. } else {
  50. let duration_sec = duration_ms / 1000.0;
  51. success(&format!("Done in {:.1}s.\n", ((duration_sec * 10.0).round() / 10.0)));
  52. }
  53. }
  54. /// Display an error message and the actual error(s)
  55. pub fn unravel_errors(message: &str, error: &Error) {
  56. if !message.is_empty() {
  57. self::error(message);
  58. }
  59. self::error(&format!("Error: {}", error));
  60. for e in error.iter().skip(1) {
  61. self::error(&format!("Reason: {}", e));
  62. }
  63. }