apply Federico "fox" Scrinzi progressbar additions of title and label display. Fix...
authorBrian Dolbec <dolsen@gentoo.org>
Mon, 9 Jul 2012 20:34:26 +0000 (13:34 -0700)
committerZac Medico <zmedico@gentoo.org>
Mon, 9 Jul 2012 20:45:52 +0000 (13:45 -0700)
pym/portage/output.py

index 98bec814317b275c0b83748bc413c8ae4a69d64f..fc6f6ebaaca88b2be26279733711a096ea84d84e 100644 (file)
@@ -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