dir_action.rs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //! What to do when encountering a directory?
  2. /// The action to take when trying to list a file that turns out to be a
  3. /// directory.
  4. ///
  5. /// By default, exa will display the information about files passed in as
  6. /// command-line arguments, with one file per entry. However, if a directory
  7. /// is passed in, exa assumes that the user wants to see its contents, rather
  8. /// than the directory itself.
  9. ///
  10. /// This can get annoying sometimes: if a user does `exa ~/Downloads/img-*`
  11. /// to see the details of every file starting with `img-`, any directories
  12. /// that happen to start with the same will be listed after the files at
  13. /// the end in a separate block. By listing directories as files, their
  14. /// directory status will be ignored, and both will be listed side-by-side.
  15. ///
  16. /// These two modes have recursive analogues in the “recurse” and “tree”
  17. /// modes. Here, instead of just listing the directories, exa will descend
  18. /// into them and print out their contents. The recurse mode does this by
  19. /// having extra output blocks at the end, while the tree mode will show
  20. /// directories inline, with their contents immediately underneath.
  21. #[derive(PartialEq, Debug, Copy, Clone)]
  22. pub enum DirAction {
  23. /// This directory should be listed along with the regular files, instead
  24. /// of having its contents queried.
  25. AsFile,
  26. /// This directory should not be listed, and should instead be opened and
  27. /// *its* files listed separately. This is the default behaviour.
  28. List,
  29. /// This directory should be listed along with the regular files, and then
  30. /// its contents should be listed afterward. The recursive contents of
  31. /// *those* contents are dictated by the options argument.
  32. Recurse(RecurseOptions),
  33. }
  34. impl DirAction {
  35. /// Gets the recurse options, if this dir action has any.
  36. pub fn recurse_options(self) -> Option<RecurseOptions> {
  37. match self {
  38. Self::Recurse(o) => Some(o),
  39. _ => None,
  40. }
  41. }
  42. /// Whether to treat directories as regular files or not.
  43. pub fn treat_dirs_as_files(self) -> bool {
  44. match self {
  45. Self::AsFile => true,
  46. Self::Recurse(o) => o.tree,
  47. Self::List => false,
  48. }
  49. }
  50. }
  51. /// The options that determine how to recurse into a directory.
  52. #[derive(PartialEq, Debug, Copy, Clone)]
  53. pub struct RecurseOptions {
  54. /// Whether recursion should be done as a tree or as multiple individual
  55. /// views of files.
  56. pub tree: bool,
  57. /// The maximum number of times that recursion should descend to, if one
  58. /// is specified.
  59. pub max_depth: Option<usize>,
  60. }
  61. impl RecurseOptions {
  62. /// Returns whether a directory of the given depth would be too deep.
  63. pub fn is_too_deep(self, depth: usize) -> bool {
  64. match self.max_depth {
  65. None => false,
  66. Some(d) => d <= depth
  67. }
  68. }
  69. }