From 773dafa8aceeb546f4d9e427a6ee3ba6677fb583 Mon Sep 17 00:00:00 2001 From: singh-priyank <43441496+singh-priyank@users.noreply.github.com> Date: Sun, 7 Apr 2024 22:20:11 +0530 Subject: [PATCH] Fix negative value file size for "into filesize" (issue #12396) (#12443) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Add support for using negative values file size for `into filesize`. This will help in sorting the file size if negative values are also passed. **Before** ![image](https://github.com/nushell/nushell/assets/43441496/e115b4b3-7526-4379-8dc0-f4f4e44839a1) **After** ![image](https://github.com/nushell/nushell/assets/43441496/4a75fb40-ebe6-46eb-b9d2-55f37db7a6fa) # User-Facing Changes - User can now sort negative filesize also # Tests + Formatting - 🟢 toolkit fmt - 🟢 toolkit clippy - 🟢 toolkit test - 🟢 toolkit test stdlib # After Submitting --------- Co-authored-by: Priyank Singh --- .../src/conversions/into/filesize.rs | 36 ++++++++++++++----- .../tests/commands/into_filesize.rs | 7 ++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/crates/nu-command/src/conversions/into/filesize.rs b/crates/nu-command/src/conversions/into/filesize.rs index c97209cd8c..706dc5c2c4 100644 --- a/crates/nu-command/src/conversions/into/filesize.rs +++ b/crates/nu-command/src/conversions/into/filesize.rs @@ -107,6 +107,11 @@ impl Command for SubCommand { example: "4KB | into filesize", result: Some(Value::test_filesize(4000)), }, + Example { + description: "Convert string with unit to filesize", + example: "'-1KB' | into filesize", + result: Some(Value::test_filesize(-1000)), + }, ] } } @@ -140,14 +145,29 @@ fn int_from_string(a_string: &str, span: Span) -> Result { // Now that we know the locale, get the thousands separator and remove it // so strings like 1,123,456 can be parsed as 1123456 let no_comma_string = a_string.replace(locale.separator(), ""); - match no_comma_string.trim().parse::() { - Ok(n) => Ok(n.0 as i64), - Err(_) => Err(ShellError::CantConvert { - to_type: "int".into(), - from_type: "string".into(), - span, - help: None, - }), + let clean_string = no_comma_string.trim(); + + // Hadle negative file size + if let Some(stripped_string) = clean_string.strip_prefix('-') { + match stripped_string.parse::() { + Ok(n) => Ok(-(n.as_u64() as i64)), + Err(_) => Err(ShellError::CantConvert { + to_type: "int".into(), + from_type: "string".into(), + span, + help: None, + }), + } + } else { + match clean_string.parse::() { + Ok(n) => Ok(n.0 as i64), + Err(_) => Err(ShellError::CantConvert { + to_type: "int".into(), + from_type: "string".into(), + span, + help: None, + }), + } } } diff --git a/crates/nu-command/tests/commands/into_filesize.rs b/crates/nu-command/tests/commands/into_filesize.rs index 4821d73205..2917e141db 100644 --- a/crates/nu-command/tests/commands/into_filesize.rs +++ b/crates/nu-command/tests/commands/into_filesize.rs @@ -61,3 +61,10 @@ fn into_filesize_negative_filesize() { assert!(actual.out.contains("-3.0 KiB")); } + +#[test] +fn into_negative_filesize() { + let actual = nu!("'-1' | into filesize"); + + assert!(actual.out.contains("-1 B")); +}