mirror of
https://github.com/nushell/nushell.git
synced 2025-04-25 05:38:20 +02:00
# 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**  **After**  # 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 <priyank.singh@soroco.com>
This commit is contained in:
parent
e234f3ea7b
commit
773dafa8ac
@ -107,6 +107,11 @@ impl Command for SubCommand {
|
|||||||
example: "4KB | into filesize",
|
example: "4KB | into filesize",
|
||||||
result: Some(Value::test_filesize(4000)),
|
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,7 +145,21 @@ fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
|||||||
// Now that we know the locale, get the thousands separator and remove it
|
// Now that we know the locale, get the thousands separator and remove it
|
||||||
// so strings like 1,123,456 can be parsed as 1123456
|
// so strings like 1,123,456 can be parsed as 1123456
|
||||||
let no_comma_string = a_string.replace(locale.separator(), "");
|
let no_comma_string = a_string.replace(locale.separator(), "");
|
||||||
match no_comma_string.trim().parse::<bytesize::ByteSize>() {
|
let clean_string = no_comma_string.trim();
|
||||||
|
|
||||||
|
// Hadle negative file size
|
||||||
|
if let Some(stripped_string) = clean_string.strip_prefix('-') {
|
||||||
|
match stripped_string.parse::<bytesize::ByteSize>() {
|
||||||
|
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::<bytesize::ByteSize>() {
|
||||||
Ok(n) => Ok(n.0 as i64),
|
Ok(n) => Ok(n.0 as i64),
|
||||||
Err(_) => Err(ShellError::CantConvert {
|
Err(_) => Err(ShellError::CantConvert {
|
||||||
to_type: "int".into(),
|
to_type: "int".into(),
|
||||||
@ -150,6 +169,7 @@ fn int_from_string(a_string: &str, span: Span) -> Result<i64, ShellError> {
|
|||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
@ -61,3 +61,10 @@ fn into_filesize_negative_filesize() {
|
|||||||
|
|
||||||
assert!(actual.out.contains("-3.0 KiB"));
|
assert!(actual.out.contains("-3.0 KiB"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn into_negative_filesize() {
|
||||||
|
let actual = nu!("'-1' | into filesize");
|
||||||
|
|
||||||
|
assert!(actual.out.contains("-1 B"));
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user