summaryrefslogtreecommitdiffstats
path: root/progress.py
diff options
context:
space:
mode:
Diffstat (limited to 'progress.py')
-rw-r--r--progress.py49
1 files changed, 30 insertions, 19 deletions
diff --git a/progress.py b/progress.py
index 4844eb88..6686ad4a 100644
--- a/progress.py
+++ b/progress.py
@@ -82,10 +82,10 @@ class Progress(object):
82 title, 82 title,
83 total=0, 83 total=0,
84 units="", 84 units="",
85 print_newline=False,
86 delay=True, 85 delay=True,
87 quiet=False, 86 quiet=False,
88 show_elapsed=False, 87 show_elapsed=False,
88 elide=False,
89 ): 89 ):
90 self._title = title 90 self._title = title
91 self._total = total 91 self._total = total
@@ -93,7 +93,7 @@ class Progress(object):
93 self._start = time.time() 93 self._start = time.time()
94 self._show = not delay 94 self._show = not delay
95 self._units = units 95 self._units = units
96 self._print_newline = print_newline 96 self._elide = elide
97 # Only show the active jobs section if we run more than one in parallel. 97 # Only show the active jobs section if we run more than one in parallel.
98 self._show_jobs = False 98 self._show_jobs = False
99 self._active = 0 99 self._active = 0
@@ -118,10 +118,18 @@ class Progress(object):
118 118
119 def _update_loop(self): 119 def _update_loop(self):
120 while True: 120 while True:
121 if self._update_event.is_set(): 121 self.update(inc=0)
122 if self._update_event.wait(timeout=1):
122 return 123 return
123 self.update(inc=0, msg=self._last_msg) 124
124 time.sleep(1) 125 def _write(self, s):
126 s = "\r" + s
127 if self._elide:
128 col = os.get_terminal_size().columns
129 if len(s) > col:
130 s = s[: col - 1] + ".."
131 sys.stderr.write(s)
132 sys.stderr.flush()
125 133
126 def start(self, name): 134 def start(self, name):
127 self._active += 1 135 self._active += 1
@@ -133,8 +141,16 @@ class Progress(object):
133 self.update(msg="finished " + name) 141 self.update(msg="finished " + name)
134 self._active -= 1 142 self._active -= 1
135 143
136 def update(self, inc=1, msg=""): 144 def update(self, inc=1, msg=None):
145 """Updates the progress indicator.
146
147 Args:
148 inc: The number of items completed.
149 msg: The message to display. If None, use the last message.
150 """
137 self._done += inc 151 self._done += inc
152 if msg is None:
153 msg = self._last_msg
138 self._last_msg = msg 154 self._last_msg = msg
139 155
140 if _NOT_TTY or IsTraceToStderr(): 156 if _NOT_TTY or IsTraceToStderr():
@@ -148,10 +164,9 @@ class Progress(object):
148 return 164 return
149 165
150 if self._total <= 0: 166 if self._total <= 0:
151 sys.stderr.write( 167 self._write(
152 "\r%s: %d,%s" % (self._title, self._done, CSI_ERASE_LINE_AFTER) 168 "%s: %d,%s" % (self._title, self._done, CSI_ERASE_LINE_AFTER)
153 ) 169 )
154 sys.stderr.flush()
155 else: 170 else:
156 p = (100 * self._done) / self._total 171 p = (100 * self._done) / self._total
157 if self._show_jobs: 172 if self._show_jobs:
@@ -165,8 +180,8 @@ class Progress(object):
165 elapsed = f" {elapsed_str(elapsed_sec)} |" 180 elapsed = f" {elapsed_str(elapsed_sec)} |"
166 else: 181 else:
167 elapsed = "" 182 elapsed = ""
168 sys.stderr.write( 183 self._write(
169 "\r%s: %2d%% %s(%d%s/%d%s)%s %s%s%s" 184 "%s: %2d%% %s(%d%s/%d%s)%s %s%s"
170 % ( 185 % (
171 self._title, 186 self._title,
172 p, 187 p,
@@ -178,10 +193,8 @@ class Progress(object):
178 elapsed, 193 elapsed,
179 msg, 194 msg,
180 CSI_ERASE_LINE_AFTER, 195 CSI_ERASE_LINE_AFTER,
181 "\n" if self._print_newline else "",
182 ) 196 )
183 ) 197 )
184 sys.stderr.flush()
185 198
186 def end(self): 199 def end(self):
187 self._update_event.set() 200 self._update_event.set()
@@ -190,15 +203,14 @@ class Progress(object):
190 203
191 duration = duration_str(time.time() - self._start) 204 duration = duration_str(time.time() - self._start)
192 if self._total <= 0: 205 if self._total <= 0:
193 sys.stderr.write( 206 self._write(
194 "\r%s: %d, done in %s%s\n" 207 "%s: %d, done in %s%s\n"
195 % (self._title, self._done, duration, CSI_ERASE_LINE_AFTER) 208 % (self._title, self._done, duration, CSI_ERASE_LINE_AFTER)
196 ) 209 )
197 sys.stderr.flush()
198 else: 210 else:
199 p = (100 * self._done) / self._total 211 p = (100 * self._done) / self._total
200 sys.stderr.write( 212 self._write(
201 "\r%s: %3d%% (%d%s/%d%s), done in %s%s\n" 213 "%s: %3d%% (%d%s/%d%s), done in %s%s\n"
202 % ( 214 % (
203 self._title, 215 self._title,
204 p, 216 p,
@@ -210,4 +222,3 @@ class Progress(object):
210 CSI_ERASE_LINE_AFTER, 222 CSI_ERASE_LINE_AFTER,
211 ) 223 )
212 ) 224 )
213 sys.stderr.flush()