~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: John Arbash Meinel
  • Date: 2006-01-23 22:33:23 UTC
  • mto: This revision was merged to the branch mainline in revision 1551.
  • Revision ID: john@arbash-meinel.com-20060123223323-16eb865383bf650a
From Jari Alto: Makefile fixes (clean target error suppression)

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
        
191
181
    def update(self, msg, current_cnt=None, total_cnt=None):
192
182
        """Update and redraw progress bar."""
193
183
 
 
184
        if current_cnt < 0:
 
185
            current_cnt = 0
 
186
            
 
187
        if current_cnt > total_cnt:
 
188
            total_cnt = current_cnt
 
189
 
194
190
        # save these for the tick() function
195
191
        self.last_msg = msg
196
192
        self.last_cnt = current_cnt
199
195
        if self.throttle():
200
196
            return 
201
197
        
202
 
        if total_cnt:
203
 
            assert current_cnt <= total_cnt
204
 
        if current_cnt:
205
 
            assert current_cnt >= 0
206
 
        
207
198
        if self.show_eta and self.start_time and total_cnt:
208
 
            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)
209
201
            eta_str = " " + str_tdelta(eta)
210
202
        else:
211
203
            eta_str = ""
279
271
                             delt % 60)
280
272
 
281
273
 
282
 
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):
283
275
    if start_time is None:
284
276
        return None
285
277
 
301
293
 
302
294
    assert total_duration >= elapsed
303
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
 
304
307
    return total_duration - elapsed
305
308
 
306
309