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,8 +1,6 @@
use chrono_humanize::HumanTime;
use nu_engine::command_prelude::*;
use nu_protocol::{
format_duration, shell_error::io::IoError, ByteStream, Config, PipelineMetadata,
};
use nu_protocol::{format_duration, shell_error::io::IoError, ByteStream, PipelineMetadata};
use std::io::Write;
const LINE_ENDING: &str = if cfg!(target_os = "windows") {
@ -50,7 +48,6 @@ impl Command for ToText {
let no_newline = call.has_flag(engine_state, stack, "no-newline")?;
let serialize_types = call.has_flag(engine_state, stack, "serialize")?;
let input = input.try_expand_range()?;
let config = stack.get_config(engine_state);
match input {
PipelineData::Empty => Ok(Value::string(String::new(), head)
@ -62,8 +59,7 @@ impl Command for ToText {
Value::Record { val, .. } => !val.is_empty(),
_ => false,
};
let mut str =
local_into_string(engine_state, value, LINE_ENDING, &config, serialize_types);
let mut str = local_into_string(engine_state, value, LINE_ENDING, serialize_types);
if add_trailing {
str.push_str(LINE_ENDING);
}
@ -98,7 +94,6 @@ impl Command for ToText {
&engine_state_clone,
val,
LINE_ENDING,
&config,
serialize_types,
);
write!(buf, "{str}").map_err(&from_io_error)?;
@ -113,7 +108,6 @@ impl Command for ToText {
&engine_state_clone,
val,
LINE_ENDING,
&config,
serialize_types,
);
str.push_str(LINE_ENDING);
@ -163,7 +157,6 @@ fn local_into_string(
engine_state: &EngineState,
value: Value,
separator: &str,
config: &Config,
serialize_types: bool,
) -> String {
let span = value.span();
@ -171,7 +164,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, .. } => config.filesize.display(val).to_string(),
Value::Filesize { val, .. } => val.to_string(),
Value::Duration { val, .. } => format_duration(val),
Value::Date { val, .. } => {
format!("{} ({})", val.to_rfc2822(), HumanTime::from(val))
@ -181,7 +174,7 @@ fn local_into_string(
Value::Glob { val, .. } => val,
Value::List { vals: val, .. } => val
.into_iter()
.map(|x| local_into_string(engine_state, x, ", ", config, serialize_types))
.map(|x| local_into_string(engine_state, x, ", ", serialize_types))
.collect::<Vec<_>>()
.join(separator),
Value::Record { val, .. } => val
@ -191,7 +184,7 @@ fn local_into_string(
format!(
"{}: {}",
x,
local_into_string(engine_state, y, ", ", config, serialize_types)
local_into_string(engine_state, y, ", ", serialize_types)
)
})
.collect::<Vec<_>>()
@ -221,7 +214,7 @@ fn local_into_string(
// that critical here
Value::Custom { val, .. } => val
.to_base_value(span)
.map(|val| local_into_string(engine_state, val, separator, config, serialize_types))
.map(|val| local_into_string(engine_state, val, separator, serialize_types))
.unwrap_or_else(|_| format!("<{}>", val.type_name())),
}
}

View File

@ -46,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: engine_state.get_config().filesize.display(val).to_string(),
actual: engine_state.get_config().filesize.format(val).to_string(),
span: length_val.span(),
})
}

View File

@ -83,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: engine_state.get_config().filesize.display(val).to_string(),
actual: engine_state.get_config().filesize.format(val).to_string(),
span: length_val.span(),
})
}

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 {