Skip to content

Commit

Permalink
fix(barchart): empty groups causes panic (#333)
Browse files Browse the repository at this point in the history
This unlikely to happen, since nobody wants to add an empty group.
Even we fix the panic, things will not render correctly.
So it is better to just not add them to the BarChart.

Signed-off-by: Ben Fekih, Hichem <hichem.f@live.de>
  • Loading branch information
karthago1 committed Jul 19, 2023
1 parent 13fb11a commit 9c95673
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/widgets/barchart/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ impl<'a> BarChart<'a> {
/// .data(BarGroup::default().bars(&[Bar::default().value(10), Bar::default().value(20)]));
/// ```
pub fn data(mut self, data: impl Into<BarGroup<'a>>) -> BarChart<'a> {
self.data.push(data.into());
let group: BarGroup = data.into();
if !group.bars.is_empty() {
self.data.push(group);
}
self
}

Expand Down Expand Up @@ -737,4 +740,20 @@ mod tests {
.add_modifier(Modifier::BOLD)
)
}

#[test]
fn test_empty_group() {
let chart = BarChart::default()
.data(BarGroup::default().label("invisible".into()))
.data(
BarGroup::default()
.label("G".into())
.bars(&[Bar::default().value(1), Bar::default().value(2)]),
);

let mut buffer = Buffer::empty(Rect::new(0, 0, 3, 3));
chart.render(buffer.area, &mut buffer);
let expected = Buffer::with_lines(vec![" █", "█ █", " G "]);
assert_buffer_eq!(buffer, expected);
}
}

0 comments on commit 9c95673

Please sign in to comment.