Address many clippy lints.

This commit is contained in:
Bill Thiede 2021-12-12 10:00:09 -08:00
parent d2477050b5
commit 9c94122d1d
5 changed files with 40 additions and 43 deletions

View File

@ -22,7 +22,7 @@ fn main() {
let opts = Opt::from_args(); let opts = Opt::from_args();
bar.push(PowerSupply::default()); bar.push(PowerSupply::default());
bar.push(CpuWidget::new()); bar.push(CpuWidget::default());
// Realtime upload/download rate for a interface // Realtime upload/download rate for a interface
bar.push(NetworkSpeedWidget::new(&opts.nic, 6)); bar.push(NetworkSpeedWidget::new(&opts.nic, 6));

View File

@ -22,7 +22,7 @@ fn main() {
let opts = Opt::from_args(); let opts = Opt::from_args();
bar.push(PowerSupply::default()); bar.push(PowerSupply::default());
bar.push(CpuWidget::new()); bar.push(CpuWidget::default());
// Realtime upload/download rate for a interface // Realtime upload/download rate for a interface
bar.push(NetworkSpeedWidget::new(&opts.nic, 6)); bar.push(NetworkSpeedWidget::new(&opts.nic, 6));

View File

@ -42,13 +42,13 @@ impl CpuWidget {
let tokens: Vec<_> = line.trim().split(|c| c == ' ' || c == '\t').collect(); let tokens: Vec<_> = line.trim().split(|c| c == ' ' || c == '\t').collect();
// The sum total CPU entry has a space instead of a number, so we skip the line when // The sum total CPU entry has a space instead of a number, so we skip the line when
// there's an extra empty column. // there's an extra empty column.
if tokens[1] == "" { if tokens[1].is_empty() {
continue; continue;
} }
if tokens[0].starts_with("cpu") { if tokens[0].starts_with("cpu") {
let parsed = tokens[1..5] let parsed = tokens[1..5]
.iter() .iter()
.map(|x| u64::from_str_radix(x, 10).unwrap()) .map(|x| x.parse::<u64>().unwrap())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// [0] user // [0] user
// [1] nice // [1] nice
@ -62,20 +62,6 @@ impl CpuWidget {
Ok(stats) Ok(stats)
} }
pub fn new() -> Self {
let last_stats = Self::read_status().unwrap();
let num_samples = 1;
let history = (0..last_stats.len())
.map(|_| (0..num_samples).map(|_| 0.).collect::<VecDeque<f32>>())
.collect::<Vec<VecDeque<f32>>>();
let ret = Self {
last_stats,
history,
};
return ret;
}
fn draw_history(&mut self) -> Option<String> { fn draw_history(&mut self) -> Option<String> {
let stats = Self::read_status().ok()?; let stats = Self::read_status().ok()?;
let percentages: Vec<_> = stats let percentages: Vec<_> = stats
@ -101,7 +87,7 @@ impl CpuWidget {
.history .history
.iter() .iter()
.map(|history| { .map(|history| {
let sg = g.render(&history.iter().map(|v| *v).collect::<Vec<f32>>()); let sg = g.render(&history.iter().copied().collect::<Vec<f32>>());
history history
.iter() .iter()
.zip(sg.chars()) .zip(sg.chars())
@ -121,6 +107,20 @@ impl CpuWidget {
} }
} }
impl Default for CpuWidget {
fn default() -> Self {
let last_stats = Self::read_status().unwrap();
let num_samples = 1;
let history = (0..last_stats.len())
.map(|_| (0..num_samples).map(|_| 0.).collect::<VecDeque<f32>>())
.collect::<Vec<VecDeque<f32>>>();
Self {
last_stats,
history,
}
}
}
impl Widget for CpuWidget { impl Widget for CpuWidget {
fn update(&mut self) -> Option<WidgetUpdate> { fn update(&mut self) -> Option<WidgetUpdate> {
if let Some(history) = self.draw_history() { if let Some(history) = self.draw_history() {

View File

@ -9,8 +9,8 @@ use std::time::SystemTime;
use crate::spark; use crate::spark;
const NETWORK_PATH_PREFIX: &'static str = "/sys/class/net"; const NETWORK_PATH_PREFIX: &str = "/sys/class/net";
const NETWORK_STAT_SUFFIX: &'static str = "statistics/dummy"; const NETWORK_STAT_SUFFIX: &str = "statistics/dummy";
struct TransferStat { struct TransferStat {
rx: u64, rx: u64,
@ -35,20 +35,19 @@ impl TransferStat {
return line; return line;
} }
return Err(Error::new(ErrorKind::Other, "Empty file")); Err(Error::new(ErrorKind::Other, "Empty file"))
}; };
let rx = u64::from_str_radix(&(read_stat_file("rx_bytes")?), 10).unwrap(); let rx = (read_stat_file("rx_bytes")?).parse().unwrap();
let tx = u64::from_str_radix(&(read_stat_file("tx_bytes")?), 10).unwrap(); let tx = (read_stat_file("tx_bytes")?).parse().unwrap();
let ts = SystemTime::now(); let ts = SystemTime::now();
return Ok(Self { rx, tx, ts }); Ok(Self { rx, tx, ts })
} }
fn duration(&self, earlier: &Self) -> f64 { fn duration(&self, earlier: &Self) -> f64 {
let duration = self.ts.duration_since(earlier.ts).unwrap(); let duration = self.ts.duration_since(earlier.ts).unwrap();
let secs = duration.as_secs() as f64 + duration.subsec_nanos() as f64 / 1_000_000_000.0; duration.as_secs() as f64 + duration.subsec_nanos() as f64 / 1_000_000_000.0
return secs;
} }
fn rx_rate(&self, earlier: &Self) -> f64 { fn rx_rate(&self, earlier: &Self) -> f64 {
@ -57,7 +56,7 @@ impl TransferStat {
return std::f64::NAN; return std::f64::NAN;
} }
return (self.rx - earlier.rx) as f64 / duration; (self.rx - earlier.rx) as f64 / duration
} }
fn tx_rate(&self, earlier: &Self) -> f64 { fn tx_rate(&self, earlier: &Self) -> f64 {
@ -66,7 +65,7 @@ impl TransferStat {
return std::f64::NAN; return std::f64::NAN;
} }
return (self.tx - earlier.tx) as f64 / duration; (self.tx - earlier.tx) as f64 / duration
} }
} }
@ -100,7 +99,7 @@ impl NetworkSpeedWidget {
return "N/A".to_string(); return "N/A".to_string();
} }
const UNIT_NAME: [&'static str; 6] = [" B/s", "KB/s", "MB/s", "GB/s", "TB/s", "PB/s"]; const UNIT_NAME: [&str; 6] = [" B/s", "KB/s", "MB/s", "GB/s", "TB/s", "PB/s"];
let mut best_unit = UNIT_NAME[0]; let mut best_unit = UNIT_NAME[0];
let mut best_multiplier = 1.0; let mut best_multiplier = 1.0;
@ -113,9 +112,7 @@ impl NetworkSpeedWidget {
best_multiplier *= 1024.0 best_multiplier *= 1024.0
} }
let ret = format!("{:6.1}{}", rate / best_multiplier, best_unit); format!("{:6.1}{}", rate / best_multiplier, best_unit)
return ret;
} }
fn get_human_readable_stat(&mut self) -> Result<(String, String)> { fn get_human_readable_stat(&mut self) -> Result<(String, String)> {
@ -130,7 +127,7 @@ impl NetworkSpeedWidget {
self.tx_history.pop_front(); self.tx_history.pop_front();
self.last_stat = cur_stat; self.last_stat = cur_stat;
return Ok((Self::format_rate(rx_rate), Self::format_rate(tx_rate))); Ok((Self::format_rate(rx_rate), Self::format_rate(tx_rate)))
} }
} }

View File

@ -19,7 +19,7 @@ use thiserror::Error;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct PowerSupply {} pub struct PowerSupply {}
const POWER_SUPPLY_GLOB: &'static str = "/sys/class/power_supply/*/uevent"; const POWER_SUPPLY_GLOB: &str = "/sys/class/power_supply/*/uevent";
#[derive(Error, Debug)] #[derive(Error, Debug)]
enum PowerSupplyError { enum PowerSupplyError {
@ -39,11 +39,11 @@ enum PowerSupplyError {
impl PowerSupply { impl PowerSupply {
fn get_update(&mut self) -> Result<Vec<String>, PowerSupplyError> { fn get_update(&mut self) -> Result<Vec<String>, PowerSupplyError> {
const BATT_100: &'static str = "&#xf240;"; const BATT_100: &str = "&#xf240;";
const BATT_75: &'static str = "&#xf241;"; const BATT_75: &str = "&#xf241;";
const BATT_50: &'static str = "&#xf242;"; const BATT_50: &str = "&#xf242;";
const BATT_25: &'static str = "&#xf243;"; const BATT_25: &str = "&#xf243;";
const BATT_0: &'static str = "&#xf244;"; const BATT_0: &str = "&#xf244;";
let mut res = Vec::new(); let mut res = Vec::new();
for ps in glob(POWER_SUPPLY_GLOB)? { for ps in glob(POWER_SUPPLY_GLOB)? {
@ -51,9 +51,9 @@ impl PowerSupply {
let values: HashMap<&str, &str> = std::str::from_utf8(&bytes)? let values: HashMap<&str, &str> = std::str::from_utf8(&bytes)?
.lines() .lines()
.map(|l| { .map(|l| {
l.split_once('=').ok_or(PowerSupplyError::ParseError( l.split_once('=').ok_or_else(|| {
"missing = in uevent line".to_string(), PowerSupplyError::ParseError("missing = in uevent line".to_string())
)) })
}) })
.collect::<Result<_, _>>()?; .collect::<Result<_, _>>()?;
let cap: u32 = values let cap: u32 = values