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.

131 lines
4.1KB

  1. use std::env;
  2. use std::error::Error as StdError;
  3. use std::io::Write;
  4. use std::time::Instant;
  5. use atty;
  6. use chrono::Duration;
  7. use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
  8. use errors::Error;
  9. use site::Site;
  10. lazy_static! {
  11. /// Termcolor color choice.
  12. /// We do not rely on ColorChoice::Auto behavior
  13. /// as the check is already performed by has_color.
  14. static ref COLOR_CHOICE: ColorChoice =
  15. if has_color() {
  16. ColorChoice::Always
  17. } else {
  18. ColorChoice::Never
  19. };
  20. }
  21. pub fn info(message: &str) {
  22. colorize(message, ColorSpec::new().set_bold(true));
  23. }
  24. pub fn warn(message: &str) {
  25. colorize(message, ColorSpec::new().set_bold(true).set_fg(Some(Color::Yellow)));
  26. }
  27. pub fn success(message: &str) {
  28. colorize(message, ColorSpec::new().set_bold(true).set_fg(Some(Color::Green)));
  29. }
  30. pub fn error(message: &str) {
  31. colorize(message, ColorSpec::new().set_bold(true).set_fg(Some(Color::Red)));
  32. }
  33. /// Print a colorized message to stdout
  34. fn colorize(message: &str, color: &ColorSpec) {
  35. let mut stdout = StandardStream::stdout(*COLOR_CHOICE);
  36. stdout.set_color(color).unwrap();
  37. write!(&mut stdout, "{}", message).unwrap();
  38. stdout.set_color(&ColorSpec::new()).unwrap();
  39. writeln!(&mut stdout).unwrap();
  40. }
  41. /// Display in the console the number of pages/sections in the site, and number of images to process
  42. pub fn notify_site_size(site: &Site) {
  43. let library = site.library.read().unwrap();
  44. println!(
  45. "-> Creating {} pages ({} orphan), {} sections, and processing {} images",
  46. library.pages().len(),
  47. site.get_number_orphan_pages(),
  48. library.sections().len() - 1, // -1 since we do not count the index as a section there
  49. site.num_img_ops(),
  50. );
  51. }
  52. /// Display in the console only the number of pages/sections in the site
  53. pub fn check_site_summary(site: &Site) {
  54. let library = site.library.read().unwrap();
  55. let orphans = library.get_all_orphan_pages();
  56. println!(
  57. "-> Site content: {} pages ({} orphan), {} sections",
  58. library.pages().len(),
  59. orphans.len(),
  60. library.sections().len() - 1, // -1 since we do not count the index as a section there
  61. );
  62. for orphan in orphans {
  63. warn(&format!("Orphan page found: {}", orphan.path));
  64. }
  65. }
  66. /// Display a warning in the console if there are ignored pages in the site
  67. pub fn warn_about_ignored_pages(site: &Site) {
  68. let library = site.library.read().unwrap();
  69. let ignored_pages: Vec<_> = library
  70. .sections_values()
  71. .iter()
  72. .flat_map(|s| s.ignored_pages.iter().map(|k| library.get_page_by_key(*k).file.path.clone()))
  73. .collect();
  74. if !ignored_pages.is_empty() {
  75. warn(&format!(
  76. "{} page(s) ignored (missing date or weight in a sorted section):",
  77. ignored_pages.len()
  78. ));
  79. for path in ignored_pages {
  80. warn(&format!("- {}", path.display()));
  81. }
  82. }
  83. }
  84. /// Print the time elapsed rounded to 1 decimal
  85. pub fn report_elapsed_time(instant: Instant) {
  86. let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64;
  87. if duration_ms < 1000.0 {
  88. success(&format!("Done in {}ms.\n", duration_ms));
  89. } else {
  90. let duration_sec = duration_ms / 1000.0;
  91. success(&format!("Done in {:.1}s.\n", ((duration_sec * 10.0).round() / 10.0)));
  92. }
  93. }
  94. /// Display an error message and the actual error(s)
  95. pub fn unravel_errors(message: &str, error: &Error) {
  96. if !message.is_empty() {
  97. self::error(message);
  98. }
  99. self::error(&format!("Error: {}", error));
  100. let mut cause = error.source();
  101. while let Some(e) = cause {
  102. self::error(&format!("Reason: {}", e));
  103. cause = e.source();
  104. }
  105. }
  106. /// Check whether to output colors
  107. fn has_color() -> bool {
  108. let use_colors = env::var("CLICOLOR").unwrap_or_else(|_| "1".to_string()) != "0"
  109. && env::var("NO_COLOR").is_err();
  110. let force_colors = env::var("CLICOLOR_FORCE").unwrap_or_else(|_| "0".to_string()) != "0";
  111. force_colors || use_colors && atty::is(atty::Stream::Stdout)
  112. }