summaryrefslogtreecommitdiffstats
path: root/src/display/components/display_bandwidth.rs
blob: dcc204c2feec3a4cda1c94eeaa6276787638f9fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.0 {
            write!(f, "{:.2}GB{}", self.bandwidth / 1_000_000_000.0, suffix)
        } else if self.bandwidth > 999_999.0 {
            write!(f, "{:.2}MB{}", self.bandwidth / 1_000_000.0, suffix)
        } else if self.bandwidth > 999.0 {
            write!(f, "{:.2}KB{}", self.bandwidth / 1000.0, suffix)
        } else {
            write!(f, "{}B{}", self.bandwidth, suffix)
        }
    }
}