From 36cf667cc8e52cf257c5de86489db1c9807717c8 Mon Sep 17 00:00:00 2001 From: Christopher Davis Date: Fri, 5 Aug 2022 17:19:47 -0400 Subject: [PATCH] properties_view: Use glib::format_size() for file size This handles appropriate localized unit names and rounding for us. --- src/widgets/properties_view.rs | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/widgets/properties_view.rs b/src/widgets/properties_view.rs index a904448..385c496 100644 --- a/src/widgets/properties_view.rs +++ b/src/widgets/properties_view.rs @@ -35,8 +35,6 @@ use std::cell::RefCell; use gtk_macros::spawn; -const MB: f64 = 1_000_000.0; -const KB: f64 = 1_000.0; const FALLBACK: &str = "-"; mod imp { @@ -338,21 +336,8 @@ impl LpPropertiesView { #[template_callback] fn file_size(&self, info: Option) -> String { info.map(|info| { - let bytes = info.size() as f64; - - let (mut size, mut unit) = (bytes, "B"); - let sizes = [(MB, "MB"), (KB, "kB")]; - - for (b, u) in sizes { - if bytes > b { - let res = bytes / b; - size = res; - unit = u; - break; - } - } - - String::from(&format!("{size:.1} {unit}")) + let size = info.size() as u64; + glib::format_size(size).to_string() }) .unwrap_or_else(|| FALLBACK.to_owned()) }