help.rs 6.0 KB

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