Merge pull request #4954 from Skryptonyte/ls_sortwidth

ls: Implement new sort option --sort=width
This commit is contained in:
Daniel Hofstetter 2023-06-06 18:46:54 +02:00 committed by GitHub
commit 0128198a99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 3 deletions

View file

@ -294,6 +294,7 @@ enum Sort {
Time,
Version,
Extension,
Width,
}
#[derive(PartialEq)]
@ -496,6 +497,7 @@ fn extract_sort(options: &clap::ArgMatches) -> Sort {
"size" => Sort::Size,
"version" => Sort::Version,
"extension" => Sort::Extension,
"width" => Sort::Width,
// below should never happen as clap already restricts the values.
_ => unreachable!("Invalid field for --sort"),
}
@ -1322,9 +1324,9 @@ pub fn uu_app() -> Command {
.arg(
Arg::new(options::SORT)
.long(options::SORT)
.help("Sort by <field>: name, none (-U), time (-t), size (-S) or extension (-X)")
.help("Sort by <field>: name, none (-U), time (-t), size (-S), extension (-X) or width")
.value_name("field")
.value_parser(["name", "none", "time", "size", "version", "extension"])
.value_parser(["name", "none", "time", "size", "version", "extension", "width"])
.require_equals(true)
.overrides_with_all([
options::SORT,
@ -1937,6 +1939,12 @@ fn sort_entries(entries: &mut [PathData], config: &Config, out: &mut BufWriter<S
.cmp(&b.p_buf.extension())
.then(a.p_buf.file_stem().cmp(&b.p_buf.file_stem()))
}),
Sort::Width => entries.sort_by(|a, b| {
a.display_name
.len()
.cmp(&b.display_name.len())
.then(a.display_name.cmp(&b.display_name))
}),
Sort::None => {}
}

View file

@ -1,4 +1,4 @@
// spell-checker:ignore (words) READMECAREFULLY birthtime doesntexist oneline somebackup lrwx somefile somegroup somehiddenbackup somehiddenfile tabsize aaaaaaaa bbbb cccc dddddddd ncccc
// spell-checker:ignore (words) READMECAREFULLY birthtime doesntexist oneline somebackup lrwx somefile somegroup somehiddenbackup somehiddenfile tabsize aaaaaaaa bbbb cccc dddddddd ncccc neee naaaaa nbcdef nfffff
#[cfg(any(unix, feature = "feat_selinux"))]
use crate::common::util::expected_result;
@ -1564,6 +1564,28 @@ fn test_ls_sort_name() {
.stdout_is(".a\n.b\na\nb\n");
}
#[test]
fn test_ls_sort_width() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("aaaaa");
at.touch("bbb");
at.touch("cccc");
at.touch("eee");
at.touch("d");
at.touch("fffff");
at.touch("abc");
at.touch("zz");
at.touch("bcdef");
scene
.ucmd()
.arg("--sort=width")
.succeeds()
.stdout_is("d\nzz\nabc\nbbb\neee\ncccc\naaaaa\nbcdef\nfffff\n");
}
#[test]
fn test_ls_order_size() {
let scene = TestScenario::new(util_name!());