~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: Robert Collins
  • Date: 2005-10-17 11:56:54 UTC
  • mfrom: (1185.16.59)
  • Revision ID: robertc@robertcollins.net-20051017115654-662239e1587524a8
mergeĀ fromĀ martin.

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
 
 
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
43
56
 
44
57
 
45
58
def _supports_progress(f):
148
161
 
149
162
 
150
163
    def __init__(self, **kwargs):
151
 
        from bzrlib.osutils import terminal_width
152
164
        _BaseProgressBar.__init__(self, **kwargs)
153
165
        self.spin_pos = 0
154
 
        self.width = terminal_width()
 
166
        self.width = _width()
155
167
        self.start_time = None
156
168
        self.last_update = None
157
 
        self.last_updates = deque()
158
169
    
159
170
 
160
171
    def throttle(self):
168
179
            if interval > 0 and interval < self.MIN_PAUSE:
169
180
                return True
170
181
 
171
 
        self.last_updates.append(now - self.last_update)
172
182
        self.last_update = now
173
183
        return False
174
184
        
196
206
            return 
197
207
        
198
208
        if self.show_eta and self.start_time and total_cnt:
199
 
            eta = get_eta(self.start_time, current_cnt, total_cnt,
200
 
                    last_updates = self.last_updates)
 
209
            eta = get_eta(self.start_time, current_cnt, total_cnt)
201
210
            eta_str = " " + str_tdelta(eta)
202
211
        else:
203
212
            eta_str = ""
271
280
                             delt % 60)
272
281
 
273
282
 
274
 
def get_eta(start_time, current, total, enough_samples=3, last_updates=None, n_recent=10):
 
283
def get_eta(start_time, current, total, enough_samples=3):
275
284
    if start_time is None:
276
285
        return None
277
286
 
293
302
 
294
303
    assert total_duration >= elapsed
295
304
 
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
 
 
307
305
    return total_duration - elapsed
308
306
 
309
307