1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

ls: ignore leading period when sorting by name (#2112)

* ls: ignore leading period when sorting by name

ls now behaves like GNU ls with respect to sorting files by ignoring
leading periods when sorting by main.

Added tests to ensure "touch a .a b .b ; ls" returns ".a  a  .b  b"

* Replaced clone/collect calls.
This commit is contained in:
Ricardo Iglesias 2021-04-25 12:08:05 -07:00 committed by GitHub
parent c7f9677b77
commit c3d7358df6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View file

@ -1139,7 +1139,13 @@ fn sort_entries(entries: &mut Vec<PathData>, config: &Config) {
entries.sort_by_key(|k| Reverse(k.md().as_ref().map(|md| md.len()).unwrap_or(0)))
}
// The default sort in GNU ls is case insensitive
Sort::Name => entries.sort_by_cached_key(|k| k.file_name.to_lowercase()),
Sort::Name => entries.sort_by_cached_key(|k| {
let has_dot: bool = k.file_name.starts_with('.');
let filename_nodot: &str = &k.file_name[if has_dot { 1 } else { 0 }..];
// We want hidden files to appear before regular files of the same
// name, so we need to negate the "has_dot" variable.
(filename_nodot.to_lowercase(), !has_dot)
}),
Sort::Version => entries.sort_by(|k, j| version_cmp::version_cmp(&k.p_buf, &j.p_buf)),
Sort::None => {}
}

View file

@ -481,6 +481,21 @@ fn test_ls_sort_name() {
.arg("--sort=name")
.succeeds()
.stdout_is(["test-1", "test-2", "test-3\n"].join(sep));
// Order of a named sort ignores leading dots.
let scene_dot = TestScenario::new(util_name!());
let at = &scene_dot.fixtures;
at.touch(".a");
at.touch("a");
at.touch(".b");
at.touch("b");
scene_dot
.ucmd()
.arg("--sort=name")
.arg("-A")
.succeeds()
.stdout_is([".a", "a", ".b", "b\n"].join(sep));
}
#[test]