mirror of
https://github.com/fdehau/tui-rs.git
synced 2024-11-11 01:10:24 +00:00
Fix sparkline panic when max is zero
This commit is contained in:
parent
584f7688f4
commit
7a5ad3fbdb
@ -89,7 +89,13 @@ impl<'a> Widget for Sparkline<'a> {
|
|||||||
.data
|
.data
|
||||||
.iter()
|
.iter()
|
||||||
.take(max_index)
|
.take(max_index)
|
||||||
.map(|e| e * u64::from(spark_area.height) * 8 / max)
|
.map(|e| {
|
||||||
|
if max != 0 {
|
||||||
|
e * u64::from(spark_area.height) * 8 / max
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
})
|
||||||
.collect::<Vec<u64>>();
|
.collect::<Vec<u64>>();
|
||||||
for j in (0..spark_area.height).rev() {
|
for j in (0..spark_area.height).rev() {
|
||||||
for (i, d) in data.iter_mut().enumerate() {
|
for (i, d) in data.iter_mut().enumerate() {
|
||||||
@ -118,3 +124,24 @@ impl<'a> Widget for Sparkline<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_does_not_panic_if_max_is_zero() {
|
||||||
|
let mut widget = Sparkline::default().data(&[0, 0, 0]);
|
||||||
|
let area = Rect::new(0, 0, 3, 1);
|
||||||
|
let mut buffer = Buffer::empty(area);
|
||||||
|
widget.draw(area, &mut buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_does_not_panic_if_max_is_set_to_zero() {
|
||||||
|
let mut widget = Sparkline::default().data(&[0, 1, 2]).max(0);
|
||||||
|
let area = Rect::new(0, 0, 3, 1);
|
||||||
|
let mut buffer = Buffer::empty(area);
|
||||||
|
widget.draw(area, &mut buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user