|
@@ -18,6 +18,7 @@ struct Stats {
|
|
|
pub struct NetSpeedSensor {
|
|
|
files: Vec<StatFiles>,
|
|
|
stats: Option<Stats>,
|
|
|
+ rate: Option<Stats>,
|
|
|
last_time: i64
|
|
|
}
|
|
|
|
|
@@ -30,13 +31,25 @@ impl NetSpeedSensor {
|
|
|
NetSpeedSensor {
|
|
|
files: files,
|
|
|
stats: None,
|
|
|
+ rate: None,
|
|
|
last_time: 0
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
impl Sensor for NetSpeedSensor {
|
|
|
- fn status(&mut self) -> String {
|
|
|
+ fn icon(&self) -> String {
|
|
|
+ "NET".to_string()
|
|
|
+ }
|
|
|
+
|
|
|
+ fn status(&self) -> String {
|
|
|
+ match self.rate.as_ref() {
|
|
|
+ Some(rate) => format!("{}↓ {}↑", format_bytes(rate.rx), format_bytes(rate.tx)),
|
|
|
+ None => "?".to_string()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fn process(&mut self) {
|
|
|
let curr_time = time::get_time().sec;
|
|
|
|
|
|
let stats = self.files
|
|
@@ -49,18 +62,17 @@ impl Sensor for NetSpeedSensor {
|
|
|
|
|
|
let diff_time = curr_time - self.last_time;
|
|
|
let output = match (self.stats.as_ref(), diff_time) {
|
|
|
- (_, 0) | (None, _) => "?".to_string(),
|
|
|
+ (_, 0) | (None, _) => None,
|
|
|
(Some(pstats), diff_time) => {
|
|
|
let rx = (stats.rx - pstats.rx) / diff_time;
|
|
|
let tx = (stats.tx - pstats.tx) / diff_time;
|
|
|
- format!("{}↓ {}↑", format_bytes(rx), format_bytes(tx))
|
|
|
+ Some(Stats { rx: rx, tx: tx })
|
|
|
}
|
|
|
};
|
|
|
|
|
|
self.last_time = curr_time;
|
|
|
self.stats = Some(stats);
|
|
|
-
|
|
|
- output
|
|
|
+ self.rate = output;
|
|
|
}
|
|
|
}
|
|
|
|