From 378b8609aa25196fccaef52302fe4af547c96aea Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Thu, 21 Dec 2017 21:31:25 +0200 Subject: [PATCH] EpisodeWidget: Change the byte unit based on the size. Closes #18. --- Cargo.lock | 7 +++++++ hammond-gtk/Cargo.toml | 1 + hammond-gtk/resources/gtk/episode_widget.ui | 1 + hammond-gtk/src/main.rs | 1 + hammond-gtk/src/widgets/episode.rs | 10 ++++++++-- 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6fb2a52..cdba1e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -611,6 +611,7 @@ dependencies = [ "gtk 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "hammond-data 0.1.0", "hammond-downloader 0.1.0", + "humansize 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "loggerv 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -637,6 +638,11 @@ name = "httparse" version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "humansize" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "hyper" version = "0.11.9" @@ -1680,6 +1686,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum gtk-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "905fcfbaaad1b44ec0b4bba9e4d527d728284c62bc2ba41fccedace2b096766f" "checksum html5ever 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba3a1fd1857a714d410c191364c5d7bf8a6487c0ab5575146d37dd7eb17ef523" "checksum httparse 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "af2f2dd97457e8fb1ae7c5a420db346af389926e36f43768b96f101546b04a07" +"checksum humansize 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d99804bdb0790b0c312a5a1115f83804b821f1a96d80759fbb57ce796d1f3778" "checksum hyper 0.11.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e0594792d2109069d0caffd176f674d770a84adf024c5bb48e686b1ee5ac7659" "checksum hyper-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c81fa95203e2a6087242c38691a0210f23e9f3f8f944350bd676522132e2985" "checksum idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "014b298351066f1512874135335d62a789ffe78a9974f94b43ed5621951eaf7d" diff --git a/hammond-gtk/Cargo.toml b/hammond-gtk/Cargo.toml index 5f250f3..ceb387d 100644 --- a/hammond-gtk/Cargo.toml +++ b/hammond-gtk/Cargo.toml @@ -12,6 +12,7 @@ gdk = "0.7.0" gdk-pixbuf = "0.3.0" gio = "0.3.0" glib = "0.4.0" +humansize = "1.0.2" lazy_static = "1.0.0" log = "0.3.8" loggerv = "0.6.0" diff --git a/hammond-gtk/resources/gtk/episode_widget.ui b/hammond-gtk/resources/gtk/episode_widget.ui index e0aa126..b0332fc 100644 --- a/hammond-gtk/resources/gtk/episode_widget.ui +++ b/hammond-gtk/resources/gtk/episode_widget.ui @@ -122,6 +122,7 @@ True False + True 42 MB True False diff --git a/hammond-gtk/src/main.rs b/hammond-gtk/src/main.rs index dd3b96a..08bfeee 100644 --- a/hammond-gtk/src/main.rs +++ b/hammond-gtk/src/main.rs @@ -11,6 +11,7 @@ extern crate diesel; extern crate dissolve; extern crate hammond_data; extern crate hammond_downloader; +extern crate humansize; #[macro_use] extern crate lazy_static; #[macro_use] diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index 93ce6dd..ddc0e3c 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -5,6 +5,7 @@ use gtk::prelude::*; use chrono::prelude::*; use open; +use humansize::{file_size_opts as size_opts, FileSize}; use hammond_data::dbqueries; use hammond_data::{EpisodeWidgetQuery, Podcast}; @@ -101,9 +102,14 @@ impl EpisodeWidget { .map(|c| c.add_class("dim-label")); } + // TODO: configure it so it will not show decimal places. if let Some(size) = episode.length() { - let megabytes = size / 1024 / 1024; // episode.length represents bytes - self.size.set_text(&format!("{} MB", megabytes)) + let s = size.file_size(size_opts::CONVENTIONAL); + if let Ok(s) = s { + self.size.set_text(&s); + } else { + self.size.hide(); + } }; let date = Utc.timestamp(i64::from(episode.epoch()), 0)