help.rs 6.2 KB

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