summaryrefslogtreecommitdiffstats
path: root/src/display/components/display_bandwidth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/display/components/display_bandwidth.rs')
-rw-r--r--src/display/components/display_bandwidth.rs28
1 files changed, 13 insertions, 15 deletions
diff --git a/src/display/components/display_bandwidth.rs b/src/display/components/display_bandwidth.rs
index d34ffe1..4c7e010 100644
--- a/src/display/components/display_bandwidth.rs
+++ b/src/display/components/display_bandwidth.rs
@@ -2,25 +2,23 @@ use std::fmt;
pub struct DisplayBandwidth {
pub bandwidth: f64,
- pub as_rate: bool,
}
impl fmt::Display for DisplayBandwidth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- let suffix = if self.as_rate { "ps" } else { "" };
- if self.bandwidth > 999_999_999_999.0 {
- // 1024 * 1024 * 1024 * 1024
- write!(f, "{:.2}TiB{suffix}", self.bandwidth / 1_099_511_627_776.0,)
- } else if self.bandwidth > 999_999_999.0 {
- // 1024 * 1024 * 1024
- write!(f, "{:.2}GiB{suffix}", self.bandwidth / 1_073_741_824.0)
- } else if self.bandwidth > 999_999.0 {
- // 1024 * 1024
- write!(f, "{:.2}MiB{suffix}", self.bandwidth / 1_048_576.0)
- } else if self.bandwidth > 999.0 {
- write!(f, "{:.2}KiB{suffix}", self.bandwidth / 1024.0)
+ // see https://github.com/rust-lang/rust/issues/41620
+ let (div, suffix) = if self.bandwidth >= 1e12 {
+ (1_099_511_627_776.0, "TiB")
+ } else if self.bandwidth >= 1e9 {
+ (1_073_741_824.0, "GiB")
+ } else if self.bandwidth >= 1e6 {
+ (1_048_576.0, "MiB")
+ } else if self.bandwidth >= 1e3 {
+ (1024.0, "KiB")
} else {
- write!(f, "{}B{suffix}", self.bandwidth)
- }
+ (1.0, "B")
+ };
+
+ write!(f, "{:.2}{suffix}", self.bandwidth / div)
}
}