Skip to content

Commit a156d96

Browse files
committed
Add new sort option .name and .Name
Add two new sort options `.name` and `.Name` which with ignore a leading `.` if present on the file name before sorting according to `name` and `Name`. This new sort is convenient if you want to list hidden and unhidden files sorted together.
1 parent 0eb7966 commit a156d96

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/fs/filter.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ pub enum SortField {
191191
/// bad, even though that’s kind of nonsensical. So it’s its own variant
192192
/// that can be reversed like usual.
193193
ModifiedAge,
194+
195+
/// The file's name, however if the name of the file begins with `.`
196+
/// ignore the leading `.` and then sort as Name
197+
NameMixHidden(SortCase),
194198
}
195199

196200
/// Whether a field should be sorted case-sensitively or case-insensitively.
@@ -253,6 +257,23 @@ impl SortField {
253257
Ordering::Equal => natord::compare_ignore_case(&*a.name, &*b.name),
254258
order => order,
255259
},
260+
261+
SortField::NameMixHidden(ABCabc) => natord::compare(
262+
SortField::strip_dot(&a.name),
263+
SortField::strip_dot(&b.name)
264+
),
265+
SortField::NameMixHidden(AaBbCc) => natord::compare_ignore_case(
266+
SortField::strip_dot(&a.name),
267+
SortField::strip_dot(&b.name)
268+
)
269+
}
270+
}
271+
272+
fn strip_dot(n: &str) -> &str {
273+
if n.starts_with(".") {
274+
&n[1..]
275+
} else {
276+
n
256277
}
257278
}
258279
}

src/options/filter.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ impl SortField {
4141
else if word == "Name" || word == "Filename" {
4242
Ok(SortField::Name(SortCase::ABCabc))
4343
}
44+
else if word == ".name" || word == ".filename" {
45+
Ok(SortField::NameMixHidden(SortCase::AaBbCc))
46+
}
47+
else if word == ".Name" || word == ".Filename" {
48+
Ok(SortField::NameMixHidden(SortCase::ABCabc))
49+
}
4450
else if word == "size" || word == "filesize" {
4551
Ok(SortField::Size)
4652
}
@@ -231,6 +237,9 @@ mod test {
231237
test!(newest: SortField <- ["--sort=oldest"]; Both => Ok(SortField::ModifiedAge));
232238
test!(age: SortField <- ["-sage"]; Both => Ok(SortField::ModifiedAge));
233239

240+
test!(mix_hidden_lowercase: SortField <- ["--sort", ".name"]; Both => Ok(SortField::NameMixHidden(SortCase::AaBbCc)));
241+
test!(mix_hidden_uppercase: SortField <- ["--sort", ".Name"]; Both => Ok(SortField::NameMixHidden(SortCase::ABCabc)));
242+
234243
// Errors
235244
test!(error: SortField <- ["--sort=colour"]; Both => Err(Misfire::BadArgument(&flags::SORT, OsString::from("colour"))));
236245

0 commit comments

Comments
 (0)