help.rs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. eza [options] [files...]
  7. META OPTIONS
  8. --help show list of command-line options
  9. -v, --version show version of eza
  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. -X, --dereference dereference symbolic links when displaying information
  18. -F, --classify=WHEN display type indicator by file names (always, auto, never)
  19. --colo[u]r=WHEN when to use terminal colours (always, auto, never)
  20. --colo[u]r-scale highlight levels of 'field' distinctly(all, age, size)
  21. --colo[u]r-scale-mode use gradient or fixed colors in --color-scale (fixed, gradient)
  22. --icons=WHEN when to display icons (always, auto, never)
  23. --no-quotes don't quote file names with spaces
  24. --hyperlink display entries as hyperlinks
  25. --absolute display entries with their absolute path (on, follow, off)
  26. -w, --width COLS set screen width in columns
  27. FILTERING AND SORTING OPTIONS
  28. -a, --all show hidden and 'dot' files. Use this twice to also
  29. show the '.' and '..' directories
  30. -A, --almost-all equivalent to --all; included for compatibility with `ls -A`
  31. -d, --list-dirs list directories as files; don't list their contents
  32. -L, --level DEPTH limit the depth of recursion
  33. -r, --reverse reverse the sort order
  34. -s, --sort SORT_FIELD which field to sort by
  35. --group-directories-first list directories before other files
  36. -D, --only-dirs list only directories
  37. -f, --only-files list only files
  38. -I, --ignore-glob GLOBS glob patterns (pipe-separated) of files to ignore";
  39. static GIT_FILTER_HELP: &str = " \
  40. --git-ignore ignore files mentioned in '.gitignore'";
  41. static USAGE_PART2: &str = " \
  42. Valid sort fields: name, Name, extension, Extension, size, type,
  43. modified, accessed, created, inode, and none.
  44. date, time, old, and new all refer to modified.
  45. LONG VIEW OPTIONS
  46. -b, --binary list file sizes with binary prefixes
  47. -B, --bytes list file sizes in bytes, without any prefixes
  48. -g, --group list each file's group
  49. --smart-group only show group if it has a different name from owner
  50. -h, --header add a header row to each column
  51. -H, --links list each file's number of hard links
  52. -i, --inode list each file's inode number
  53. -m, --modified use the modified timestamp field
  54. -M, --mounts show mount details (Linux and Mac only)
  55. -n, --numeric list numeric user and group IDs
  56. -O, --flags list file flags (Mac, BSD, and Windows only)
  57. -S, --blocksize show size of allocated file system blocks
  58. -t, --time FIELD which timestamp field to list (modified, accessed, created)
  59. -u, --accessed use the accessed timestamp field
  60. -U, --created use the created timestamp field
  61. --changed use the changed timestamp field
  62. --time-style how to format timestamps (default, iso, long-iso,
  63. full-iso, relative, or a custom style '+<FORMAT>'
  64. like '+%Y-%m-%d %H:%M')
  65. --total-size show the size of a directory as the size of all
  66. files and directories inside (unix only)
  67. --no-permissions suppress the permissions field
  68. -o, --octal-permissions list each file's permission in octal format
  69. --no-filesize suppress the filesize field
  70. --no-user suppress the user field
  71. --no-time suppress the time field
  72. --stdin read file names from stdin, one per line or other separator
  73. specified in environment";
  74. static GIT_VIEW_HELP: &str = " \
  75. --git list each file's Git status, if tracked or ignored
  76. --no-git suppress Git status (always overrides --git,
  77. --git-repos, --git-repos-no-status)
  78. --git-repos list root of git-tree status";
  79. static EXTENDED_HELP: &str = " \
  80. -@, --extended list each file's extended attributes and sizes";
  81. static SECATTR_HELP: &str = " \
  82. -Z, --context list each file's security context";
  83. /// All the information needed to display the help text, which depends
  84. /// on which features are enabled and whether the user only wants to
  85. /// see one section’s help.
  86. #[derive(PartialEq, Eq, Debug, Copy, Clone)]
  87. pub struct HelpString;
  88. impl HelpString {
  89. /// Determines how to show help, if at all, based on the user’s
  90. /// command-line arguments. This one works backwards from the other
  91. /// ‘deduce’ functions, returning Err if help needs to be shown.
  92. ///
  93. /// We don’t do any strict-mode error checking here: it’s OK to give
  94. /// the --help or --long flags more than once. Actually checking for
  95. /// errors when the user wants help is kind of petty!
  96. pub fn deduce(matches: &MatchedFlags<'_>) -> Option<Self> {
  97. if matches.count(&flags::HELP) > 0 {
  98. Some(Self)
  99. } else {
  100. None
  101. }
  102. }
  103. }
  104. impl fmt::Display for HelpString {
  105. /// Format this help options into an actual string of help
  106. /// text to be displayed to the user.
  107. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
  108. write!(f, "{USAGE_PART1}")?;
  109. if cfg!(feature = "git") {
  110. write!(f, "\n{GIT_FILTER_HELP}")?;
  111. }
  112. write!(f, "\n{USAGE_PART2}")?;
  113. if cfg!(feature = "git") {
  114. write!(f, "\n{GIT_VIEW_HELP}")?;
  115. }
  116. if xattr::ENABLED {
  117. write!(f, "\n{EXTENDED_HELP}")?;
  118. write!(f, "\n{SECATTR_HELP}")?;
  119. }
  120. writeln!(f)
  121. }
  122. }
  123. #[cfg(test)]
  124. mod test {
  125. use crate::options::{Options, OptionsResult};
  126. use std::ffi::OsStr;
  127. #[test]
  128. fn help() {
  129. let args = vec![OsStr::new("--help")];
  130. let opts = Options::parse(args, &None);
  131. assert!(matches!(opts, OptionsResult::Help(_)));
  132. }
  133. #[test]
  134. fn help_with_file() {
  135. let args = vec![OsStr::new("--help"), OsStr::new("me")];
  136. let opts = Options::parse(args, &None);
  137. assert!(matches!(opts, OptionsResult::Help(_)));
  138. }
  139. #[test]
  140. fn unhelpful() {
  141. let args = vec![];
  142. let opts = Options::parse(args, &None);
  143. assert!(!matches!(opts, OptionsResult::Help(_))); // no help when --help isn’t passed
  144. }
  145. }