1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/gdb/progressbar.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,48 @@ 1.4 +# This Source Code Form is subject to the terms of the Mozilla Public 1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.7 + 1.8 +# Text progress bar library, like curl or scp. 1.9 + 1.10 +import sys, datetime 1.11 + 1.12 +class ProgressBar(object): 1.13 + def __init__(self, label, limit, label_width=12): 1.14 + self.label = label 1.15 + self.limit = limit 1.16 + self.label_width = label_width 1.17 + self.cur = 0 1.18 + self.t0 = datetime.datetime.now() 1.19 + self.fullwidth = None 1.20 + 1.21 + self.barlen = 64 - self.label_width 1.22 + self.fmt = '\r%-' + str(label_width) + 's %3d%% %-' + str(self.barlen) + 's| %6.1fs' 1.23 + 1.24 + def update(self, value): 1.25 + self.cur = value 1.26 + pct = int(100.0 * self.cur / self.limit) 1.27 + barlen = int(1.0 * self.barlen * self.cur / self.limit) - 1 1.28 + bar = '='*barlen + '>' 1.29 + dt = datetime.datetime.now() - self.t0 1.30 + dt = dt.seconds + dt.microseconds * 1e-6 1.31 + line = self.fmt%(self.label[:self.label_width], pct, bar, dt) 1.32 + self.fullwidth = len(line) 1.33 + sys.stdout.write(line) 1.34 + sys.stdout.flush() 1.35 + 1.36 + # Clear the current bar and leave the cursor at the start of the line. 1.37 + def clear(self): 1.38 + if (self.fullwidth): 1.39 + sys.stdout.write('\r' + ' ' * self.fullwidth + '\r') 1.40 + self.fullwidth = None 1.41 + 1.42 + def finish(self): 1.43 + self.update(self.limit) 1.44 + sys.stdout.write('\n') 1.45 + 1.46 +if __name__ == '__main__': 1.47 + pb = ProgressBar('test', 12) 1.48 + for i in range(12): 1.49 + pb.update(i) 1.50 + time.sleep(0.5) 1.51 + pb.finish()