Cleanup and fixes

This commit is contained in:
Ian Manske
2025-03-08 20:55:09 -08:00
parent e8c3694050
commit e116f8b32c
3 changed files with 10 additions and 19 deletions

View File

@ -127,14 +127,12 @@ 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(
FilesizeFormatter::new()
.unit(arg.unit)
.precision(None)
.format(*val)
.to_string(),
span,
),
Value::Filesize { val, .. } => FilesizeFormatter::new()
.unit(arg.unit)
.precision(None)
.format(*val)
.to_string()
.into_value(span),
Value::Error { .. } => val.clone(),
_ => Value::error(
ShellError::OnlySupportsThisInputType {

View File

@ -4,12 +4,7 @@ use nu_utils::get_system_locale;
impl IntoValue for FilesizeUnitFormat {
fn into_value(self, span: Span) -> Value {
match self {
FilesizeUnitFormat::Metric => "metric",
FilesizeUnitFormat::Binary => "binary",
FilesizeUnitFormat::Unit(unit) => unit.as_str(),
}
.into_value(span)
self.as_str().into_value(span)
}
}
@ -24,12 +19,11 @@ impl FilesizeConfig {
FilesizeFormatter::new()
.unit(self.unit)
.precision(self.precision)
.locale(get_system_locale()) // TODO: cache this somewhere or pass in as argument
}
pub fn format(&self, filesize: Filesize) -> FormattedFilesize {
self.formatter()
.locale(get_system_locale()) // TODO: cache this somewhere or pass in as argument
.format(filesize)
self.formatter().format(filesize)
}
}

View File

@ -751,7 +751,7 @@ impl FilesizeFormatter {
self
}
/// Format a [`Filesize`] into [`FormattedFilesize`] which implements [`fmt::Display`].
/// Format a [`Filesize`] into a [`FormattedFilesize`] which implements [`fmt::Display`].
///
/// # Examples
/// ```
@ -801,7 +801,6 @@ impl fmt::Display for FormattedFilesize {
let Filesize(filesize) = filesize;
let precision = f.precision().or(precision);
// Format an exact, possibly fractional, string representation of `filesize`.
let bytes = unit.as_bytes() as i64;
let whole = filesize / bytes;
let fract = (filesize % bytes).unsigned_abs();