Skip to content

Commit

Permalink
Merge pull request #4954 from Skryptonyte/ls_sortwidth
Browse files Browse the repository at this point in the history
ls: Implement new sort option --sort=width
  • Loading branch information
cakebaker authored Jun 6, 2023
2 parents 8a543d3 + c263960 commit 0128198
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ enum Sort {
Time,
Version,
Extension,
Width,
}

#[derive(PartialEq)]
Expand Down Expand Up @@ -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"),
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 => {}
}

Expand Down
24 changes: 23 additions & 1 deletion tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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!());
Expand Down

0 comments on commit 0128198

Please sign in to comment.