From 2c159591fff2451bcb109f9731a89c41ba12c42a Mon Sep 17 00:00:00 2001 From: Cliff Wells Date: Fri, 12 Jan 2018 16:08:06 -0800 Subject: [PATCH 1/4] Add TextBar --- progressbar/widgets.py | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/progressbar/widgets.py b/progressbar/widgets.py index d14a383..88f85c2 100644 --- a/progressbar/widgets.py +++ b/progressbar/widgets.py @@ -354,3 +354,68 @@ def update(self, pbar, width): if not self.fill_left: rpad, lpad = lpad, rpad return '%s%s%s%s%s' % (left, lpad, marker, rpad, right) + + +class TextBar(WidgetHFill): + """A progress bar which displays specified text in the background.""" + + TIME_SENSITIVE = True + + __slots__ = ( + 'text', 'fill', 'left', 'right', 'align' + 'show_percentage', 'show_count', 'normal', 'reverse' + ) + + def __init__(self, text, left='|', right='|', fill=' ', align='left', + show_percentage=True, show_count=True, + normal="\033[0;0m", reverse="\033[;7m"): + """Creates a customizable progress bar. + text - string to display in Bar. + left - string or updatable object to use as a left border. + right - string or updatable object to use as a right border. + align - one of 'left', 'right', 'center'. + fill - character to pad with. + show_percentage - whether to display percentage indicator. + show_count - whether to show "x/y" progress indicator. + reverse - ANSI color to use for complete. + normal - ANSI color to use for remainder. + """ + self.text = text + self.left = left + self.right = right + self.fill = fill + self.show_percentage = show_percentage + self.show_count = show_count + self.align = { + 'left': 'ljust', + 'right': 'rjust', + 'center': 'center' + }.get(align, 'ljust') + self.normal = normal + self.reverse = reverse + + def update(self, pbar, width): + """Updates the progress bar and its subcomponents.""" + + left, marked, right = ( + format_updatable(i, pbar) + for i in (self.left, self.text, self.right) + ) + + width -= len(left) + len(right) + percentage = pbar.currval / pbar.maxval + position = int(percentage * width) + + items = [''] + if self.show_count: + items.append("%03d/%03d" % (pbar.currval, pbar.maxval)) + if self.show_percentage: + items.append("% 3.0f%%" % (percentage * 100)) + items.append(self.text) + + text = getattr(' '.join(items), self.align)(width, self.fill) + marked = text[:position] if pbar.maxval else '' + unmarked = text[position:width] + bar = self.reverse + marked + self.normal + unmarked + + return '%s%s%s' % (left, bar, right) From 5b11ed0edb955df5b12dd3b6831119aa9d565443 Mon Sep 17 00:00:00 2001 From: Cliff Wells Date: Fri, 12 Jan 2018 18:31:58 -0800 Subject: [PATCH 2/4] better names, fix formatting of count --- progressbar/widgets.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/progressbar/widgets.py b/progressbar/widgets.py index 88f85c2..1b25bc0 100644 --- a/progressbar/widgets.py +++ b/progressbar/widgets.py @@ -356,19 +356,19 @@ def update(self, pbar, width): return '%s%s%s%s%s' % (left, lpad, marker, rpad, right) -class TextBar(WidgetHFill): +class LabeledBar(WidgetHFill): """A progress bar which displays specified text in the background.""" TIME_SENSITIVE = True __slots__ = ( 'text', 'fill', 'left', 'right', 'align' - 'show_percentage', 'show_count', 'normal', 'reverse' + 'show_percentage', 'show_count', 'done_color', 'todo_color' ) def __init__(self, text, left='|', right='|', fill=' ', align='left', show_percentage=True, show_count=True, - normal="\033[0;0m", reverse="\033[;7m"): + todo_color="\033[0;0m", done_color="\033[;7m"): """Creates a customizable progress bar. text - string to display in Bar. left - string or updatable object to use as a left border. @@ -391,8 +391,8 @@ def __init__(self, text, left='|', right='|', fill=' ', align='left', 'right': 'rjust', 'center': 'center' }.get(align, 'ljust') - self.normal = normal - self.reverse = reverse + self.todo_color = todo_color + self.done_color = done_color def update(self, pbar, width): """Updates the progress bar and its subcomponents.""" @@ -416,6 +416,6 @@ def update(self, pbar, width): text = getattr(' '.join(items), self.align)(width, self.fill) marked = text[:position] if pbar.maxval else '' unmarked = text[position:width] - bar = self.reverse + marked + self.normal + unmarked + bar = self.done_color + marked + self.todo_color + unmarked return '%s%s%s' % (left, bar, right) From edd5394f08df0806c9deeb77925c2246d27408d7 Mon Sep 17 00:00:00 2001 From: Cliff Wells Date: Fri, 12 Jan 2018 23:12:48 -0800 Subject: [PATCH 3/4] fix docstring --- progressbar/widgets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/progressbar/widgets.py b/progressbar/widgets.py index 1b25bc0..6aa7852 100644 --- a/progressbar/widgets.py +++ b/progressbar/widgets.py @@ -377,8 +377,8 @@ def __init__(self, text, left='|', right='|', fill=' ', align='left', fill - character to pad with. show_percentage - whether to display percentage indicator. show_count - whether to show "x/y" progress indicator. - reverse - ANSI color to use for complete. - normal - ANSI color to use for remainder. + done_color - ANSI color to use for complete. + todo_color - ANSI color to use for remainder. """ self.text = text self.left = left From 7a6a06fefb38970d065626ab3929c5bc49eb8f06 Mon Sep 17 00:00:00 2001 From: Cliff Wells Date: Fri, 12 Jan 2018 23:14:25 -0800 Subject: [PATCH 4/4] meters off by default --- progressbar/widgets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/progressbar/widgets.py b/progressbar/widgets.py index 6aa7852..63d26a4 100644 --- a/progressbar/widgets.py +++ b/progressbar/widgets.py @@ -367,7 +367,7 @@ class LabeledBar(WidgetHFill): ) def __init__(self, text, left='|', right='|', fill=' ', align='left', - show_percentage=True, show_count=True, + show_percentage=False, show_count=False, todo_color="\033[0;0m", done_color="\033[;7m"): """Creates a customizable progress bar. text - string to display in Bar.