diff --git a/README.md b/README.md index 21b8220..3793bc2 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # statusbar +Python package for displaying status information in command line interfaces. + [![Build status](https://img.shields.io/travis/mailund/progressbar.svg)](https://travis-ci.org/mailund/statusbar) [![Coverage Status](https://img.shields.io/coveralls/mailund/statusbar.svg)](https://coveralls.io/github/mailund/statusbar) [![License](https://img.shields.io/badge/license-GPL%20%28%3E=%203%29-brightgreen.svg?style=flat)](http://www.gnu.org/licenses/gpl-3.0.html) -Python package for displaying status information in command line interfaces. This package can be used to show status information through one or more lines of formatted status bars where each status bar consist of three components: a label, a progress bar, and a statistics/summary field. diff --git a/statusbar/__init__.py b/statusbar/__init__.py index 4a183d6..5cb1828 100644 --- a/statusbar/__init__.py +++ b/statusbar/__init__.py @@ -34,7 +34,11 @@ def __init__(self, count, symbol, fg, bg, style): self.style_reset = colorama.Style.RESET_ALL def format_chunk(self, width): - pass + return "{format_start}{bar}{format_end}".format( + format_start=self.style + self.bg + self.fg, + bar=self.symbol * width, + format_end=self.fg_reset + self.bg_reset + self.style_reset + ) class ProgressBar: @@ -74,4 +78,10 @@ def _get_chunk_sizes(self, width): def format_progress(self, width): """Format the progress bar to fit into "width" characters.""" - pass + # Because we use two characters for brackets the width we have + # for the chunks is width - 2. + chunk_widths = self._get_chunk_sizes(width - 2) + progress_chunks = [chunk.format_chunk(chunk_width) + for (chunk, chunk_width) + in zip(self._progress_chunks, chunk_widths)] + return "[{}]".format("".join(progress_chunks)) diff --git a/tests/__init__.py b/tests/__init__.py index 3a4b13a..fa20bd5 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,5 @@ import unittest +import colorama import statusbar @@ -40,3 +41,33 @@ def test_chunk_widths(self): pb.add_progress(3, '#') breakpoints = pb._get_chunk_sizes(4) self.assertListEqual(breakpoints, [1, 3]) + + def test_progress_formatting(self): + pb = statusbar.ProgressBar() + pb.add_progress(1, '.') + pb.add_progress(1, '#') + progress = pb.format_progress(4) + self.assertEqual(progress, "[.#]") + + pb = statusbar.ProgressBar() + pb.add_progress(1, '.') + pb.add_progress(2, '#') + progress = pb.format_progress(5) + self.assertEqual(progress, "[.##]") + + # Adding a forground colour makes each segment ten characters + # longer; five characters are used for setting the color and another + # five for resetting it again. These are not shown, so the width + # doesn't take this into account. + pb = statusbar.ProgressBar() + pb.add_progress(1, '.', fg=colorama.Fore.GREEN) + pb.add_progress(2, '#', fg=colorama.Fore.RED) + progress = pb.format_progress(5) + self.assertEqual(progress[0], "[") + self.assertEqual(progress[1:6], colorama.Fore.GREEN) + self.assertEqual(progress[6], ".") + self.assertEqual(progress[7:12], colorama.Fore.RESET) + self.assertEqual(progress[12:17], colorama.Fore.RED) + self.assertEqual(progress[17:19], "##") + self.assertEqual(progress[19:24], colorama.Fore.RESET) + self.assertEqual(progress[24], "]")