Add show_unit config option

This commit is contained in:
Ian Manske 2025-03-09 14:13:00 -07:00
parent 689d72dfea
commit b5eda61fcd
2 changed files with 11 additions and 1 deletions

View File

@ -11,6 +11,7 @@ impl IntoValue for FilesizeUnitFormat {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct FilesizeConfig { pub struct FilesizeConfig {
pub unit: FilesizeUnitFormat, pub unit: FilesizeUnitFormat,
pub show_unit: bool,
pub precision: Option<usize>, pub precision: Option<usize>,
} }
@ -18,6 +19,7 @@ impl FilesizeConfig {
pub fn formatter(&self) -> FilesizeFormatter { pub fn formatter(&self) -> FilesizeFormatter {
FilesizeFormatter::new() FilesizeFormatter::new()
.unit(self.unit) .unit(self.unit)
.show_unit(self.show_unit)
.precision(self.precision) .precision(self.precision)
.locale(get_system_locale()) // TODO: cache this somewhere or pass in as argument .locale(get_system_locale()) // TODO: cache this somewhere or pass in as argument
} }
@ -31,6 +33,7 @@ impl Default for FilesizeConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
unit: FilesizeUnitFormat::Metric, unit: FilesizeUnitFormat::Metric,
show_unit: true,
precision: Some(1), precision: Some(1),
} }
} }
@ -67,6 +70,7 @@ impl UpdateFromValue for FilesizeConfig {
errors.type_mismatch(path, Type::String, val) errors.type_mismatch(path, Type::String, val)
} }
} }
"show_unit" => self.show_unit.update(val, path, errors),
"precision" => match *val { "precision" => match *val {
Value::Nothing { .. } => self.precision = None, Value::Nothing { .. } => self.precision = None,
Value::Int { val, .. } if val >= 0 => self.precision = Some(val as usize), Value::Int { val, .. } if val >= 0 => self.precision = Some(val as usize),
@ -83,6 +87,7 @@ impl IntoValue for FilesizeConfig {
fn into_value(self, span: Span) -> Value { fn into_value(self, span: Span) -> Value {
record! { record! {
"unit" => self.unit.into_value(span), "unit" => self.unit.into_value(span),
"show_unit" => self.show_unit.into_value(span),
"precision" => self.precision.map(|x| x as i64).into_value(span), "precision" => self.precision.map(|x| x as i64).into_value(span),
} }
.into_value(span) .into_value(span)

View File

@ -381,9 +381,14 @@ $env.config.datetime_format.normal = "%m/%d/%y %I:%M:%S%p"
# Otherwise, setting this to one of the filesize units will use that particular unit when displaying all file sizes. # Otherwise, setting this to one of the filesize units will use that particular unit when displaying all file sizes.
$env.config.filesize.unit = 'metric' $env.config.filesize.unit = 'metric'
# filesize.show_unit (bool):
# Whether to show or hide the file size unit. Useful if `$env.config.filesize.unit` is set to a fixed unit,
# and you don't want that same unit repeated over and over again in which case you can set this to `false`.
$env.config.filesize.show_unit = true
# filesize.precision (int or nothing): # filesize.precision (int or nothing):
# The number of digits to display after the decimal point for file sizes. # The number of digits to display after the decimal point for file sizes.
# When set to `null`, all digits after the decimal point will be displayed. # When set to `null`, all digits after the decimal point, if any, will be displayed.
$env.config.filesize.precision = 1 $env.config.filesize.precision = 1
# --------------------- # ---------------------