This class defines methods to update and draw a progress bar
| 15 | from lib.core.data import kb |
| 16 | |
| 17 | class ProgressBar(object): |
| 18 | """ |
| 19 | This class defines methods to update and draw a progress bar |
| 20 | """ |
| 21 | |
| 22 | def __init__(self, minValue=0, maxValue=10, totalWidth=None): |
| 23 | self._progBar = "[]" |
| 24 | self._min = int(minValue) |
| 25 | self._max = int(maxValue) |
| 26 | self._span = max(self._max - self._min, 0.001) |
| 27 | self._width = totalWidth if totalWidth else conf.progressWidth |
| 28 | self._amount = 0 |
| 29 | self._start = None |
| 30 | self.update() |
| 31 | |
| 32 | def _convertSeconds(self, value): |
| 33 | seconds = value |
| 34 | minutes = seconds // 60 |
| 35 | seconds = seconds - (minutes * 60) |
| 36 | |
| 37 | return "%.2d:%.2d" % (minutes, seconds) |
| 38 | |
| 39 | def update(self, newAmount=0): |
| 40 | """ |
| 41 | This method updates the progress bar |
| 42 | """ |
| 43 | |
| 44 | if newAmount < self._min: |
| 45 | newAmount = self._min |
| 46 | elif newAmount > self._max: |
| 47 | newAmount = self._max |
| 48 | |
| 49 | self._amount = newAmount |
| 50 | |
| 51 | # Figure out the new percent done, round to an integer |
| 52 | diffFromMin = float(self._amount - self._min) |
| 53 | percentDone = (diffFromMin / float(self._span)) * 100.0 |
| 54 | percentDone = round(percentDone) |
| 55 | percentDone = min(100, int(percentDone)) |
| 56 | |
| 57 | # Figure out how many hash bars the percentage should be |
| 58 | allFull = self._width - len("100%% [] %s/%s (ETA 00:00)" % (self._max, self._max)) |
| 59 | numHashes = (percentDone / 100.0) * allFull |
| 60 | numHashes = int(round(numHashes)) |
| 61 | |
| 62 | # Build a progress bar with an arrow of equal signs |
| 63 | if numHashes == 0: |
| 64 | self._progBar = "[>%s]" % (" " * (allFull - 1)) |
| 65 | elif numHashes == allFull: |
| 66 | self._progBar = "[%s]" % ("=" * allFull) |
| 67 | else: |
| 68 | self._progBar = "[%s>%s]" % ("=" * (numHashes - 1), " " * (allFull - numHashes)) |
| 69 | |
| 70 | # Add the percentage at the beginning of the progress bar |
| 71 | percentString = getUnicode(percentDone) + "%" |
| 72 | self._progBar = "%s %s" % (percentString, self._progBar) |
| 73 | |
| 74 | def progress(self, newAmount): |