filter.rs 14 KB

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