nu-parser + nu-protocol: switch to metric for KB, MB, GB, add KiB, MiB, GiB units (#3035)

fixes inconsistency with formatting/rendering which uses standard Rust byte_unit
https://en.wikipedia.org/wiki/Byte#Multiple-byte_units
This commit is contained in:
Saeed Rasooli
2021-02-10 06:01:12 +03:30
committed by GitHub
parent 3443ca40c5
commit b403fb1275
7 changed files with 128 additions and 17 deletions

View File

@ -346,7 +346,7 @@ impl HasSpan for ExternalCommand {
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone, Hash, Copy, Deserialize, Serialize)]
pub enum Unit {
// Filesize units
// Filesize units: metric
Byte,
Kilobyte,
Megabyte,
@ -354,6 +354,11 @@ pub enum Unit {
Terabyte,
Petabyte,
// Filesize units: ISO/IEC 80000
Kibibyte,
Mebibyte,
Gibibyte,
// Duration units
Nanosecond,
Microsecond,
@ -540,6 +545,9 @@ impl Unit {
Unit::Gigabyte => "GB",
Unit::Terabyte => "TB",
Unit::Petabyte => "PB",
Unit::Kibibyte => "KiB",
Unit::Mebibyte => "MiB",
Unit::Gibibyte => "GiB",
Unit::Nanosecond => "ns",
Unit::Microsecond => "us",
Unit::Millisecond => "ms",
@ -558,13 +566,18 @@ impl Unit {
match self {
Unit::Byte => filesize(convert_number_to_u64(&size)),
Unit::Kilobyte => filesize(convert_number_to_u64(&size) * 1024),
Unit::Megabyte => filesize(convert_number_to_u64(&size) * 1024 * 1024),
Unit::Gigabyte => filesize(convert_number_to_u64(&size) * 1024 * 1024 * 1024),
Unit::Terabyte => filesize(convert_number_to_u64(&size) * 1024 * 1024 * 1024 * 1024),
Unit::Kilobyte => filesize(convert_number_to_u64(&size) * 1000),
Unit::Megabyte => filesize(convert_number_to_u64(&size) * 1000 * 1000),
Unit::Gigabyte => filesize(convert_number_to_u64(&size) * 1000 * 1000 * 1000),
Unit::Terabyte => filesize(convert_number_to_u64(&size) * 1000 * 1000 * 1000 * 1000),
Unit::Petabyte => {
filesize(convert_number_to_u64(&size) * 1024 * 1024 * 1024 * 1024 * 1024)
filesize(convert_number_to_u64(&size) * 1000 * 1000 * 1000 * 1000 * 1000)
}
Unit::Kibibyte => filesize(convert_number_to_u64(&size) * 1024),
Unit::Mebibyte => filesize(convert_number_to_u64(&size) * 1024 * 1024),
Unit::Gibibyte => filesize(convert_number_to_u64(&size) * 1024 * 1024 * 1024),
Unit::Nanosecond => duration(size.to_bigint().expect("Conversion should never fail.")),
Unit::Microsecond => {
duration(size.to_bigint().expect("Conversion should never fail.") * 1000)

View File

@ -3,7 +3,10 @@ use nu_errors::ShellError;
use std::path::{Component, Path, PathBuf};
fn is_value_tagged_dir(value: &Value) -> bool {
matches!(&value.value, UntaggedValue::Row(_) | UntaggedValue::Table(_))
matches!(
&value.value,
UntaggedValue::Row(_) | UntaggedValue::Table(_)
)
}
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]