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
This commit is contained in:
Darren Schroeder 2021-05-26 18:02:24 -05:00 committed by GitHub
parent 6ae7884786
commit d8c4b9c4fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 4 deletions

30
Cargo.lock generated
View file

@ -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"

View file

@ -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" }

View file

@ -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::<Vec<&str>>();
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()