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):
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):
"""
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 = " "
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