help.rs 5.6 KB

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