Draw historic CPU usage.
Version spark library and extend.
This commit is contained in:
65
src/spark.rs
65
src/spark.rs
@@ -12,33 +12,40 @@ use std::f32::MAX as f32_max;
|
||||
/// let sparkline = spark::graph(&[1.0, 5.0, 22.0, 13.0, 53.0]);
|
||||
/// assert_eq!(sparkline, "▁▁▃▂█");
|
||||
/// ```
|
||||
pub fn graph(values: &[f32]) -> String {
|
||||
let ticks = "▁▂▃▄▅▆▇█";
|
||||
|
||||
/* XXX: This doesn't feel like idiomatic Rust */
|
||||
let mut min: f32 = f32_max;
|
||||
let mut max: f32 = 0.0;
|
||||
|
||||
for &i in values.iter() {
|
||||
if i > max {
|
||||
max = i;
|
||||
}
|
||||
if i < min {
|
||||
min = i;
|
||||
}
|
||||
}
|
||||
|
||||
let ratio = if max == min {
|
||||
1.0
|
||||
} else {
|
||||
(ticks.chars().count() - 1) as f32 / (max - min)
|
||||
};
|
||||
|
||||
values
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|n| (n - min) * ratio)
|
||||
.map(|n| n.floor() as usize)
|
||||
.filter_map(|n| ticks.chars().nth(n))
|
||||
.collect()
|
||||
pub struct Graph {
|
||||
pub min: Option<f32>,
|
||||
pub max: Option<f32>,
|
||||
}
|
||||
|
||||
impl Graph {
|
||||
pub fn render(&self, values: &[f32]) -> String {
|
||||
let ticks = "▁▂▃▄▅▆▇█";
|
||||
|
||||
/* XXX: This doesn't feel like idiomatic Rust */
|
||||
let mut min: f32 = self.min.unwrap_or(f32_max);
|
||||
let mut max: f32 = self.max.unwrap_or(0.);
|
||||
|
||||
for &i in values.iter() {
|
||||
if i > max {
|
||||
max = i;
|
||||
}
|
||||
if i < min {
|
||||
min = i;
|
||||
}
|
||||
}
|
||||
|
||||
let ratio = if max == min {
|
||||
1.0
|
||||
} else {
|
||||
(ticks.chars().count() - 1) as f32 / (max - min)
|
||||
};
|
||||
|
||||
values
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|n| (n - min) * ratio)
|
||||
.map(|n| n.floor() as usize)
|
||||
.filter_map(|n| ticks.chars().nth(n))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user