Respect system locale when formatting file sizes via config (#15271)

# Description

Commands and other pieces of code using `$env.config.format.filesize` to
format filesizes now respect the system locale when formatting the
numeric portion of a file size.

# User-Facing Changes

- System locale is respected when using `$env.config.format.filesize` to
format file sizes.
- Formatting a file size with a binary unit is now exact for large file
sizes and units.
- The output of `to text` is no longer dependent on the config.
This commit is contained in:
Ian Manske
2025-03-09 13:43:02 -07:00
committed by GitHub
parent 4fe7865ad0
commit d97b2e3c60
8 changed files with 412 additions and 186 deletions

View File

@ -1,9 +1,9 @@
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_engine::command_prelude::*;
use nu_protocol::{engine::StateWorkingSet, FilesizeUnit};
use nu_protocol::{engine::StateWorkingSet, FilesizeFormatter, FilesizeUnit};
struct Arguments {
format: FilesizeUnit,
unit: FilesizeUnit,
cell_paths: Option<Vec<CellPath>>,
}
@ -61,10 +61,10 @@ impl Command for FormatFilesize {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let format = parse_filesize_unit(call.req::<Spanned<String>>(engine_state, stack, 0)?)?;
let unit = 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, cell_paths };
let arg = Arguments { unit, cell_paths };
operate(
format_value_impl,
arg,
@ -80,10 +80,10 @@ impl Command for FormatFilesize {
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let format = parse_filesize_unit(call.req_const::<Spanned<String>>(working_set, 0)?)?;
let unit = 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, cell_paths };
let arg = Arguments { unit, cell_paths };
operate(
format_value_impl,
arg,
@ -127,7 +127,11 @@ fn parse_filesize_unit(format: Spanned<String>) -> Result<FilesizeUnit, ShellErr
fn format_value_impl(val: &Value, arg: &Arguments, span: Span) -> Value {
let value_span = val.span();
match val {
Value::Filesize { val, .. } => Value::string(val.display(arg.format).to_string(), span),
Value::Filesize { val, .. } => FilesizeFormatter::new()
.unit(arg.unit)
.format(*val)
.to_string()
.into_value(span),
Value::Error { .. } => val.clone(),
_ => Value::error(
ShellError::OnlySupportsThisInputType {