properties_view: Use glib::format_size() for file size
This handles appropriate localized unit names and rounding for us.
This commit is contained in:
parent
3395de4661
commit
36cf667cc8
1 changed files with 2 additions and 17 deletions
|
|
@ -35,8 +35,6 @@ use std::cell::RefCell;
|
||||||
|
|
||||||
use gtk_macros::spawn;
|
use gtk_macros::spawn;
|
||||||
|
|
||||||
const MB: f64 = 1_000_000.0;
|
|
||||||
const KB: f64 = 1_000.0;
|
|
||||||
const FALLBACK: &str = "-";
|
const FALLBACK: &str = "-";
|
||||||
|
|
||||||
mod imp {
|
mod imp {
|
||||||
|
|
@ -338,21 +336,8 @@ impl LpPropertiesView {
|
||||||
#[template_callback]
|
#[template_callback]
|
||||||
fn file_size(&self, info: Option<gio::FileInfo>) -> String {
|
fn file_size(&self, info: Option<gio::FileInfo>) -> String {
|
||||||
info.map(|info| {
|
info.map(|info| {
|
||||||
let bytes = info.size() as f64;
|
let size = info.size() as u64;
|
||||||
|
glib::format_size(size).to_string()
|
||||||
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}"))
|
|
||||||
})
|
})
|
||||||
.unwrap_or_else(|| FALLBACK.to_owned())
|
.unwrap_or_else(|| FALLBACK.to_owned())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue