refactor: Cpu+Mem stats use tuple struct

This commit is contained in:
Jack Wills
2022-10-16 01:23:02 +00:00
parent 9cb0c414af
commit a060d03258
+19 -22
View File
@@ -107,6 +107,7 @@ impl<T> StatefulList<T> {
} }
} }
/// Return the current status of the select list, e.g. 2/5,
pub fn get_state_title(&self) -> String { pub fn get_state_title(&self) -> String {
if self.items.is_empty() { if self.items.is_empty() {
String::new() String::new()
@@ -254,13 +255,11 @@ pub trait Stats {
/// So can use custom display formatter /// So can use custom display formatter
/// Use trait Stats for use as generic in draw_chart function /// Use trait Stats for use as generic in draw_chart function
#[derive(Debug, Default, Clone, Copy)] #[derive(Debug, Default, Clone, Copy)]
pub struct CpuStats { pub struct CpuStats(f64);
value: f64,
}
impl CpuStats { impl CpuStats {
pub const fn new(value: f64) -> Self { pub const fn new(value: f64) -> Self {
Self { value } Self(value)
} }
} }
@@ -268,21 +267,21 @@ impl Eq for CpuStats {}
impl PartialEq for CpuStats { impl PartialEq for CpuStats {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.value == other.value self.0 == other.0
} }
} }
impl PartialOrd for CpuStats { impl PartialOrd for CpuStats {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.value.partial_cmp(&other.value) self.0.partial_cmp(&other.0)
} }
} }
impl Ord for CpuStats { impl Ord for CpuStats {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
if self.value > other.value { if self.0 > other.0 {
Ordering::Greater Ordering::Greater
} else if (self.value - other.value).abs() < 0.01 { } else if (self.0 - other.0).abs() < 0.01 {
Ordering::Equal Ordering::Equal
} else { } else {
Ordering::Less Ordering::Less
@@ -292,13 +291,13 @@ impl Ord for CpuStats {
impl Stats for CpuStats { impl Stats for CpuStats {
fn get_value(&self) -> f64 { fn get_value(&self) -> f64 {
self.value self.0
} }
} }
impl fmt::Display for CpuStats { impl fmt::Display for CpuStats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let disp = format!("{:05.2}%", self.value); let disp = format!("{:05.2}%", self.0);
write!(f, "{:>x$}", disp, x = f.width().unwrap_or(1)) write!(f, "{:>x$}", disp, x = f.width().unwrap_or(1))
} }
} }
@@ -307,41 +306,39 @@ impl fmt::Display for CpuStats {
/// So can use custom display formatter /// So can use custom display formatter
/// Use trait Stats for use as generic in draw_chart function /// Use trait Stats for use as generic in draw_chart function
#[derive(Debug, Default, Clone, Copy, Eq)] #[derive(Debug, Default, Clone, Copy, Eq)]
pub struct ByteStats { pub struct ByteStats(u64);
value: u64,
}
impl PartialEq for ByteStats { impl PartialEq for ByteStats {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.value == other.value self.0 == other.0
} }
} }
impl PartialOrd for ByteStats { impl PartialOrd for ByteStats {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.value.partial_cmp(&other.value) self.0.partial_cmp(&other.0)
} }
} }
impl Ord for ByteStats { impl Ord for ByteStats {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
self.value.cmp(&other.value) self.0.cmp(&other.0)
} }
} }
impl ByteStats { impl ByteStats {
pub const fn new(value: u64) -> Self { pub const fn new(value: u64) -> Self {
Self { value } Self(value)
} }
pub fn update(&mut self, value: u64) { pub fn update(&mut self, value: u64) {
self.value = value; self.0 = value;
} }
} }
#[allow(clippy::cast_precision_loss)] #[allow(clippy::cast_precision_loss)]
impl Stats for ByteStats { impl Stats for ByteStats {
fn get_value(&self) -> f64 { fn get_value(&self) -> f64 {
self.value as f64 self.0 as f64
} }
} }
@@ -353,7 +350,7 @@ impl fmt::Display for ByteStats {
x if x >= ONE_GB => format!("{y:.2} GB", y = as_f64 / ONE_GB), x if x >= ONE_GB => format!("{y:.2} GB", y = as_f64 / ONE_GB),
x if x >= ONE_MB => format!("{y:.2} MB", y = as_f64 / ONE_MB), x if x >= ONE_MB => format!("{y:.2} MB", y = as_f64 / ONE_MB),
x if x >= ONE_KB => format!("{y:.2} kB", y = as_f64 / ONE_KB), x if x >= ONE_KB => format!("{y:.2} kB", y = as_f64 / ONE_KB),
_ => format!("{} B", self.value), _ => format!("{} B", self.0),
}; };
write!(f, "{:>x$}", p, x = f.width().unwrap_or(1)) write!(f, "{:>x$}", p, x = f.width().unwrap_or(1))
} }
@@ -426,7 +423,7 @@ impl ContainerItem {
self.cpu_stats self.cpu_stats
.iter() .iter()
.enumerate() .enumerate()
.map(|i| (i.0 as f64, i.1.value as f64)) .map(|i| (i.0 as f64, i.1.0 as f64))
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }
@@ -436,7 +433,7 @@ impl ContainerItem {
self.mem_stats self.mem_stats
.iter() .iter()
.enumerate() .enumerate()
.map(|i| (i.0 as f64, i.1.value as f64)) .map(|i| (i.0 as f64, i.1.0 as f64))
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }