From 3f33c5b816d5949271a31fee3d9a1e7370fd1baa Mon Sep 17 00:00:00 2001 From: Rayhan Faizel Date: Tue, 6 Jun 2023 20:55:42 +0530 Subject: [PATCH 1/3] ls: Implement --sort=width --- src/uu/ls/src/ls.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/uu/ls/src/ls.rs b/src/uu/ls/src/ls.rs index e5ad4bcd459..7db591cf3b7 100644 --- a/src/uu/ls/src/ls.rs +++ b/src/uu/ls/src/ls.rs @@ -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 : name, none (-U), time (-t), size (-S) or extension (-X)") + .help("Sort by : 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 entries.sort_by(|a, b| { + a.display_name + .len() + .cmp(&b.display_name.len()) + .then(a.display_name.cmp(&b.display_name)) + }), Sort::None => {} } From 21f1a119c3378e9cc17603352c1903a72845d4b2 Mon Sep 17 00:00:00 2001 From: Rayhan Faizel Date: Tue, 6 Jun 2023 20:56:42 +0530 Subject: [PATCH 2/3] tests/ls: Implement tests for sort by width option --- tests/by-util/test_ls.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 45ced867a46..49c2272c099 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -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!()); From c26396087f9b802306e21d14fa02ce4200caa031 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Tue, 6 Jun 2023 17:44:04 +0200 Subject: [PATCH 3/3] ls: add words to spell-checker:ignore --- tests/by-util/test_ls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 49c2272c099..1266a7cab92 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -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;