From 83b7af059a9b02c868238e829be2f0d18b631ded Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Mon, 9 Jul 2012 13:34:26 -0700 Subject: apply Federico "fox" Scrinzi progressbar additions of title and label display. Fix a couple bugs and add max_desc_length param. --- pym/portage/output.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/pym/portage/output.py b/pym/portage/output.py index 98bec8143..fc6f6ebaa 100644 --- a/pym/portage/output.py +++ b/pym/portage/output.py @@ -631,11 +631,14 @@ class EOutput(object): class ProgressBar(object): """The interface is copied from the ProgressBar class from the EasyDialogs module (which is Mac only).""" - def __init__(self, title=None, maxval=0, label=None): - self._title = title + def __init__(self, title=None, maxval=0, label=None, max_desc_length=25): + self._title = title or "" self._maxval = maxval - self._label = maxval + self._label = label or "" self._curval = 0 + self._desc = "" + self._desc_max_length = max_desc_length + self._set_desc() @property def curval(self): @@ -659,10 +662,23 @@ class ProgressBar(object): def title(self, newstr): """Sets the text in the title bar of the progress dialog to newstr.""" self._title = newstr + self._set_desc() def label(self, newstr): """Sets the text in the progress box of the progress dialog to newstr.""" self._label = newstr + self._set_desc() + + def _set_desc(self): + self._desc = "%s%s" % ( + "%s: " % self._title if self._title else "", + "%s" % self._label if self._label else "" + ) + if len(self._desc) > self._desc_max_length: # truncate if too long + self._desc = "%s..." % self._desc[:self._desc_max_length - 3] + if len(self._desc): + self._desc = self._desc.ljust(self._desc_max_length) + def set(self, value, maxval=None): """ @@ -722,6 +738,8 @@ class TermProgressBar(ProgressBar): if cols < percentage_str_width: return "" bar_space = cols - percentage_str_width - square_brackets_width + if self._desc: + bar_space -= self._desc_max_length + 1 if maxval == 0: max_bar_width = bar_space-3 image = " " @@ -751,7 +769,11 @@ class TermProgressBar(ProgressBar): percentage_str_width += 1 bar_space -= 1 max_bar_width = bar_space - 1 - image = ("%d%% " % percentage).rjust(percentage_str_width) + image = "%s%s" % ( + self._desc, + ("%d%%" % percentage).ljust(percentage_str_width), + ) + if cols < min_columns: return image offset = float(curval) / maxval -- cgit v1.2.3-1-g7c22