From 1ca9cc0915113c5b6b8d3c69ae3327dcfb2be1ba Mon Sep 17 00:00:00 2001 From: rajvira10 Date: Fri, 16 Oct 2026 23:10:38 +0600 Subject: [PATCH] Prevent duplicate bars when redrawing vertical charts --- termgraph/chart.py | 4 ++++ tests/test_chart_redraw.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/test_chart_redraw.py diff --git a/termgraph/chart.py b/termgraph/chart.py index 7622b44..5e4c8be 100644 --- a/termgraph/chart.py +++ b/termgraph/chart.py @@ -398,6 +398,10 @@ def _prepare_vertical(self, value: float, num_blocks: int) -> None: def draw(self) -> None: """Draws the vertical chart""" + self.value_list.clear() + self.zipped_list.clear() + self.vertical_list.clear() + self.maxi = 0 self._print_header() colors = self.args.get_arg("colors") diff --git a/tests/test_chart_redraw.py b/tests/test_chart_redraw.py new file mode 100644 index 0000000..0b2371a --- /dev/null +++ b/tests/test_chart_redraw.py @@ -0,0 +1,20 @@ +import pytest + +from termgraph.args import Args +from termgraph.chart import BarChart, StackedChart, VerticalChart +from termgraph.data import Data + + +@pytest.mark.parametrize("chart_type", [BarChart, StackedChart, VerticalChart]) +@pytest.mark.parametrize("options", [{}, {"no_values": True}, {"no_labels": True}]) +def test_drawing_chart_again_preserves_output(capsys, chart_type, options): + data = Data([[3], [5]], ["A", "B"]) + chart = chart_type(data, Args(width=5, **options)) + + chart.draw() + expected = capsys.readouterr().out + assert "▇" in expected + + for _ in range(2): + chart.draw() + assert capsys.readouterr().out == expected