Add locale option to filesize display

This commit is contained in:
Ian Manske
2025-03-07 22:00:18 -08:00
parent 7d17c2eb5e
commit f4c6ce6d06
7 changed files with 266 additions and 162 deletions

View File

@ -171,7 +171,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, .. } => config.filesize.format(val).to_string(),
Value::Duration { val, .. } => format_duration(val),
Value::Date { val, .. } => {
format!("{} ({})", val.to_rfc2822(), HumanTime::from(val))

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, FilesizeFormat, 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,13 @@ 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, .. } => Value::string(
FilesizeFormat::new()
.unit(arg.unit)
.format(*val)
.to_string(),
span,
),
Value::Error { .. } => val.clone(),
_ => Value::error(
ShellError::OnlySupportsThisInputType {