help.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. use std::fmt;
  2. use crate::fs::feature::xattr;
  3. use crate::options::flags;
  4. use crate::options::parser::MatchedFlags;
  5. static USAGE_PART1: &str = "Usage:
  6. exa [options] [files...]
  7. META OPTIONS
  8. -?, --help show list of command-line options
  9. -v, --version show version of exa
  10. DISPLAY OPTIONS
  11. -1, --oneline display one entry per line
  12. -l, --long display extended file metadata as a table
  13. -G, --grid display entries as a grid (default)
  14. -x, --across sort the grid across, rather than downwards
  15. -R, --recurse recurse into directories
  16. -T, --tree recurse into directories as a tree
  17. -F, --classify display type indicator by file names
  18. --colo[u]r=WHEN when to use terminal colours (always, auto, never)
  19. --colo[u]r-scale highlight levels of file sizes distinctly
  20. --icons display icons
  21. --no-icons don't display icons (always overrides --icons)
  22. FILTERING AND SORTING OPTIONS
  23. -a, --all show hidden and 'dot' files
  24. -d, --list-dirs list directories as files; don't list their contents
  25. -L, --level DEPTH limit the depth of recursion
  26. -r, --reverse reverse the sort order
  27. -s, --sort SORT_FIELD which field to sort by
  28. --group-directories-first list directories before other files
  29. -D, --only-dirs list only directories
  30. -I, --ignore-glob GLOBS glob patterns (pipe-separated) of files to ignore";
  31. static USAGE_PART2: &str = " \
  32. Valid sort fields: name, Name, extension, Extension, size, type,
  33. modified, accessed, created, inode, and none.
  34. date, time, old, and new all refer to modified.
  35. LONG VIEW OPTIONS
  36. -b, --binary list file sizes with binary prefixes
  37. -B, --bytes list file sizes in bytes, without any prefixes
  38. -g, --group list each file's group
  39. -h, --header add a header row to each column
  40. -H, --links list each file's number of hard links
  41. -i, --inode list each file's inode number
  42. -m, --modified use the modified timestamp field
  43. -n, --numeric list numeric user and group IDs
  44. -S, --blocks show number of file system blocks
  45. -t, --time FIELD which timestamp field to list (modified, accessed, created)
  46. -u, --accessed use the accessed timestamp field
  47. -U, --created use the created timestamp field
  48. --changed use the changed timestamp field
  49. --time-style how to format timestamps (default, iso, long-iso, full-iso, relative)
  50. --no-permissions suppress the permissions field
  51. --octal-permissions list each file's permission in octal format
  52. --no-filesize suppress the filesize field
  53. --no-user suppress the user field
  54. --no-time suppress the time field";
  55. static GIT_FILTER_HELP: &str = " --git-ignore ignore files mentioned in '.gitignore'";
  56. static GIT_VIEW_HELP: &str = " --git list each file's Git status, if tracked or ignored";
  57. static EXTENDED_HELP: &str = " -@, --extended list each file's extended attributes and sizes";
  58. /// All the information needed to display the help text, which depends
  59. /// on which features are enabled and whether the user only wants to
  60. /// see one section’s help.
  61. #[derive(PartialEq, Eq, Debug, Copy, Clone)]
  62. pub struct HelpString;
  63. impl HelpString {
  64. /// Determines how to show help, if at all, based on the user’s
  65. /// command-line arguments. This one works backwards from the other
  66. /// ‘deduce’ functions, returning Err if help needs to be shown.
  67. ///
  68. /// We don’t do any strict-mode error checking here: it’s OK to give
  69. /// the --help or --long flags more than once. Actually checking for
  70. /// errors when the user wants help is kind of petty!
  71. pub fn deduce(matches: &MatchedFlags<'_>) -> Option<Self> {
  72. if matches.count(&flags::HELP) > 0 {
  73. Some(Self)
  74. }
  75. else {
  76. None
  77. }
  78. }
  79. }
  80. impl fmt::Display for HelpString {
  81. /// Format this help options into an actual string of help
  82. /// text to be displayed to the user.
  83. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
  84. write!(f, "{}", USAGE_PART1)?;
  85. if cfg!(feature = "git") {
  86. write!(f, "\n{}", GIT_FILTER_HELP)?;
  87. }
  88. write!(f, "\n{}", USAGE_PART2)?;
  89. if cfg!(feature = "git") {
  90. write!(f, "\n{}", GIT_VIEW_HELP)?;
  91. }
  92. if xattr::ENABLED {
  93. write!(f, "\n{}", EXTENDED_HELP)?;
  94. }
  95. writeln!(f)
  96. }
  97. }
  98. #[cfg(test)]
  99. mod test {
  100. use crate::options::{Options, OptionsResult};
  101. use std::ffi::OsStr;
  102. #[test]
  103. fn help() {
  104. let args = vec![ OsStr::new("--help") ];
  105. let opts = Options::parse(args, &None);
  106. assert!(matches!(opts, OptionsResult::Help(_)));
  107. }
  108. #[test]
  109. fn help_with_file() {
  110. let args = vec![ OsStr::new("--help"), OsStr::new("me") ];
  111. let opts = Options::parse(args, &None);
  112. assert!(matches!(opts, OptionsResult::Help(_)));
  113. }
  114. #[test]
  115. fn unhelpful() {
  116. let args = vec![];
  117. let opts = Options::parse(args, &None);
  118. assert!(! matches!(opts, OptionsResult::Help(_))) // no help when --help isn’t passed
  119. }
  120. }