/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/progress.py

  • Committer: John Arbash Meinel
  • Date: 2006-03-08 14:31:23 UTC
  • mfrom: (1598 +trunk)
  • mto: (1685.1.1 bzr-encoding)
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060308143123-448308b0db4de410
[merge] bzr.dev 1573, lots of updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005 Aaron Bentley <aaron.bentley@utoronto.ca>
2
 
# Copyright (C) 2005 Canonical <canonical.com>
 
2
# Copyright (C) 2005, 2006 Canonical <canonical.com>
3
3
#
4
4
#    This program is free software; you can redistribute it and/or modify
5
5
#    it under the terms of the GNU General Public License as published by
42
42
from collections import deque
43
43
 
44
44
 
 
45
import bzrlib.errors as errors
 
46
 
 
47
 
45
48
def _supports_progress(f):
46
49
    if not hasattr(f, 'isatty'):
47
50
        return False
61
64
    else:
62
65
        return DotsProgressBar(to_file=to_file, **kwargs)
63
66
    
64
 
    
 
67
 
 
68
class ProgressBarStack(object):
 
69
    """A stack of progress bars."""
 
70
 
 
71
    def __init__(self,
 
72
                 to_file=sys.stderr,
 
73
                 show_pct=False,
 
74
                 show_spinner=False,
 
75
                 show_eta=True,
 
76
                 show_bar=True,
 
77
                 show_count=True,
 
78
                 to_messages_file=sys.stdout,
 
79
                 klass=None):
 
80
        """Setup the stack with the parameters the progress bars should have."""
 
81
        self._to_file = to_file
 
82
        self._show_pct = show_pct
 
83
        self._show_spinner = show_spinner
 
84
        self._show_eta = show_eta
 
85
        self._show_bar = show_bar
 
86
        self._show_count = show_count
 
87
        self._to_messages_file = to_messages_file
 
88
        self._stack = []
 
89
        self._klass = klass or TTYProgressBar
 
90
 
 
91
    def get_nested(self):
 
92
        """Return a nested progress bar."""
 
93
        # initial implementation - return a new bar each time.
 
94
        new_bar = self._klass(to_file=self._to_file,
 
95
                              show_pct=self._show_pct,
 
96
                              show_spinner=self._show_spinner,
 
97
                              show_eta=self._show_eta,
 
98
                              show_bar=self._show_bar,
 
99
                              show_count=self._show_count,
 
100
                              to_messages_file=self._to_messages_file,
 
101
                              _stack=self)
 
102
        self._stack.append(new_bar)
 
103
        return new_bar
 
104
 
 
105
    def return_pb(self, bar):
 
106
        """Return bar after its been used."""
 
107
        if bar is not self._stack[-1]:
 
108
            raise errors.MissingProgressBarFinish()
 
109
        self._stack.pop()
 
110
 
 
111
 
65
112
class _BaseProgressBar(object):
 
113
 
66
114
    def __init__(self,
67
115
                 to_file=sys.stderr,
68
116
                 show_pct=False,
69
117
                 show_spinner=False,
70
118
                 show_eta=True,
71
119
                 show_bar=True,
72
 
                 show_count=True):
 
120
                 show_count=True,
 
121
                 to_messages_file=sys.stdout,
 
122
                 _stack=None):
73
123
        object.__init__(self)
74
124
        self.to_file = to_file
75
 
 
 
125
        self.to_messages_file = to_messages_file
76
126
        self.last_msg = None
77
127
        self.last_cnt = None
78
128
        self.last_total = None
81
131
        self.show_eta = show_eta
82
132
        self.show_bar = show_bar
83
133
        self.show_count = show_count
84
 
 
 
134
        self._stack = _stack
 
135
 
 
136
    def finished(self):
 
137
        """Return this bar to its progress stack."""
 
138
        self.clear()
 
139
        assert self._stack is not None
 
140
        self._stack.return_pb(self)
 
141
 
 
142
    def note(self, fmt_string, *args, **kwargs):
 
143
        """Record a note without disrupting the progress bar."""
 
144
        self.clear()
 
145
        self.to_messages_file.write(fmt_string % args)
 
146
        self.to_messages_file.write('\n')
85
147
 
86
148
 
87
149
class DummyProgress(_BaseProgressBar):
98
160
    def clear(self):
99
161
        pass
100
162
        
101
 
    
 
163
    def note(self, fmt_string, *args, **kwargs):
 
164
        """See _BaseProgressBar.note()."""
 
165
 
 
166
 
102
167
class DotsProgressBar(_BaseProgressBar):
 
168
 
103
169
    def __init__(self, **kwargs):
104
170
        _BaseProgressBar.__init__(self, **kwargs)
105
171
        self.last_msg = None
186
252
            
187
253
        if current_cnt > total_cnt:
188
254
            total_cnt = current_cnt
189
 
 
 
255
        
 
256
        old_msg = self.last_msg
190
257
        # save these for the tick() function
191
258
        self.last_msg = msg
192
259
        self.last_cnt = current_cnt
193
260
        self.last_total = total_cnt
194
261
            
195
 
        if self.throttle():
 
262
        if old_msg == self.last_msg and self.throttle():
196
263
            return 
197
264
        
198
265
        if self.show_eta and self.start_time and total_cnt:
255
322
        self.to_file.write('\r' + m.ljust(self.width - 1))
256
323
        #self.to_file.flush()
257
324
            
258
 
 
259
325
    def clear(self):        
260
326
        self.to_file.write('\r%s\r' % (' ' * (self.width - 1)))
261
327
        #self.to_file.flush()        
262
 
    
263
328
 
264
329
        
265
330
def str_tdelta(delt):