From d8c4b9c4fbe04b6ac54ab7530d6f157812c54ed6 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Wed, 26 May 2021 18:02:24 -0500 Subject: [PATCH] add locale for ls size (#3497) * add locale for ls size * updated to work when it's not an exact match like de_DE when it needs de --- Cargo.lock | 30 ++++++++++++++++++++++++++++++ crates/nu-data/Cargo.toml | 6 +++--- crates/nu-data/src/base/shape.rs | 21 ++++++++++++++++++++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a928d5b0a6..6b533731a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1206,6 +1206,16 @@ dependencies = [ "syn 1.0.71", ] +[[package]] +name = "cstr_core" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2807c5e92588b6bf1c8c0354af2a4f079d0586c683df322aea719d5dc9b8d5bb" +dependencies = [ + "cty", + "memchr", +] + [[package]] name = "csv" version = "1.1.6" @@ -1257,6 +1267,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "cty" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7313c0d620d0cb4dbd9d019e461a4beb501071ff46ec0ab933efb4daa76d73e3" + [[package]] name = "curl" version = "0.4.36" @@ -3586,6 +3602,7 @@ dependencies = [ "query_interface", "serde 1.0.125", "sha2 0.9.3", + "sys-locale", "toml", "users", ] @@ -6235,6 +6252,19 @@ dependencies = [ "yaml-rust", ] +[[package]] +name = "sys-locale" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91f89ebb59fa30d4f65fafc2d68e94f6975256fd87e812dd99cb6e020c8563df" +dependencies = [ + "cc", + "cstr_core", + "libc", + "web-sys", + "winapi 0.3.9", +] + [[package]] name = "sysinfo" version = "0.16.5" diff --git a/crates/nu-data/Cargo.toml b/crates/nu-data/Cargo.toml index 2842c23f3f..eeed54c4ea 100644 --- a/crates/nu-data/Cargo.toml +++ b/crates/nu-data/Cargo.toml @@ -12,8 +12,8 @@ doctest = false [dependencies] bigdecimal = "0.2.0" byte-unit = "4.0.9" - chrono = "0.4.19" +common-path = "1.0.0" derive-new = "0.5.8" directories-next = { version = "2.0.0", optional = true } dirs-next = { version = "2.0.0", optional = true } @@ -25,9 +25,9 @@ num-format = "0.4.0" num-traits = "0.2.14" query_interface = "0.3.5" serde = { version = "1.0.123", features = ["derive"] } -toml = "0.5.8" sha2 = "0.9.3" -common-path = "1.0.0" +sys-locale = "0.1.0" +toml = "0.5.8" nu-errors = { version = "0.31.1", path = "../nu-errors" } nu-protocol = { version = "0.31.1", path = "../nu-protocol" } diff --git a/crates/nu-data/src/base/shape.rs b/crates/nu-data/src/base/shape.rs index ed2e1bb513..0fd28ca978 100644 --- a/crates/nu-data/src/base/shape.rs +++ b/crates/nu-data/src/base/shape.rs @@ -11,6 +11,7 @@ use std::cmp::Ordering; use std::fmt::Debug; use std::hash::{Hash, Hasher}; use std::path::PathBuf; +use sys_locale::get_locale; #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)] pub struct InlineRange { @@ -195,8 +196,26 @@ impl InlineShape { match adj_byte.get_unit() { byte_unit::ByteUnit::B => { + let locale_string = get_locale().unwrap_or_else(|| String::from("en-US")); + // Since get_locale() and Locale::from_name() don't always return the same items + // we need to try and parse it to match. For instance, a valid locale is de_DE + // however Locale::from_name() wants only de so we split and parse it out. + let locale = match Locale::from_name(&locale_string) { + Ok(loc) => loc, + _ => { + let all = num_format::Locale::available_names(); + let locale_prefix = &locale_string.split('_').collect::>(); + if all.contains(&locale_prefix[0]) { + // eprintln!("Found alternate: {}", &locale_prefix[0]); + Locale::from_name(locale_prefix[0]).unwrap_or(Locale::en) + } else { + // eprintln!("Unable to find matching locale. Defaulting to en-US"); + Locale::en + } + } + }; let locale_byte = adj_byte.get_value() as u64; - let locale_byte_string = locale_byte.to_formatted_string(&Locale::en); + let locale_byte_string = locale_byte.to_formatted_string(&locale); if filesize_format.1 == "auto" { let doc = (DbgDocBldr::primitive(locale_byte_string) + DbgDocBldr::space()