Add show_unit option to FilesizeFormatter

This commit is contained in:
Ian Manske 2025-03-09 14:00:46 -07:00
parent d97b2e3c60
commit 689d72dfea

View File

@ -610,6 +610,7 @@ impl FromStr for FilesizeUnitFormat {
/// ///
/// [`FilesizeFormatter`] is a builder struct that you can modify via the following methods: /// [`FilesizeFormatter`] is a builder struct that you can modify via the following methods:
/// - [`unit`](Self::unit) /// - [`unit`](Self::unit)
/// - [`show_unit`](Self::show_unit)
/// - [`precision`](Self::precision) /// - [`precision`](Self::precision)
/// - [`locale`](Self::locale) /// - [`locale`](Self::locale)
/// ///
@ -637,6 +638,7 @@ impl FromStr for FilesizeUnitFormat {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FilesizeFormatter { pub struct FilesizeFormatter {
unit: FilesizeUnitFormat, unit: FilesizeUnitFormat,
show_unit: bool,
precision: Option<usize>, precision: Option<usize>,
locale: Locale, locale: Locale,
} }
@ -646,12 +648,14 @@ impl FilesizeFormatter {
/// ///
/// The default formatter has: /// The default formatter has:
/// - a [`unit`](Self::unit) of [`FilesizeUnitFormat::Metric`]. /// - a [`unit`](Self::unit) of [`FilesizeUnitFormat::Metric`].
/// - a [`show_unit`](Self::show_unit) of `true`.
/// - a [`precision`](Self::precision) of `None`. /// - a [`precision`](Self::precision) of `None`.
/// - a [`locale`](Self::locale) of [`Locale::en_US_POSIX`] /// - a [`locale`](Self::locale) of [`Locale::en_US_POSIX`]
/// (a very plain format with no thousands separators). /// (a very plain format with no thousands separators).
pub fn new() -> Self { pub fn new() -> Self {
FilesizeFormatter { FilesizeFormatter {
unit: FilesizeUnitFormat::Metric, unit: FilesizeUnitFormat::Metric,
show_unit: true,
precision: None, precision: None,
locale: Locale::en_US_POSIX, locale: Locale::en_US_POSIX,
} }
@ -682,6 +686,30 @@ impl FilesizeFormatter {
self self
} }
/// Sets whether to show or omit the file size unit in the formatted output.
///
/// This setting can be used to disable the unit formatting from [`FilesizeFormatter`]
/// and instead provide your own.
///
/// Note that the [`FilesizeUnitFormat`] provided to [`unit`](Self::unit) is still used to
/// format the numeric portion of a [`Filesize`]. So, setting `show_unit` to `false` is only
/// recommended for [`FilesizeUnitFormat::Unit`], since this will keep the unit the same
/// for all [`Filesize`]s. [`FilesizeUnitFormat::Metric`] and [`FilesizeUnitFormat::Binary`],
/// on the other hand, will adapt the unit to match the magnitude of each formatted [`Filesize`].
///
/// # Examples
/// ```
/// # use nu_protocol::{Filesize, FilesizeFormatter, FilesizeUnit};
/// let filesize = Filesize::from_unit(4, FilesizeUnit::KiB).unwrap();
/// let formatter = FilesizeFormatter::new().show_unit(false);
///
/// assert_eq!(formatter.unit(FilesizeUnit::B).format(filesize).to_string(), "4096");
/// assert_eq!(format!("{} KB", formatter.unit(FilesizeUnit::KiB).format(filesize)), "4 KB");
/// ```
pub fn show_unit(self, show_unit: bool) -> Self {
Self { show_unit, ..self }
}
/// Set the number of digits to display after the decimal place. /// Set the number of digits to display after the decimal place.
/// ///
/// Note that digits after the decimal place will never be shown if: /// Note that digits after the decimal place will never be shown if:
@ -790,6 +818,7 @@ impl fmt::Display for FormattedFilesize {
let Self { filesize, format } = *self; let Self { filesize, format } = *self;
let FilesizeFormatter { let FilesizeFormatter {
unit, unit,
show_unit,
precision, precision,
locale, locale,
} = format; } = format;
@ -839,7 +868,10 @@ impl fmt::Display for FormattedFilesize {
} }
} }
if show_unit {
write!(f, " {unit}")?; write!(f, " {unit}")?;
}
Ok(()) Ok(())
} }
} }