~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: Martin Pool
  • Date: 2006-01-13 08:12:22 UTC
  • mfrom: (1185.63.5 bzr.patches)
  • Revision ID: mbp@sourcefrog.net-20060113081222-6b572004a2ade0cc
[merge] test_hashcache_raise from Denys

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
 
 
43
 
 
44
 
def _width():
45
 
    """Return estimated terminal width.
46
 
 
47
 
    TODO: Do something smart on Windows?
48
 
 
49
 
    TODO: Is there anything that gets a better update when the window
50
 
          is resized while the program is running?
51
 
    """
52
 
    try:
53
 
        return int(os.environ['COLUMNS'])
54
 
    except (IndexError, KeyError, ValueError):
55
 
        return 80
 
42
from collections import deque
56
43
 
57
44
 
58
45
def _supports_progress(f):
161
148
 
162
149
 
163
150
    def __init__(self, **kwargs):
 
151
        from bzrlib.osutils import terminal_width
164
152
        _BaseProgressBar.__init__(self, **kwargs)
165
153
        self.spin_pos = 0
166
 
        self.width = _width()
 
154
        self.width = terminal_width()
167
155
        self.start_time = None
168
156
        self.last_update = None
 
157
        self.last_updates = deque()
169
158
    
170
159
 
171
160
    def throttle(self):
179
168
            if interval > 0 and interval < self.MIN_PAUSE:
180
169
                return True
181
170
 
 
171
        self.last_updates.append(now - self.last_update)
182
172
        self.last_update = now
183
173
        return False
184
174
        
206
196
            return 
207
197
        
208
198
        if self.show_eta and self.start_time and total_cnt:
209
 
            eta = get_eta(self.start_time, current_cnt, total_cnt)
 
199
            eta = get_eta(self.start_time, current_cnt, total_cnt,
 
200
                    last_updates = self.last_updates)
210
201
            eta_str = " " + str_tdelta(eta)
211
202
        else:
212
203
            eta_str = ""
280
271
                             delt % 60)
281
272
 
282
273
 
283
 
def get_eta(start_time, current, total, enough_samples=3):
 
274
def get_eta(start_time, current, total, enough_samples=3, last_updates=None, n_recent=10):
284
275
    if start_time is None:
285
276
        return None
286
277
 
302
293
 
303
294
    assert total_duration >= elapsed
304
295
 
 
296
    if last_updates and len(last_updates) >= n_recent:
 
297
        while len(last_updates) > n_recent:
 
298
            last_updates.popleft()
 
299
        avg = sum(last_updates) / float(len(last_updates))
 
300
        time_left = avg * (total - current)
 
301
 
 
302
        old_time_left = total_duration - elapsed
 
303
 
 
304
        # We could return the average, or some other value here
 
305
        return (time_left + old_time_left) / 2
 
306
 
305
307
    return total_duration - elapsed
306
308
 
307
309