forked from extern/nushell
Add decimals to int when using into string --decimals
(#6085)
* Add decimals to int when using `into string --decimals` * Add tests for `into string` when converting int with `--decimals` * Apply formatting * Merge `into_str` test files * Comment out unused code and add TODOs * Use decimal separator depending on system locale * Add test helper to run closure in different locale * Add tests for int-to-string conversion using different locales * Add utils function to get system locale * Add panic message when locking mutex fails * Catch and resume panic later to prevent Mutex poisoning when test fails * Move test to `nu-test-support` to keep `nu-utils` free of `nu-*` dependencies See https://github.com/nushell/nushell/pull/6085#issuecomment-1193131694 * Rename test support fn `with_fake_locale` to `with_locale_override` * Move `get_system_locale()` to `locale` module * Allow overriding locale with special env variable (when not in release) * Use special env var to override locale during testing * Allow callback to return a value in `with_locale_override()` * Allow multiple options in `nu!` macro * Allow to set locale as `nu!` macro option * Use new `locale` option of `nu!` macro instead of `with_locale_override` Using the `locale` options does not lock the `LOCALE_OVERRIDE_MUTEX` mutex in `nu-test-support::locale_override` but instead calls the `nu` command directly with the `NU_LOCALE_OVERRIDE` environment variable. This allows for parallel test excecution. * Fix: Add option identifier for `cwd` in usage of `nu!` macro * Rely on `Display` trait for formatting `nu!` macro command - Removed the `DisplayPath` trait - Implement `Display` for `AbsolutePath`, `RelativePath` and `AbsoluteFile` * Default to locale `en_US.UTF-8` for tests when using `nu!` macro * Add doc comment to `nu!` macro * Format code using `cargo fmt --all` * Pass function directly instead of wrapping the call in a closure https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure * Pass function to `or_else()` instead of calling it inside `or()` https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call * Fix: Add option identifier for `cwd` in usage of `nu!` macro
This commit is contained in:
@ -16,7 +16,8 @@ pub use custom_value::CustomValue;
|
||||
use fancy_regex::Regex;
|
||||
pub use from_value::FromValue;
|
||||
use indexmap::map::IndexMap;
|
||||
use num_format::{Locale, ToFormattedString};
|
||||
use nu_utils::get_system_locale;
|
||||
use num_format::ToFormattedString;
|
||||
pub use range::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
@ -28,7 +29,6 @@ use std::{
|
||||
{cmp::Ordering, fmt::Debug},
|
||||
};
|
||||
pub use stream::*;
|
||||
use sys_locale::get_locale;
|
||||
pub use unit::*;
|
||||
|
||||
/// Core structured values that pass through the pipeline in Nushell.
|
||||
@ -2760,25 +2760,7 @@ pub fn format_filesize(num_bytes: i64, format_value: &str, filesize_metric: bool
|
||||
|
||||
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_string = locale_string.replace('_', "-"); // en_AU -> en-AU
|
||||
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 = get_system_locale();
|
||||
let locale_byte = adj_byte.get_value() as u64;
|
||||
let locale_byte_string = locale_byte.to_formatted_string(&locale);
|
||||
|
||||
|
Reference in New Issue
Block a user