~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

[merge] fix \t in commit messages

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
import sys
40
40
import time
41
41
import os
 
42
from collections import deque
42
43
 
43
44
 
44
45
def _width():
166
167
        self.width = _width()
167
168
        self.start_time = None
168
169
        self.last_update = None
 
170
        self.last_updates = deque()
169
171
    
170
172
 
171
173
    def throttle(self):
179
181
            if interval > 0 and interval < self.MIN_PAUSE:
180
182
                return True
181
183
 
 
184
        self.last_updates.append(now - self.last_update)
182
185
        self.last_update = now
183
186
        return False
184
187
        
191
194
    def update(self, msg, current_cnt=None, total_cnt=None):
192
195
        """Update and redraw progress bar."""
193
196
 
 
197
        if current_cnt < 0:
 
198
            current_cnt = 0
 
199
            
 
200
        if current_cnt > total_cnt:
 
201
            total_cnt = current_cnt
 
202
 
194
203
        # save these for the tick() function
195
204
        self.last_msg = msg
196
205
        self.last_cnt = current_cnt
199
208
        if self.throttle():
200
209
            return 
201
210
        
202
 
        if total_cnt:
203
 
            assert current_cnt <= total_cnt
204
 
        if current_cnt:
205
 
            assert current_cnt >= 0
206
 
        
207
211
        if self.show_eta and self.start_time and total_cnt:
208
 
            eta = get_eta(self.start_time, current_cnt, total_cnt)
 
212
            eta = get_eta(self.start_time, current_cnt, total_cnt,
 
213
                    last_updates = self.last_updates)
209
214
            eta_str = " " + str_tdelta(eta)
210
215
        else:
211
216
            eta_str = ""
279
284
                             delt % 60)
280
285
 
281
286
 
282
 
def get_eta(start_time, current, total, enough_samples=3):
 
287
def get_eta(start_time, current, total, enough_samples=3, last_updates=None, n_recent=10):
283
288
    if start_time is None:
284
289
        return None
285
290
 
301
306
 
302
307
    assert total_duration >= elapsed
303
308
 
 
309
    if last_updates and len(last_updates) >= n_recent:
 
310
        while len(last_updates) > n_recent:
 
311
            last_updates.popleft()
 
312
        avg = sum(last_updates) / float(len(last_updates))
 
313
        time_left = avg * (total - current)
 
314
 
 
315
        old_time_left = total_duration - elapsed
 
316
 
 
317
        # We could return the average, or some other value here
 
318
        return (time_left + old_time_left) / 2
 
319
 
304
320
    return total_duration - elapsed
305
321
 
306
322