filter.rs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //! Parsing the options for `FileFilter`.
  2. use crate::fs::filter::{FileFilter, FileFilterFlags, GitIgnore, IgnorePatterns, SortCase, SortField};
  3. use crate::fs::DotFilter;
  4. use crate::options::parser::MatchedFlags;
  5. use crate::options::{flags, OptionsError};
  6. impl FileFilter {
  7. /// Determines which of all the file filter options to use.
  8. pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  9. use FileFilterFlags as FFF;
  10. let mut filter_flags:Vec<FileFilterFlags> = vec![];
  11. for (has,flag) in &[
  12. (matches.has(&flags::REVERSE)?, FFF::Reverse),
  13. (matches.has(&flags::ONLY_DIRS)?, FFF::OnlyDirs),
  14. (matches.has(&flags::ONLY_FILES)?, FFF::OnlyFiles),
  15. ] {
  16. if *has {
  17. filter_flags.push(flag.clone());
  18. }
  19. }
  20. #[rustfmt::skip]
  21. return Ok(Self {
  22. list_dirs_first: matches.has(&flags::DIRS_FIRST)?,
  23. flags: filter_flags,
  24. sort_field: SortField::deduce(matches)?,
  25. dot_filter: DotFilter::deduce(matches)?,
  26. ignore_patterns: IgnorePatterns::deduce(matches)?,
  27. git_ignore: GitIgnore::deduce(matches)?,
  28. });
  29. }
  30. }
  31. impl SortField {
  32. /// Determines which sort field to use based on the `--sort` argument.
  33. /// This argument’s value can be one of several flags, listed above.
  34. /// Returns the default sort field if none is given, or `Err` if the
  35. /// value doesn’t correspond to a sort field we know about.
  36. fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  37. let Some(word) = matches.get(&flags::SORT)? else { return Ok(Self::default()) };
  38. // Get String because we can’t match an OsStr
  39. let Some(word) = word.to_str() else { return Err(OptionsError::BadArgument(&flags::SORT, word.into())) };
  40. let field = match word {
  41. "name" | "filename" => Self::Name(SortCase::AaBbCc),
  42. "Name" | "Filename" => Self::Name(SortCase::ABCabc),
  43. ".name" | ".filename" => Self::NameMixHidden(SortCase::AaBbCc),
  44. ".Name" | ".Filename" => Self::NameMixHidden(SortCase::ABCabc),
  45. "size" | "filesize" => Self::Size,
  46. "ext" | "extension" => Self::Extension(SortCase::AaBbCc),
  47. "Ext" | "Extension" => Self::Extension(SortCase::ABCabc),
  48. // “new” sorts oldest at the top and newest at the bottom; “old”
  49. // sorts newest at the top and oldest at the bottom. I think this
  50. // is the right way round to do this: “size” puts the smallest at
  51. // the top and the largest at the bottom, doesn’t it?
  52. "date" | "time" | "mod" | "modified" | "new" | "newest" => Self::ModifiedDate,
  53. // Similarly, “age” means that files with the least age (the
  54. // newest files) get sorted at the top, and files with the most
  55. // age (the oldest) at the bottom.
  56. "age" | "old" | "oldest" => Self::ModifiedAge,
  57. "ch" | "changed" => Self::ChangedDate,
  58. "acc" | "accessed" => Self::AccessedDate,
  59. "cr" | "created" => Self::CreatedDate,
  60. #[cfg(unix)]
  61. "inode" => Self::FileInode,
  62. "type" => Self::FileType,
  63. "none" => Self::Unsorted,
  64. _ => {
  65. return Err(OptionsError::BadArgument(&flags::SORT, word.into()));
  66. }
  67. };
  68. Ok(field)
  69. }
  70. }
  71. // I’ve gone back and forth between whether to sort case-sensitively or
  72. // insensitively by default. The default string sort in most programming
  73. // languages takes each character’s ASCII value into account, sorting
  74. // “Documents” before “apps”, but there’s usually an option to ignore
  75. // characters’ case, putting “apps” before “Documents”.
  76. //
  77. // The argument for following case is that it’s easy to forget whether an item
  78. // begins with an uppercase or lowercase letter and end up having to scan both
  79. // the uppercase and lowercase sub-lists to find the item you want. If you
  80. // happen to pick the sublist it’s not in, it looks like it’s missing, which
  81. // is worse than if you just take longer to find it.
  82. // (https://ux.stackexchange.com/a/79266)
  83. //
  84. // The argument for ignoring case is that it makes exa sort files differently
  85. // from shells. A user would expect a directory’s files to be in the same
  86. // order if they used “exa ~/directory” or “exa ~/directory/*”, but exa sorts
  87. // them in the first case, and the shell in the second case, so they wouldn’t
  88. // be exactly the same if exa does something non-conventional.
  89. //
  90. // However, exa already sorts files differently: it uses natural sorting from
  91. // the natord crate, sorting the string “2” before “10” because the number’s
  92. // smaller, because that’s usually what the user expects to happen. Users will
  93. // name their files with numbers expecting them to be treated like numbers,
  94. // rather than lists of numeric characters.
  95. //
  96. // In the same way, users will name their files with letters expecting the
  97. // order of the letters to matter, rather than each letter’s character’s ASCII
  98. // value. So exa breaks from tradition and ignores case while sorting:
  99. // “apps” first, then “Documents”.
  100. //
  101. // You can get the old behaviour back by sorting with `--sort=Name`.
  102. impl Default for SortField {
  103. fn default() -> Self {
  104. Self::Name(SortCase::AaBbCc)
  105. }
  106. }
  107. impl DotFilter {
  108. /// Determines the dot filter based on how many `--all` options were
  109. /// given: one will show dotfiles, but two will show `.` and `..` too.
  110. /// --almost-all is equivalent to --all, included for compatibility with
  111. /// `ls -A`.
  112. ///
  113. /// It also checks for the `--tree` option, because of a special case
  114. /// where `--tree --all --all` won’t work: listing the parent directory
  115. /// in tree mode would loop onto itself!
  116. ///
  117. /// `--almost-all` binds stronger than multiple `--all` as we currently do not take the order
  118. /// of arguments into account and it is the safer option (does not clash with `--tree`)
  119. pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  120. let all_count = matches.count(&flags::ALL);
  121. let has_almost_all = matches.has(&flags::ALMOST_ALL)?;
  122. match (all_count, has_almost_all) {
  123. (0, false) => Ok(Self::JustFiles),
  124. // either a single --all or at least one --almost-all is given
  125. (1, _) | (0, true) => Ok(Self::Dotfiles),
  126. // more than one --all
  127. (c, _) => {
  128. if matches.count(&flags::TREE) > 0 {
  129. Err(OptionsError::TreeAllAll)
  130. } else if matches.is_strict() && c > 2 {
  131. Err(OptionsError::Conflict(&flags::ALL, &flags::ALL))
  132. } else {
  133. Ok(Self::DotfilesAndDots)
  134. }
  135. }
  136. }
  137. }
  138. }
  139. impl IgnorePatterns {
  140. /// Determines the set of glob patterns to use based on the
  141. /// `--ignore-glob` argument’s value. This is a list of strings
  142. /// separated by pipe (`|`) characters, given in any order.
  143. pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  144. // If there are no inputs, we return a set of patterns that doesn’t
  145. // match anything, rather than, say, `None`.
  146. let Some(inputs) = matches.get(&flags::IGNORE_GLOB)? else { return Ok(Self::empty()) };
  147. // Awkwardly, though, a glob pattern can be invalid, and we need to
  148. // deal with invalid patterns somehow.
  149. let (patterns, mut errors) = Self::parse_from_iter(inputs.to_string_lossy().split('|'));
  150. // It can actually return more than one glob error,
  151. // but we only use one. (TODO)
  152. match errors.pop() {
  153. Some(e) => Err(e.into()),
  154. None => Ok(patterns),
  155. }
  156. }
  157. }
  158. impl GitIgnore {
  159. pub fn deduce(matches: &MatchedFlags<'_>) -> Result<Self, OptionsError> {
  160. if matches.has(&flags::GIT_IGNORE)? {
  161. Ok(Self::CheckAndIgnore)
  162. } else {
  163. Ok(Self::Off)
  164. }
  165. }
  166. }
  167. #[cfg(test)]
  168. mod test {
  169. use super::*;
  170. use crate::options::flags;
  171. use crate::options::parser::Flag;
  172. use std::ffi::OsString;
  173. macro_rules! test {
  174. ($name:ident: $type:ident <- $inputs:expr; $stricts:expr => $result:expr) => {
  175. #[test]
  176. fn $name() {
  177. use crate::options::parser::Arg;
  178. use crate::options::test::parse_for_test;
  179. use crate::options::test::Strictnesses::*;
  180. static TEST_ARGS: &[&Arg] = &[
  181. &flags::SORT,
  182. &flags::ALL,
  183. &flags::ALMOST_ALL,
  184. &flags::TREE,
  185. &flags::IGNORE_GLOB,
  186. &flags::GIT_IGNORE,
  187. ];
  188. for result in parse_for_test($inputs.as_ref(), TEST_ARGS, $stricts, |mf| {
  189. $type::deduce(mf)
  190. }) {
  191. assert_eq!(result, $result);
  192. }
  193. }
  194. };
  195. }
  196. mod sort_fields {
  197. use super::*;
  198. // Default behaviour
  199. test!(empty: SortField <- []; Both => Ok(SortField::default()));
  200. // Sort field arguments
  201. test!(one_arg: SortField <- ["--sort=mod"]; Both => Ok(SortField::ModifiedDate));
  202. test!(one_long: SortField <- ["--sort=size"]; Both => Ok(SortField::Size));
  203. test!(one_short: SortField <- ["-saccessed"]; Both => Ok(SortField::AccessedDate));
  204. test!(lowercase: SortField <- ["--sort", "name"]; Both => Ok(SortField::Name(SortCase::AaBbCc)));
  205. test!(uppercase: SortField <- ["--sort", "Name"]; Both => Ok(SortField::Name(SortCase::ABCabc)));
  206. test!(old: SortField <- ["--sort", "new"]; Both => Ok(SortField::ModifiedDate));
  207. test!(oldest: SortField <- ["--sort=newest"]; Both => Ok(SortField::ModifiedDate));
  208. test!(new: SortField <- ["--sort", "old"]; Both => Ok(SortField::ModifiedAge));
  209. test!(newest: SortField <- ["--sort=oldest"]; Both => Ok(SortField::ModifiedAge));
  210. test!(age: SortField <- ["-sage"]; Both => Ok(SortField::ModifiedAge));
  211. test!(mix_hidden_lowercase: SortField <- ["--sort", ".name"]; Both => Ok(SortField::NameMixHidden(SortCase::AaBbCc)));
  212. test!(mix_hidden_uppercase: SortField <- ["--sort", ".Name"]; Both => Ok(SortField::NameMixHidden(SortCase::ABCabc)));
  213. // Errors
  214. test!(error: SortField <- ["--sort=colour"]; Both => Err(OptionsError::BadArgument(&flags::SORT, OsString::from("colour"))));
  215. // Overriding
  216. test!(overridden: SortField <- ["--sort=cr", "--sort", "mod"]; Last => Ok(SortField::ModifiedDate));
  217. test!(overridden_2: SortField <- ["--sort", "none", "--sort=Extension"]; Last => Ok(SortField::Extension(SortCase::ABCabc)));
  218. test!(overridden_3: SortField <- ["--sort=cr", "--sort", "mod"]; Complain => Err(OptionsError::Duplicate(Flag::Long("sort"), Flag::Long("sort"))));
  219. test!(overridden_4: SortField <- ["--sort", "none", "--sort=Extension"]; Complain => Err(OptionsError::Duplicate(Flag::Long("sort"), Flag::Long("sort"))));
  220. }
  221. mod dot_filters {
  222. use super::*;
  223. // Default behaviour
  224. test!(empty: DotFilter <- []; Both => Ok(DotFilter::JustFiles));
  225. // --all
  226. test!(all: DotFilter <- ["--all"]; Both => Ok(DotFilter::Dotfiles));
  227. test!(all_all: DotFilter <- ["--all", "-a"]; Both => Ok(DotFilter::DotfilesAndDots));
  228. test!(all_all_2: DotFilter <- ["-aa"]; Both => Ok(DotFilter::DotfilesAndDots));
  229. test!(all_all_3: DotFilter <- ["-aaa"]; Last => Ok(DotFilter::DotfilesAndDots));
  230. test!(all_all_4: DotFilter <- ["-aaa"]; Complain => Err(OptionsError::Conflict(&flags::ALL, &flags::ALL)));
  231. // --all and --tree
  232. test!(tree_a: DotFilter <- ["-Ta"]; Both => Ok(DotFilter::Dotfiles));
  233. test!(tree_aa: DotFilter <- ["-Taa"]; Both => Err(OptionsError::TreeAllAll));
  234. test!(tree_aaa: DotFilter <- ["-Taaa"]; Both => Err(OptionsError::TreeAllAll));
  235. // --almost-all
  236. test!(almost_all: DotFilter <- ["--almost-all"]; Both => Ok(DotFilter::Dotfiles));
  237. test!(almost_all_all: DotFilter <- ["-Aa"]; Both => Ok(DotFilter::Dotfiles));
  238. test!(almost_all_all_2: DotFilter <- ["-Aaa"]; Both => Ok(DotFilter::DotfilesAndDots));
  239. }
  240. mod ignore_patterns {
  241. use super::*;
  242. use std::iter::FromIterator;
  243. fn pat(string: &'static str) -> glob::Pattern {
  244. glob::Pattern::new(string).unwrap()
  245. }
  246. // Various numbers of globs
  247. test!(none: IgnorePatterns <- []; Both => Ok(IgnorePatterns::empty()));
  248. test!(one: IgnorePatterns <- ["--ignore-glob", "*.ogg"]; Both => Ok(IgnorePatterns::from_iter(vec![ pat("*.ogg") ])));
  249. test!(two: IgnorePatterns <- ["--ignore-glob=*.ogg|*.MP3"]; Both => Ok(IgnorePatterns::from_iter(vec![ pat("*.ogg"), pat("*.MP3") ])));
  250. test!(loads: IgnorePatterns <- ["-I*|?|.|*"]; Both => Ok(IgnorePatterns::from_iter(vec![ pat("*"), pat("?"), pat("."), pat("*") ])));
  251. // Overriding
  252. test!(overridden: IgnorePatterns <- ["-I=*.ogg", "-I", "*.mp3"]; Last => Ok(IgnorePatterns::from_iter(vec![ pat("*.mp3") ])));
  253. test!(overridden_2: IgnorePatterns <- ["-I", "*.OGG", "-I*.MP3"]; Last => Ok(IgnorePatterns::from_iter(vec![ pat("*.MP3") ])));
  254. test!(overridden_3: IgnorePatterns <- ["-I=*.ogg", "-I", "*.mp3"]; Complain => Err(OptionsError::Duplicate(Flag::Short(b'I'), Flag::Short(b'I'))));
  255. test!(overridden_4: IgnorePatterns <- ["-I", "*.OGG", "-I*.MP3"]; Complain => Err(OptionsError::Duplicate(Flag::Short(b'I'), Flag::Short(b'I'))));
  256. }
  257. mod git_ignores {
  258. use super::*;
  259. test!(off: GitIgnore <- []; Both => Ok(GitIgnore::Off));
  260. test!(on: GitIgnore <- ["--git-ignore"]; Both => Ok(GitIgnore::CheckAndIgnore));
  261. }
  262. }