diff options
Diffstat (limited to 'progress.py')
-rw-r--r-- | progress.py | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/progress.py b/progress.py index d19fc5b1..ae797488 100644 --- a/progress.py +++ b/progress.py | |||
@@ -25,6 +25,22 @@ _NOT_TTY = not os.isatty(2) | |||
25 | CSI_ERASE_LINE = '\x1b[2K' | 25 | CSI_ERASE_LINE = '\x1b[2K' |
26 | 26 | ||
27 | 27 | ||
28 | def duration_str(total): | ||
29 | """A less noisy timedelta.__str__. | ||
30 | |||
31 | The default timedelta stringification contains a lot of leading zeros and | ||
32 | uses microsecond resolution. This makes for noisy output. | ||
33 | """ | ||
34 | hours, rem = divmod(total, 3600) | ||
35 | mins, secs = divmod(rem, 60) | ||
36 | ret = '%.3fs' % (secs,) | ||
37 | if mins: | ||
38 | ret = '%im%s' % (mins, ret) | ||
39 | if hours: | ||
40 | ret = '%ih%s' % (hours, ret) | ||
41 | return ret | ||
42 | |||
43 | |||
28 | class Progress(object): | 44 | class Progress(object): |
29 | def __init__(self, title, total=0, units='', print_newline=False): | 45 | def __init__(self, title, total=0, units='', print_newline=False): |
30 | self._title = title | 46 | self._title = title |
@@ -87,18 +103,21 @@ class Progress(object): | |||
87 | if _NOT_TTY or IsTrace() or not self._show: | 103 | if _NOT_TTY or IsTrace() or not self._show: |
88 | return | 104 | return |
89 | 105 | ||
106 | duration = duration_str(time() - self._start) | ||
90 | if self._total <= 0: | 107 | if self._total <= 0: |
91 | sys.stderr.write('%s\r%s: %d, done.\n' % ( | 108 | sys.stderr.write('%s\r%s: %d, done in %s\n' % ( |
92 | CSI_ERASE_LINE, | 109 | CSI_ERASE_LINE, |
93 | self._title, | 110 | self._title, |
94 | self._done)) | 111 | self._done, |
112 | duration)) | ||
95 | sys.stderr.flush() | 113 | sys.stderr.flush() |
96 | else: | 114 | else: |
97 | p = (100 * self._done) / self._total | 115 | p = (100 * self._done) / self._total |
98 | sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done.\n' % ( | 116 | sys.stderr.write('%s\r%s: %3d%% (%d%s/%d%s), done in %s\n' % ( |
99 | CSI_ERASE_LINE, | 117 | CSI_ERASE_LINE, |
100 | self._title, | 118 | self._title, |
101 | p, | 119 | p, |
102 | self._done, self._units, | 120 | self._done, self._units, |
103 | self._total, self._units)) | 121 | self._total, self._units, |
122 | duration)) | ||
104 | sys.stderr.flush() | 123 | sys.stderr.flush() |