Debian Codegraph @

js/src/gdb/progressbar.py

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