From 16c6c0977e386acac2a69a490a5f9c4d070f98a3 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Sat, 23 Apr 2016 22:43:42 -0700 Subject: [PATCH] progress: make it easier to change display width --- src/wormhole/cli/progress.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/wormhole/cli/progress.py b/src/wormhole/cli/progress.py index dec42df..23f13e7 100644 --- a/src/wormhole/cli/progress.py +++ b/src/wormhole/cli/progress.py @@ -28,7 +28,9 @@ class ProgressPrinter: def _print(self, completed): # scp does "<>(13% 168MB 39.3MB/s 00:27 ETA)" # we do "Progress: #### 13% 168MB" - fmt = "Progress: %-40s %3d%% %4d%s" + screen_width = 70 + bar_width = screen_width - 30 + fmt = "Progress: %-{}s %3d%% %4d%s".format(bar_width) short_unit_size, short_unit_name = 1, "B" if self._expected > 9999: short_unit_size, short_unit_name = 1000, "KB" @@ -40,10 +42,10 @@ class ProgressPrinter: percentage_complete = ((1.0 * completed / self._expected) if self._expected else 1.0) - bars = "#" * int(percentage_complete * 40) + bars = "#" * int(percentage_complete * bar_width) perc = int(100 * percentage_complete) short_unit_count = int(completed / short_unit_size) out = fmt % (bars, perc, short_unit_count, short_unit_name) - print(u"\r"+" "*70, end=u"", file=self._stdout) + print(u"\r"+" "*screen_width, end=u"", file=self._stdout) print(u"\r"+out, end=u"", file=self._stdout) self._stdout.flush()