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

ls: Implement --sort=width

This commit is contained in:
Rayhan Faizel 2023-06-06 20:55:42 +05:30
parent 82f5fec688
commit 3f33c5b816

View file

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