Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/uu/ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ uucore = { workspace = true, features = [
"fs",
"fsext",
"fsxattr",
"i18n-collator",
"parser-size",
"parser-glob",
"quoting-style",
Expand Down
15 changes: 14 additions & 1 deletion src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ impl UError for LsError {
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uucore::clap_localization::handle_clap_result_with_exit_code(uu_app(), args, 2)?;

uucore::i18n::collator::init_locale_collation();

let config = Config::from(&matches)?;

let locs = matches
Expand Down Expand Up @@ -1157,7 +1159,18 @@ fn sort_entries(entries: &mut [PathData], config: &Config) {
entries.sort_unstable_by_key(|k| Reverse(k.metadata().map_or(0, Metadata::len)));
}
// The default sort in GNU ls is case insensitive
Sort::Name => entries.sort_unstable_by(|a, b| a.display_name().cmp(b.display_name())),
Sort::Name => {
if uucore::i18n::collator::should_use_locale_collation() {
entries.sort_unstable_by(|a, b| {
uucore::i18n::collator::locale_cmp(
os_str_as_bytes_lossy(a.display_name()).as_ref(),
os_str_as_bytes_lossy(b.display_name()).as_ref(),
)
});
} else {
entries.sort_unstable_by(|a, b| a.display_name().cmp(b.display_name()));
}
}
Sort::Version => entries.sort_unstable_by(|a, b| {
version_cmp(
os_str_as_bytes_lossy(a.path().as_os_str()).as_ref(),
Expand Down
20 changes: 20 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,26 @@ fn test_ls_sort_name() {
.stdout_is(".a\n.b\na\nb\n");
}

// https://github.com/uutils/coreutils/issues/11831
// In a UTF-8 locale, GNU ls places "." and ".." before names starting with
// punctuation such as '#' due to locale-aware collation.
#[test]
fn test_ls_sort_dot_first_utf8_locale() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("#asdf");
at.touch("bar");
at.touch("foo");

scene
.ucmd()
.env("LANG", "en_US.UTF-8")
.env("LC_ALL", "en_US.UTF-8")
.arg("-1a")
.succeeds()
.stdout_is(".\n..\n#asdf\nbar\nfoo\n");
}

#[test]
fn test_ls_sort_width() {
let scene = TestScenario::new(util_name!());
Expand Down
Loading