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.

71 lines
2.0KB

  1. use std::time::Instant;
  2. use chrono::Duration;
  3. use term_painter::ToStyle;
  4. use term_painter::Color::*;
  5. use gutenberg::errors::Error;
  6. use gutenberg::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 = site.get_ignored_pages();
  31. if !ignored_pages.is_empty() {
  32. warn(&format!(
  33. "{} page(s) ignored (missing date or order in a sorted section):",
  34. ignored_pages.len()
  35. ));
  36. for path in site.get_ignored_pages() {
  37. warn(&format!("- {}", path.display()));
  38. }
  39. }
  40. }
  41. /// Print the time elapsed rounded to 1 decimal
  42. pub fn report_elapsed_time(instant: Instant) {
  43. let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64;
  44. if duration_ms < 1000.0 {
  45. success(&format!("Done in {}ms.\n", duration_ms));
  46. } else {
  47. let duration_sec = duration_ms / 1000.0;
  48. success(&format!("Done in {:.1}s.\n", ((duration_sec * 10.0).round() / 10.0)));
  49. }
  50. }
  51. /// Display an error message and the actual error(s)
  52. pub fn unravel_errors(message: &str, error: &Error) {
  53. self::error(message);
  54. self::error(&format!("Error: {}", error));
  55. for e in error.iter().skip(1) {
  56. self::error(&format!("Reason: {}", e));
  57. }
  58. }