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.

115 lines
3.5KB

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