diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/gdb/progressbar.py | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/gdb/progressbar.py')
-rw-r--r-- | js/src/gdb/progressbar.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/js/src/gdb/progressbar.py b/js/src/gdb/progressbar.py new file mode 100644 index 0000000000..04be825c95 --- /dev/null +++ b/js/src/gdb/progressbar.py @@ -0,0 +1,54 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +# Text progress bar library, like curl or scp. + +import datetime +import sys +import time + + +class ProgressBar(object): + def __init__(self, label, limit, label_width=12): + self.label = label + self.limit = limit + self.label_width = label_width + self.cur = 0 + self.t0 = datetime.datetime.now() + self.fullwidth = None + + self.barlen = 64 - self.label_width + self.fmt = ( + "\r%-" + str(label_width) + "s %3d%% %-" + str(self.barlen) + "s| %6.1fs" + ) + + def update(self, value): + self.cur = value + pct = int(100.0 * self.cur / self.limit) + barlen = int(1.0 * self.barlen * self.cur / self.limit) - 1 + bar = "=" * barlen + ">" + dt = datetime.datetime.now() - self.t0 + dt = dt.seconds + dt.microseconds * 1e-6 + line = self.fmt % (self.label[: self.label_width], pct, bar, dt) + self.fullwidth = len(line) + sys.stdout.write(line) + sys.stdout.flush() + + # Clear the current bar and leave the cursor at the start of the line. + def clear(self): + if self.fullwidth: + sys.stdout.write("\r" + " " * self.fullwidth + "\r") + self.fullwidth = None + + def finish(self): + self.update(self.limit) + sys.stdout.write("\n") + + +if __name__ == "__main__": + pb = ProgressBar("test", 12) + for i in range(12): + pb.update(i) + time.sleep(0.5) + pb.finish() |