~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-02-18 02:33:47 UTC
  • mfrom: (1534.1.24 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060218023347-0952c65f668bfd68
Merge Robert Collins integration.

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):
82
69
                 show_spinner=False,
83
70
                 show_eta=True,
84
71
                 show_bar=True,
85
 
                 show_count=True):
 
72
                 show_count=True,
 
73
                 to_messages_file=sys.stdout):
86
74
        object.__init__(self)
87
75
        self.to_file = to_file
88
 
 
 
76
        self.to_messages_file = to_messages_file
89
77
        self.last_msg = None
90
78
        self.last_cnt = None
91
79
        self.last_total = None
95
83
        self.show_bar = show_bar
96
84
        self.show_count = show_count
97
85
 
 
86
    def note(self, fmt_string, *args, **kwargs):
 
87
        """Record a note without disrupting the progress bar."""
 
88
        self.clear()
 
89
        self.to_messages_file.write(fmt_string % args)
 
90
        self.to_messages_file.write('\n')
98
91
 
99
92
 
100
93
class DummyProgress(_BaseProgressBar):
111
104
    def clear(self):
112
105
        pass
113
106
        
114
 
    
 
107
    def note(self, fmt_string, *args, **kwargs):
 
108
        """See _BaseProgressBar.note()."""
 
109
 
 
110
 
115
111
class DotsProgressBar(_BaseProgressBar):
116
112
    def __init__(self, **kwargs):
117
113
        _BaseProgressBar.__init__(self, **kwargs)
161
157
 
162
158
 
163
159
    def __init__(self, **kwargs):
 
160
        from bzrlib.osutils import terminal_width
164
161
        _BaseProgressBar.__init__(self, **kwargs)
165
162
        self.spin_pos = 0
166
 
        self.width = _width()
 
163
        self.width = terminal_width()
167
164
        self.start_time = None
168
165
        self.last_update = None
 
166
        self.last_updates = deque()
169
167
    
170
168
 
171
169
    def throttle(self):
179
177
            if interval > 0 and interval < self.MIN_PAUSE:
180
178
                return True
181
179
 
 
180
        self.last_updates.append(now - self.last_update)
182
181
        self.last_update = now
183
182
        return False
184
183
        
206
205
            return 
207
206
        
208
207
        if self.show_eta and self.start_time and total_cnt:
209
 
            eta = get_eta(self.start_time, current_cnt, total_cnt)
 
208
            eta = get_eta(self.start_time, current_cnt, total_cnt,
 
209
                    last_updates = self.last_updates)
210
210
            eta_str = " " + str_tdelta(eta)
211
211
        else:
212
212
            eta_str = ""
264
264
        self.to_file.write('\r' + m.ljust(self.width - 1))
265
265
        #self.to_file.flush()
266
266
            
267
 
 
268
267
    def clear(self):        
269
268
        self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
270
269
        #self.to_file.flush()        
271
 
    
272
270
 
273
271
        
274
272
def str_tdelta(delt):
280
278
                             delt % 60)
281
279
 
282
280
 
283
 
def get_eta(start_time, current, total, enough_samples=3):
 
281
def get_eta(start_time, current, total, enough_samples=3, last_updates=None, n_recent=10):
284
282
    if start_time is None:
285
283
        return None
286
284
 
302
300
 
303
301
    assert total_duration >= elapsed
304
302
 
 
303
    if last_updates and len(last_updates) >= n_recent:
 
304
        while len(last_updates) > n_recent:
 
305
            last_updates.popleft()
 
306
        avg = sum(last_updates) / float(len(last_updates))
 
307
        time_left = avg * (total - current)
 
308
 
 
309
        old_time_left = total_duration - elapsed
 
310
 
 
311
        # We could return the average, or some other value here
 
312
        return (time_left + old_time_left) / 2
 
313
 
305
314
    return total_duration - elapsed
306
315
 
307
316