Skip to content

Commit

Permalink
Improve and fix filesize formatting/display (#14397)
Browse files Browse the repository at this point in the history
# Description

This PR cleans up the code surrounding formatting and displaying file
sizes.
- The `byte_unit` crate we use for file size units displays kilobytes as
`KB`, which is not the SI or ISO/IEC standard. Rather it should be `kB`,
so this fixes #8872. On some systems, `KB` actually means `KiB`, so this
avoids any potential confusion.
- The `byte_unit` crate, when displaying file sizes, casts integers to
floats which will lose precision for large file sizes. This PR adds a
custom `Display` implementation for `Filesize` that can give an exact
string representation of a `Filesize` for metric/SI units.
- This PR also removes the dependency on the `byte_unit` crate which
brought in several other dependencies.

Additionally, this PR makes some changes to the config for filesize
formatting (`$env.config.filesize`).
- The previous filesize config had the `metric` and `format` options. If
a metric (SI) unit was set in `format`, but `metric` was set to false,
then the `metric` option would take precedence and convert `format` to
the corresponding binary unit (or vice versa). E.g., `{ format: kB,
metric: false }` => `KiB`. Instead, this PR adds the `unit` option to
replace the `format` and `metric` options. `unit` can be set to a fixed
file size unit like `kB` or `KiB`, or it can be set to one of the
special options: `binary` or `metric`. These options tells nushell to
format file sizes using an appropriately scaled metric or binary unit
(examples below).
  ```nushell
  # precision = null

  # unit = kB
  1kB  # 1 kB
  1KiB # 1.024 kB
  
  # unit = KiB
  1kB  # 0.9765625 KiB
  1KiB # 1 KiB
  
  # unit = metric
  1000B     # 1 kB
  1024B     # 1.024 kB
  10_000MB  # 10 GB
  10_240MiB # 10.73741824 GB

  # unit = binary
  1000B     # 1000 B
  1024B     # 1 KiB
  10_000MB  # 9.313225746154785 GiB
  10_240MiB # 10 GiB
  ```
- In addition, this PR also adds the `precision` option to the filesize
config. It determines how many digits to show after the decimal point.
If set to null, then everything after the decimal point is shown.
- The default filesize config is `{ unit: metric, precision: 1 }`.

# User-Facing Changes

- Commands that use the config to format file sizes will follow the
changes described above (e.g., `table`, `into string`, `to text`, etc.).
- The file size unit/format passed to `format filesize` is now case
sensitive. An error with the valid units is shown if the case does not
match.
- `$env.config.filesize.format` and `$env.config.filesize.metric` are
deprecated and replaced by `$env.config.filesize.unit`.
- A new `$env.config.filesize.precision` option was added.

# Tests + Formatting

Mostly updated test expected outputs.

# After Submitting

This PR does not change the way NUON serializes file sizes, because that
would require changing the nu parser to be able to losslessly decode the
new, exact string representation introduced in this PR.

Similarly, this PR also does not change the file size parsing in any
way. Although the file size units provided to `format filesize` or the
filesize config are now case-sensitive, the same is not yet true for
file size literals in nushell code.
  • Loading branch information
IanManske authored Jan 23, 2025
1 parent befedda commit 93e1217
Show file tree
Hide file tree
Showing 23 changed files with 433 additions and 546 deletions.
225 changes: 12 additions & 213 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions crates/nu-command/src/conversions/into/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl Command for SubCommand {
},
Example {
description: "convert filesize to string",
example: "1KiB | into string",
result: Some(Value::test_string("1.0 KiB")),
example: "1kB | into string",
result: Some(Value::test_string("1.0 kB")),
},
Example {
description: "convert duration to string",
Expand Down
6 changes: 2 additions & 4 deletions crates/nu-command/src/formats/to/text.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use chrono_humanize::HumanTime;
use nu_engine::command_prelude::*;
use nu_protocol::{
format_duration, format_filesize_from_conf, ByteStream, Config, PipelineMetadata,
};
use nu_protocol::{format_duration, ByteStream, Config, PipelineMetadata};
use std::io::Write;

const LINE_ENDING: &str = if cfg!(target_os = "windows") {
Expand Down Expand Up @@ -170,7 +168,7 @@ fn local_into_string(
Value::Bool { val, .. } => val.to_string(),
Value::Int { val, .. } => val.to_string(),
Value::Float { val, .. } => val.to_string(),
Value::Filesize { val, .. } => format_filesize_from_conf(val, config),
Value::Filesize { val, .. } => config.filesize.display(val).to_string(),
Value::Duration { val, .. } => format_duration(val),
Value::Date { val, .. } => {
format!("{} ({})", val.to_rfc2822(), HumanTime::from(val))
Expand Down
3 changes: 1 addition & 2 deletions crates/nu-command/src/random/binary.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use nu_engine::command_prelude::*;
use nu_protocol::format_filesize_from_conf;
use rand::{thread_rng, RngCore};

#[derive(Clone)]
Expand Down Expand Up @@ -47,7 +46,7 @@ impl Command for SubCommand {
Value::Filesize { val, .. } => {
usize::try_from(val).map_err(|_| ShellError::InvalidValue {
valid: "a non-negative int or filesize".into(),
actual: format_filesize_from_conf(val, engine_state.get_config()),
actual: engine_state.get_config().filesize.display(val).to_string(),
span: length_val.span(),
})
}
Expand Down
3 changes: 1 addition & 2 deletions crates/nu-command/src/random/chars.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use nu_engine::command_prelude::*;
use nu_protocol::format_filesize_from_conf;
use rand::{
distributions::{Alphanumeric, Distribution},
thread_rng,
Expand Down Expand Up @@ -84,7 +83,7 @@ fn chars(
Value::Filesize { val, .. } => {
usize::try_from(val).map_err(|_| ShellError::InvalidValue {
valid: "a non-negative int or filesize".into(),
actual: format_filesize_from_conf(val, engine_state.get_config()),
actual: engine_state.get_config().filesize.display(val).to_string(),
span: length_val.span(),
})
}
Expand Down
44 changes: 19 additions & 25 deletions crates/nu-command/src/strings/format/filesize.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::command_prelude::*;
use nu_protocol::{engine::StateWorkingSet, format_filesize};
use nu_protocol::{engine::StateWorkingSet, FilesizeUnit};

struct Arguments {
format_value: String,
format: FilesizeUnit,
cell_paths: Option<Vec<CellPath>>,
}

Expand Down Expand Up @@ -61,16 +61,10 @@ impl Command for FormatFilesize {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let format_value = call
.req::<Value>(engine_state, stack, 0)?
.coerce_into_string()?
.to_ascii_lowercase();
let format = parse_filesize_unit(call.req::<Spanned<String>>(engine_state, stack, 0)?)?;
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
let arg = Arguments {
format_value,
cell_paths,
};
let arg = Arguments { format, cell_paths };
operate(
format_value_impl,
arg,
Expand All @@ -86,16 +80,10 @@ impl Command for FormatFilesize {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let format_value = call
.req_const::<Value>(working_set, 0)?
.coerce_into_string()?
.to_ascii_lowercase();
let format = parse_filesize_unit(call.req_const::<Spanned<String>>(working_set, 0)?)?;
let cell_paths: Vec<CellPath> = call.rest_const(working_set, 1)?;
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
let arg = Arguments {
format_value,
cell_paths,
};
let arg = Arguments { format, cell_paths };
operate(
format_value_impl,
arg,
Expand All @@ -119,21 +107,27 @@ impl Command for FormatFilesize {
},
Example {
description: "Convert the size data to MB",
example: "4Gb | format filesize MB",
result: Some(Value::test_string("4000.0 MB")),
example: "4GB | format filesize MB",
result: Some(Value::test_string("4000 MB")),
},
]
}
}

fn parse_filesize_unit(format: Spanned<String>) -> Result<FilesizeUnit, ShellError> {
format.item.parse().map_err(|_| ShellError::InvalidValue {
valid:
"'B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', or 'EiB'"
.into(),
actual: format.item,
span: format.span,
})
}

fn format_value_impl(val: &Value, arg: &Arguments, span: Span) -> Value {
let value_span = val.span();
match val {
Value::Filesize { val, .. } => Value::string(
// don't need to concern about metric, we just format units by what user input.
format_filesize(*val, &arg.format_value, None),
span,
),
Value::Filesize { val, .. } => Value::string(val.display(arg.format).to_string(), span),
Value::Error { .. } => val.clone(),
_ => Value::error(
ShellError::OnlySupportsThisInputType {
Expand Down
8 changes: 4 additions & 4 deletions crates/nu-command/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ fn format_filesize_works() {
cwd: dirs.test(), pipeline(
"
ls
| format filesize KB size
| format filesize kB size
| get size
| first
"
));

assert_eq!(actual.out, "0.0 KB");
assert_eq!(actual.out, "0 kB");
})
}

Expand All @@ -105,10 +105,10 @@ fn format_filesize_works_with_nonempty_files() {
);

#[cfg(not(windows))]
assert_eq!(actual.out, "25");
assert_eq!(actual.out, "25 B");

#[cfg(windows)]
assert_eq!(actual.out, "27");
assert_eq!(actual.out, "27 B");
},
)
}
69 changes: 33 additions & 36 deletions crates/nu-command/tests/commands/into_filesize.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
use nu_test_support::{nu, pipeline};

#[test]
fn into_filesize_int() {
fn int() {
let actual = nu!("1 | into filesize");

assert!(actual.out.contains("1 B"));
}

#[test]
fn into_filesize_float() {
fn float() {
let actual = nu!("1.2 | into filesize");

assert!(actual.out.contains("1 B"));
}

#[test]
fn into_filesize_str() {
let actual = nu!(r#"
'2000' | into filesize
"#);

assert!(actual.out.contains("2.0 KiB"));
fn str() {
let actual = nu!("'2000' | into filesize");
assert!(actual.out.contains("2.0 kB"));
}

#[test]
fn into_filesize_str_newline() {
fn str_newline() {
let actual = nu!(pipeline(
r#"
"2000
" | into filesize
"#
));

assert!(actual.out.contains("2.0 KiB"));
assert!(actual.out.contains("2.0 kB"));
}

#[test]
fn into_filesize_str_many_newlines() {
fn str_many_newlines() {
let actual = nu!(pipeline(
r#"
"2000
Expand All @@ -45,88 +42,88 @@ fn into_filesize_str_many_newlines() {
"#
));

assert!(actual.out.contains("2.0 KiB"));
assert!(actual.out.contains("2.0 kB"));
}

#[test]
fn into_filesize_filesize() {
let actual = nu!("3kib | into filesize");
fn filesize() {
let actual = nu!("3kB | into filesize");

assert!(actual.out.contains("3.0 KiB"));
assert!(actual.out.contains("3.0 kB"));
}

#[test]
fn into_filesize_negative_filesize() {
let actual = nu!("-3kib | into filesize");
fn negative_filesize() {
let actual = nu!("-3kB | into filesize");

assert!(actual.out.contains("-3.0 KiB"));
assert!(actual.out.contains("-3.0 kB"));
}

#[test]
fn into_filesize_negative_str_filesize() {
let actual = nu!("'-3kib' | into filesize");
fn negative_str_filesize() {
let actual = nu!("'-3kB' | into filesize");

assert!(actual.out.contains("-3.0 KiB"));
assert!(actual.out.contains("-3.0 kB"));
}

#[test]
fn into_filesize_wrong_negative_str_filesize() {
let actual = nu!("'--3kib' | into filesize");
fn wrong_negative_str_filesize() {
let actual = nu!("'--3kB' | into filesize");

assert!(actual.err.contains("can't convert string to filesize"));
}

#[test]
fn into_filesize_large_negative_str_filesize() {
let actual = nu!("'-10000PiB' | into filesize");
fn large_negative_str_filesize() {
let actual = nu!("'-10000PB' | into filesize");

assert!(actual.err.contains("can't convert string to filesize"));
}

#[test]
fn into_filesize_negative_str() {
fn negative_str() {
let actual = nu!("'-1' | into filesize");

assert!(actual.out.contains("-1 B"));
}

#[test]
fn into_filesize_wrong_negative_str() {
fn wrong_negative_str() {
let actual = nu!("'--1' | into filesize");

assert!(actual.err.contains("can't convert string to filesize"));
}

#[test]
fn into_filesize_positive_str_filesize() {
let actual = nu!("'+1Kib' | into filesize");
fn positive_str_filesize() {
let actual = nu!("'+1kB' | into filesize");

assert!(actual.out.contains("1.0 KiB"));
assert!(actual.out.contains("1.0 kB"));
}

#[test]
fn into_filesize_wrong_positive_str_filesize() {
let actual = nu!("'++1Kib' | into filesize");
fn wrong_positive_str_filesize() {
let actual = nu!("'++1kB' | into filesize");

assert!(actual.err.contains("can't convert string to filesize"));
}

#[test]
fn into_filesize_large_positive_str_filesize() {
let actual = nu!("'+10000PiB' | into filesize");
fn large_positive_str_filesize() {
let actual = nu!("'+10000PB' | into filesize");

assert!(actual.err.contains("can't convert string to filesize"));
}

#[test]
fn into_filesize_positive_str() {
fn positive_str() {
let actual = nu!("'+1' | into filesize");

assert!(actual.out.contains("1 B"));
}

#[test]
fn into_filesize_wrong_positive_str() {
fn wrong_positive_str() {
let actual = nu!("'++1' | into filesize");

assert!(actual.err.contains("can't convert string to filesize"));
Expand Down
Loading

0 comments on commit 93e1217

Please sign in to comment.