Skip to content

Commit

Permalink
fix(barchart): avoid divide by zero in rendering
Browse files Browse the repository at this point in the history
Fixes: #521
  • Loading branch information
joshka committed Sep 21, 2023
1 parent be55a5f commit 9362015
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/widgets/barchart.rs
Expand Up @@ -506,10 +506,15 @@ impl<'a> Widget for BarChart<'a> {
buf.set_style(area, self.style);

self.render_block(&mut area, buf);

if area.area() == 0 {
return;
}
if self.data.is_empty() {
return;
}
if self.bar_width == 0 {
return;

Check warning on line 516 in src/widgets/barchart.rs

View check run for this annotation

Codecov / codecov/patch

src/widgets/barchart.rs#L516

Added line #L516 was not covered by tests
}

let label_height = self.label_height();
if area.height <= label_height {
Expand Down Expand Up @@ -1180,4 +1185,16 @@ mod tests {
]);
assert_buffer_eq!(buffer, expected);
}

#[test]
fn handles_zero_width() {
// this test is to ensure that a BarChart with zero bar / gap width does not panic
let chart = BarChart::default()
.data(&[("A", 1)])
.bar_width(0)
.bar_gap(0);
let mut buffer = Buffer::empty(Rect::new(0, 0, 0, 10));
chart.render(buffer.area, &mut buffer);
assert_buffer_eq!(buffer, Buffer::empty(Rect::new(0, 0, 0, 10)));
}
}

0 comments on commit 9362015

Please sign in to comment.