From 1549944789e5f8ec3c53c25f28f828ad710a1b40 Mon Sep 17 00:00:00 2001 From: LeoniePhiline <22329650+LeoniePhiline@users.noreply.github.com> Date: Thu, 4 Jul 2024 22:39:08 +0200 Subject: [PATCH] feat: determine system locale to use in locale-aware unicode sorting Prerequisite to an implementation of #1006[1] following the example of tidy's `sort_carefully`[2]. [1]: https://github.com/eza-community/eza/issues/1006 [2]: https://github.com/sts10/tidy/blob/main/src/list_manipulations.rs#L24 --- Cargo.lock | 45 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 23 +++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e4bd8220b..167df0892 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -346,6 +346,17 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "dunce" version = "1.0.4" @@ -394,6 +405,7 @@ dependencies = [ "criterion", "git2", "glob", + "icu_locid", "libc", "locale", "log", @@ -529,6 +541,18 @@ dependencies = [ "cc", ] +[[package]] +name = "icu_locid" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13acbb8371917fc971be86fc8057c41a64b521c184808a698c02acc242dbf637" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", +] + [[package]] name = "idna" version = "0.2.3" @@ -637,6 +661,12 @@ version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "969488b55f8ac402214f3f5fd243ebb7206cf82de60d3172994707a4bcc2b829" +[[package]] +name = "litemap" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "643cb0b8d4fcc284004d5fd0d67ccf61dfffadb7f75e1e71bc420f4688a3a704" + [[package]] name = "locale" version = "0.2.2" @@ -1220,6 +1250,15 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1710e589de0a76aaf295cd47a6699f6405737dbfd3cf2b75c92d000b548d0e6" +[[package]] +name = "tinystr" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9117f5d4db391c1cf6927e7bea3db74b9a1c1add8f7eda9ffd5364f40f57b82f" +dependencies = [ + "displaydoc", +] + [[package]] name = "tinytemplate" version = "1.2.1" @@ -1621,6 +1660,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "writeable" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" + [[package]] name = "zoneinfo_compiled" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index 1603d7e12..cd70bd43e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,6 +74,7 @@ name = "eza" nu-ansi-term = "0.50.0" chrono = { version = "0.4.34", default-features = false, features = ["clock"] } glob = "0.3" +icu_locid = "1.5.0" libc = "0.2" locale = "0.2" log = "0.4" diff --git a/src/main.rs b/src/main.rs index 162e950cc..e2da914c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,6 +81,25 @@ fn main() { } } + let lang = if let Ok(lang) = std::env::var("LANG") { + // Split e.g. "en_US.UTF-8", extracting "en_US". + match lang.split_once('.') { + Some((lang, _)) => lang.to_owned(), + None => lang, + } + } else { + // Fall back to "en_US" if the `LANG` environment variable is not set. + warn!("`LANG` is not set. Falling back to 'en_US'."); + String::from("en_US") + }; + + let locale: icu_locid::Locale = if let Ok(locale) = lang.parse() { + locale + } else { + warn!("Your locale '{lang}' could not be parsed. Falling back to 'en_US'."); + "en_US".parse().unwrap() + }; + let git = git_options(&options, &input_paths); let writer = io::stdout(); let git_repos = git_repos(&options, &input_paths); @@ -91,6 +110,7 @@ fn main() { options, writer, input_paths, + locale, theme, console_width, git, @@ -149,6 +169,9 @@ pub struct Exa<'args> { /// names (anything that isn’t an option). pub input_paths: Vec<&'args OsStr>, + /// The system locale, used for locale-aware unicode string sorting. + pub locale: icu_locid::Locale, + /// The theme that has been configured from the command-line options and /// environment variables. If colours are disabled, this is a theme with /// every style set to the default.