mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-10 15:14:27 +00:00
Fix sparkline panic when max is zero
This commit is contained in:
parent
584f7688f4
commit
7a5ad3fbdb
1 changed files with 28 additions and 1 deletions
|
@ -89,7 +89,13 @@ impl<'a> Widget for Sparkline<'a> {
|
|||
.data
|
||||
.iter()
|
||||
.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>>();
|
||||
for j in (0..spark_area.height).rev() {
|
||||
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 a new issue