Skip to content

Commit

Permalink
Formatting progress bars.
Browse files Browse the repository at this point in the history
  • Loading branch information
mailund committed Sep 28, 2016
1 parent 2154a70 commit 3b45e68
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
3 changes: 2 additions & 1 deletion 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.
14 changes: 12 additions & 2 deletions statusbar/__init__.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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))
31 changes: 31 additions & 0 deletions tests/__init__.py
@@ -1,4 +1,5 @@
import unittest
import colorama
import statusbar


Expand Down Expand Up @@ -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], "]")

0 comments on commit 3b45e68

Please sign in to comment.