Add support for extension sort.

This commit is contained in:
Allen Hsu 2020-09-16 09:58:08 +10:00 committed by Abin Simon
parent c2cbd575dc
commit b6378b0ca1
4 changed files with 55 additions and 0 deletions

View file

@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] - ReleaseDate
### Added
- Add support for `--extensionsort` `-X` from [aldhsu](https://github.com/aldhsu)
## [0.18.0] - 2020-08-29
### Added

View file

@ -154,6 +154,13 @@ pub fn build() -> App<'static, 'static> {
.multiple(true)
.help("Sort by size"),
)
.arg(
Arg::with_name("extensionsort")
.short("X")
.long("extensionsort")
.multiple(true)
.help("Sort by file extension"),
)
.arg(
Arg::with_name("reverse")
.short("r")

View file

@ -58,6 +58,8 @@ impl Flags {
SortFlag::Time
} else if matches.is_present("sizesort") {
SortFlag::Size
} else if matches.is_present("extensionsort") {
SortFlag::Extension
} else {
SortFlag::Name
};
@ -306,6 +308,7 @@ pub enum SortFlag {
Name,
Time,
Size,
Extension,
}
#[derive(Clone, Debug, Copy, PartialEq, Eq)]

View file

@ -19,6 +19,7 @@ pub fn assemble_sorters(flags: &Flags) -> Vec<(SortOrder, SortFn)> {
SortFlag::Name => by_name,
SortFlag::Size => by_size,
SortFlag::Time => by_date,
SortFlag::Extension => by_extension,
};
sorters.push((flags.sort_order, other_sort));
sorters
@ -55,6 +56,10 @@ fn by_date(a: &Meta, b: &Meta) -> Ordering {
b.date.cmp(&a.date).then(a.name.cmp(&b.name))
}
fn by_extension(a: &Meta, b: &Meta) -> Ordering {
a.name.extension().cmp(&b.name.extension())
}
#[cfg(test)]
mod tests {
use super::*;
@ -220,4 +225,42 @@ mod tests {
let sorter = assemble_sorters(&flags);
assert_eq!(by_meta(&sorter, &meta_a, &meta_z), Ordering::Greater);
}
#[test]
fn test_sort_assemble_sorters_by_extension() {
let tmp_dir = tempdir().expect("failed to create temp dir");
// Create the file with rs extension;
let path_a = tmp_dir.path().join("aaa.rs");
File::create(&path_a).expect("failed to create file");
let meta_a = Meta::from_path(&path_a, false).expect("failed to get meta");
// Create the file with rs extension;
let path_z = tmp_dir.path().join("zzz.rs");
File::create(&path_z).expect("failed to create file");
let meta_z = Meta::from_path(&path_z, false).expect("failed to get meta");
// Create the file with js extension;
let path_j = tmp_dir.path().join("zzz.js");
File::create(&path_j).expect("failed to create file");
let meta_j = Meta::from_path(&path_j, false).expect("failed to get meta");
// Create the file with txt extension;
let path_t = tmp_dir.path().join("zzz.txt");
File::create(&path_t).expect("failed to create file");
let meta_t = Meta::from_path(&path_t, false).expect("failed to get meta");
let mut flags = Flags::default();
flags.sort_by = SortFlag::Extension;
// Sort by extension
let sorter = assemble_sorters(&flags);
assert_eq!(by_meta(&sorter, &meta_a, &meta_z), Ordering::Equal);
let sorter = assemble_sorters(&flags);
assert_eq!(by_meta(&sorter, &meta_a, &meta_j), Ordering::Greater);
let sorter = assemble_sorters(&flags);
assert_eq!(by_meta(&sorter, &meta_a, &meta_t), Ordering::Less);
}
}