Skip to content

Commit

Permalink
fix(barchart): avoid divide by zero in rendering (#525)
Browse files Browse the repository at this point in the history
Fixes: #521
  • Loading branch information
joshka authored Sep 21, 2023
1 parent be55a5f commit c5ea656
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/widgets/barchart.rs
Original file line number Diff line number Diff line change
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;
}

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 c5ea656

Please sign in to comment.