Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | |
michael@0 | 5 | # Text progress bar library, like curl or scp. |
michael@0 | 6 | |
michael@0 | 7 | import sys, datetime |
michael@0 | 8 | |
michael@0 | 9 | class ProgressBar(object): |
michael@0 | 10 | def __init__(self, label, limit, label_width=12): |
michael@0 | 11 | self.label = label |
michael@0 | 12 | self.limit = limit |
michael@0 | 13 | self.label_width = label_width |
michael@0 | 14 | self.cur = 0 |
michael@0 | 15 | self.t0 = datetime.datetime.now() |
michael@0 | 16 | self.fullwidth = None |
michael@0 | 17 | |
michael@0 | 18 | self.barlen = 64 - self.label_width |
michael@0 | 19 | self.fmt = '\r%-' + str(label_width) + 's %3d%% %-' + str(self.barlen) + 's| %6.1fs' |
michael@0 | 20 | |
michael@0 | 21 | def update(self, value): |
michael@0 | 22 | self.cur = value |
michael@0 | 23 | pct = int(100.0 * self.cur / self.limit) |
michael@0 | 24 | barlen = int(1.0 * self.barlen * self.cur / self.limit) - 1 |
michael@0 | 25 | bar = '='*barlen + '>' |
michael@0 | 26 | dt = datetime.datetime.now() - self.t0 |
michael@0 | 27 | dt = dt.seconds + dt.microseconds * 1e-6 |
michael@0 | 28 | line = self.fmt%(self.label[:self.label_width], pct, bar, dt) |
michael@0 | 29 | self.fullwidth = len(line) |
michael@0 | 30 | sys.stdout.write(line) |
michael@0 | 31 | sys.stdout.flush() |
michael@0 | 32 | |
michael@0 | 33 | # Clear the current bar and leave the cursor at the start of the line. |
michael@0 | 34 | def clear(self): |
michael@0 | 35 | if (self.fullwidth): |
michael@0 | 36 | sys.stdout.write('\r' + ' ' * self.fullwidth + '\r') |
michael@0 | 37 | self.fullwidth = None |
michael@0 | 38 | |
michael@0 | 39 | def finish(self): |
michael@0 | 40 | self.update(self.limit) |
michael@0 | 41 | sys.stdout.write('\n') |
michael@0 | 42 | |
michael@0 | 43 | if __name__ == '__main__': |
michael@0 | 44 | pb = ProgressBar('test', 12) |
michael@0 | 45 | for i in range(12): |
michael@0 | 46 | pb.update(i) |
michael@0 | 47 | time.sleep(0.5) |
michael@0 | 48 | pb.finish() |