~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: Martin Pool
  • Date: 2005-07-22 19:44:09 UTC
  • Revision ID: mbp@sourcefrog.net-20050722194409-9f99ffe3d62cf8c0
- tidy up imports to use full module names

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
 
19
 
"""
20
 
Simple text-mode progress indicator.
21
 
 
22
 
Everyone loves ascii art!
 
19
"""Simple text-mode progress indicator.
23
20
 
24
21
To display an indicator, create a ProgressBar object.  Call it,
25
22
passing Progress objects indicating the current state.  When done,
29
26
not to clutter log files.
30
27
"""
31
28
 
32
 
# TODO: remove functions in favour of keeping everything in one class
33
 
 
34
29
# TODO: should be a global option e.g. --silent that disables progress
35
30
# indicators, preferably without needing to adjust all code that
36
31
# potentially calls them.
38
33
# TODO: Perhaps don't write updates faster than a certain rate, say
39
34
# 5/second.
40
35
 
 
36
# TODO: Optionally show elapsed time instead/as well as ETA; nicer
 
37
# when the rate is unpredictable
 
38
 
41
39
 
42
40
import sys
43
41
import time
59
57
 
60
58
 
61
59
def _supports_progress(f):
62
 
    return hasattr(f, 'isatty') and f.isatty()
 
60
    if not hasattr(f, 'isatty'):
 
61
        return False
 
62
    if not f.isatty():
 
63
        return False
 
64
    import os
 
65
    if os.environ.get('TERM') == 'dumb':
 
66
        # e.g. emacs compile window
 
67
        return False
 
68
    return True
63
69
 
64
70
 
65
71
 
110
116
        self.show_bar = show_bar
111
117
        self.show_count = show_count
112
118
 
 
119
        self.width = _width()
 
120
        
113
121
 
114
122
    def tick(self):
115
123
        self.update(self.last_msg, self.last_cnt, self.last_total)
136
144
 
137
145
        self.last_update = now
138
146
        
139
 
        width = _width()
140
 
 
141
147
        if total_cnt:
142
148
            assert current_cnt <= total_cnt
143
149
        if current_cnt:
177
183
 
178
184
        if self.show_bar:
179
185
            # progress bar, if present, soaks up all remaining space
180
 
            cols = width - 1 - len(msg) - len(spin_str) - len(pct_str) \
 
186
            cols = self.width - 1 - len(msg) - len(spin_str) - len(pct_str) \
181
187
                   - len(eta_str) - len(count_str) - 3
182
188
 
183
189
            if total_cnt:
198
204
 
199
205
        m = spin_str + bar_str + msg + count_str + pct_str + eta_str
200
206
 
201
 
        assert len(m) < width
202
 
        self.to_file.write('\r' + m.ljust(width - 1))
 
207
        assert len(m) < self.width
 
208
        self.to_file.write('\r' + m.ljust(self.width - 1))
203
209
        #self.to_file.flush()
204
210
            
205
211
 
207
213
        if self.suppressed:
208
214
            return
209
215
        
210
 
        self.to_file.write('\r%s\r' % (' ' * (_width() - 1)))
 
216
        self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
211
217
        #self.to_file.flush()        
212
218
    
213
219