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.

console.rs 3.3KB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. use std::env;
  2. use std::io::Write;
  3. use std::time::Instant;
  4. use atty;
  5. use chrono::Duration;
  6. use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
  7. use errors::Error;
  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. }
  38. /// Display in the console the number of pages/sections in the site
  39. pub fn notify_site_size(site: &Site) {
  40. println!(
  41. "-> Creating {} pages ({} orphan), {} sections, and processing {} images",
  42. site.library.pages().len(),
  43. site.get_all_orphan_pages().len(),
  44. site.library.sections().len() - 1, // -1 since we do not the index as a section
  45. site.num_img_ops(),
  46. );
  47. }
  48. /// Display a warning in the console if there are ignored pages in the site
  49. pub fn warn_about_ignored_pages(site: &Site) {
  50. let ignored_pages: Vec<_> = site.library
  51. .sections_values()
  52. .iter()
  53. .flat_map(|s| {
  54. s.ignored_pages
  55. .iter()
  56. .map(|k| site.library.get_page_by_key(*k).file.path.clone())
  57. })
  58. .collect();
  59. if !ignored_pages.is_empty() {
  60. warn(&format!(
  61. "{} page(s) ignored (missing date or weight in a sorted section):",
  62. ignored_pages.len()
  63. ));
  64. for path in ignored_pages {
  65. warn(&format!("- {}", path.display()));
  66. }
  67. }
  68. }
  69. /// Print the time elapsed rounded to 1 decimal
  70. pub fn report_elapsed_time(instant: Instant) {
  71. let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64;
  72. if duration_ms < 1000.0 {
  73. success(&format!("Done in {}ms.\n", duration_ms));
  74. } else {
  75. let duration_sec = duration_ms / 1000.0;
  76. success(&format!("Done in {:.1}s.\n", ((duration_sec * 10.0).round() / 10.0)));
  77. }
  78. }
  79. /// Display an error message and the actual error(s)
  80. pub fn unravel_errors(message: &str, error: &Error) {
  81. if !message.is_empty() {
  82. self::error(message);
  83. }
  84. self::error(&format!("Error: {}", error));
  85. for e in error.iter().skip(1) {
  86. self::error(&format!("Reason: {}", e));
  87. }
  88. }
  89. /// Check whether to output colors
  90. fn has_color() -> bool {
  91. let use_colors = env::var("CLICOLOR").unwrap_or_else(|_| "1".to_string()) != "0" && env::var("NO_COLOR").is_err();
  92. let force_colors = env::var("CLICOLOR_FORCE").unwrap_or_else(|_|"0".to_string()) != "0";
  93. force_colors || use_colors && atty::is(atty::Stream::Stdout)
  94. }