From 62495c3bd1afb46969640fb6307085b33e4a85cb Mon Sep 17 00:00:00 2001 From: Kemyt <34860241+Kemyt@users.noreply.github.com> Date: Sun, 27 Sep 2020 17:15:44 +0200 Subject: [PATCH] fix(widgets/barchart): fix chart filled more than actual (#383) * Fix barchart incorrectly showing chart filled more than actual Determination of how filled the bar should be was incorrectly taking the entire chart height into account, when in fact it should take height-1, because of extra line with label. Because of that the chart appeared fuller than it should and was full before reaching maximum value. * Add a test case for checking if barchart fills correctly up to max value Co-authored-by: Kemyt --- src/widgets/barchart.rs | 2 +- tests/widgets_barchart.rs | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/widgets_barchart.rs diff --git a/src/widgets/barchart.rs b/src/widgets/barchart.rs index fee9bb43..dc57d1ad 100644 --- a/src/widgets/barchart.rs +++ b/src/widgets/barchart.rs @@ -157,7 +157,7 @@ impl<'a> Widget for BarChart<'a> { .map(|&(l, v)| { ( l, - v * u64::from(chart_area.height) * 8 / std::cmp::max(max, 1), + v * u64::from(chart_area.height - 1) * 8 / std::cmp::max(max, 1), ) }) .collect::>(); diff --git a/tests/widgets_barchart.rs b/tests/widgets_barchart.rs new file mode 100644 index 00000000..a7efaf68 --- /dev/null +++ b/tests/widgets_barchart.rs @@ -0,0 +1,40 @@ +use tui::backend::TestBackend; +use tui::buffer::Buffer; +use tui::widgets::{BarChart, Block, Borders}; +use tui::Terminal; + +#[test] +fn widgets_barchart_not_full_below_max_value() { + let test_case = |expected| { + let backend = TestBackend::new(30, 10); + let mut terminal = Terminal::new(backend).unwrap(); + + terminal + .draw(|f| { + let size = f.size(); + let barchart = BarChart::default() + .block(Block::default().borders(Borders::ALL)) + .data(&[("empty", 0), ("half", 50), ("almost", 99), ("full", 100)]) + .max(100) + .bar_width(7) + .bar_gap(0); + f.render_widget(barchart, size); + }) + .unwrap(); + terminal.backend().assert_buffer(&expected); + }; + + // check that bars fill up correctly up to max value + test_case(Buffer::with_lines(vec![ + "┌────────────────────────────┐", + "│ ▇▇▇▇▇▇▇███████│", + "│ ██████████████│", + "│ ██████████████│", + "│ ▄▄▄▄▄▄▄██████████████│", + "│ █████████████████████│", + "│ █████████████████████│", + "│ ██50█████99█████100██│", + "│empty half almost full │", + "└────────────────────────────┘", + ])); +}