~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

  • Committer: Matt Nordhoff
  • Date: 2009-04-04 02:50:01 UTC
  • mfrom: (4253 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4256.
  • Revision ID: mnordhoff@mattnordhoff.com-20090404025001-z1403k0tatmc8l91
Merge bzr.dev, fixing conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
 
48
48
                 stderr=None):
49
49
        """Create a TextUIFactory.
50
50
 
51
 
        :param bar_type: The type of progress bar to create. It defaults to 
 
51
        :param bar_type: The type of progress bar to create. It defaults to
52
52
                         letting the bzrlib.progress.ProgressBar factory auto
53
53
                         select.   Deprecated.
54
54
        """
60
60
        # paints progress, network activity, etc
61
61
        self._progress_view = TextProgressView(self.stderr)
62
62
 
63
 
    def prompt(self, prompt):
64
 
        """Emit prompt on the CLI."""
65
 
        self.stdout.write(prompt)
66
 
        
67
63
    def clear_term(self):
68
64
        """Prepare the terminal for output.
69
65
 
72
68
        # XXX: If this is preparing to write to stdout, but that's for example
73
69
        # directed into a file rather than to the terminal, and the progress
74
70
        # bar _is_ going to the terminal, we shouldn't need
75
 
        # to clear it.  We might need to separately check for the case of 
 
71
        # to clear it.  We might need to separately check for the case of
76
72
        self._progress_view.clear()
77
73
 
78
74
    def note(self, msg):
82
78
 
83
79
    def report_transport_activity(self, transport, byte_count, direction):
84
80
        """Called by transports as they do IO.
85
 
        
 
81
 
86
82
        This may update a progress bar, spinner, or similar display.
87
83
        By default it does nothing.
88
84
        """
89
 
        self._progress_view.show_transport_activity(byte_count)
 
85
        self._progress_view._show_transport_activity(transport,
 
86
            direction, byte_count)
90
87
 
91
88
    def _progress_updated(self, task):
92
89
        """A task has been updated and wants to be displayed.
93
90
        """
94
 
        if task != self._task_stack[-1]:
 
91
        if not self._task_stack:
 
92
            warnings.warn("%r updated but no tasks are active" %
 
93
                (task,))
 
94
        elif task != self._task_stack[-1]:
95
95
            warnings.warn("%r is not the top progress task %r" %
96
96
                (task, self._task_stack[-1]))
97
97
        self._progress_view.show_progress(task)
102
102
 
103
103
class TextProgressView(object):
104
104
    """Display of progress bar and other information on a tty.
105
 
    
106
 
    This shows one line of text, including possibly a network indicator, spinner, 
 
105
 
 
106
    This shows one line of text, including possibly a network indicator, spinner,
107
107
    progress bar, message, etc.
108
108
 
109
109
    One instance of this is created and held by the UI, and fed updates when a
127
127
        self._last_repaint = 0
128
128
        # time we last got information about transport activity
129
129
        self._transport_update_time = 0
130
 
        self._task_fraction = None
131
130
        self._last_task = None
132
131
        self._total_byte_count = 0
133
132
        self._bytes_since_update = 0
143
142
 
144
143
    def _render_bar(self):
145
144
        # return a string for the progress bar itself
146
 
        if (self._last_task is not None) and self._last_task.show_bar:
 
145
        if (self._last_task is None) or self._last_task.show_bar:
 
146
            # If there's no task object, we show space for the bar anyhow.
 
147
            # That's because most invocations of bzr will end showing progress
 
148
            # at some point, though perhaps only after doing some initial IO.
 
149
            # It looks better to draw the progress bar initially rather than
 
150
            # to have what looks like an incomplete progress bar.
147
151
            spin_str =  r'/-\|'[self._spin_pos % 4]
148
152
            self._spin_pos += 1
149
 
            f = self._task_fraction or 0
150
153
            cols = 20
151
 
            # number of markers highlighted in bar
152
 
            markers = int(round(float(cols) * f)) - 1
 
154
            if self._last_task is None:
 
155
                completion_fraction = 0
 
156
            else:
 
157
                completion_fraction = \
 
158
                    self._last_task._overall_completion_fraction() or 0
 
159
            markers = int(round(float(cols) * completion_fraction)) - 1
153
160
            bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
154
161
            return bar_str
155
 
        elif (self._last_task is None) or self._last_task.show_spinner:
 
162
        elif self._last_task.show_spinner:
 
163
            # The last task wanted just a spinner, no bar
156
164
            spin_str =  r'/-\|'[self._spin_pos % 4]
157
165
            self._spin_pos += 1
158
166
            return spin_str + ' '
168
176
            s = ' %d' % (task.current_cnt)
169
177
        else:
170
178
            s = ''
171
 
        self._task_fraction = task._overall_completion_fraction()
172
179
        # compose all the parent messages
173
180
        t = task
174
181
        m = task.msg
178
185
                m = t.msg + ':' + m
179
186
        return m + s
180
187
 
181
 
    def _repaint(self):
 
188
    def _render_line(self):
182
189
        bar_string = self._render_bar()
183
190
        if self._last_task:
184
191
            task_msg = self._format_task(self._last_task)
185
192
        else:
186
193
            task_msg = ''
187
194
        trans = self._last_transport_msg
188
 
        if trans and task_msg:
 
195
        if trans:
189
196
            trans += ' | '
190
 
        s = (bar_string
191
 
             + trans
192
 
             + task_msg
193
 
             )
 
197
        return (bar_string + trans + task_msg)
 
198
 
 
199
    def _repaint(self):
 
200
        s = self._render_line()
194
201
        self._show_line(s)
195
202
        self._have_output = True
196
203
 
197
204
    def show_progress(self, task):
 
205
        """Called by the task object when it has changed.
 
206
        
 
207
        :param task: The top task object; its parents are also included 
 
208
            by following links.
 
209
        """
 
210
        must_update = task is not self._last_task
198
211
        self._last_task = task
199
212
        now = time.time()
200
 
        if now < self._last_repaint + 0.1:
 
213
        if (not must_update) and (now < self._last_repaint + 0.1):
201
214
            return
202
 
        if now > self._transport_update_time + 5:
 
215
        if now > self._transport_update_time + 10:
203
216
            # no recent activity; expire it
204
217
            self._last_transport_msg = ''
205
218
        self._last_repaint = now
206
219
        self._repaint()
207
220
 
208
 
    def show_transport_activity(self, byte_count):
209
 
        """Called by transports as they do IO.
210
 
        
 
221
    def _show_transport_activity(self, transport, direction, byte_count):
 
222
        """Called by transports via the ui_factory, as they do IO.
 
223
 
211
224
        This may update a progress bar, spinner, or similar display.
212
225
        By default it does nothing.
213
226
        """
219
232
        now = time.time()
220
233
        if self._transport_update_time is None:
221
234
            self._transport_update_time = now
222
 
        elif now >= (self._transport_update_time + 0.2):
 
235
        elif now >= (self._transport_update_time + 0.5):
223
236
            # guard against clock stepping backwards, and don't update too
224
237
            # often
225
238
            rate = self._bytes_since_update / (now - self._transport_update_time)
226
 
            msg = ("%6dkB @ %4dkB/s" %
227
 
                (self._total_byte_count>>10, int(rate)>>10,))
 
239
            scheme = getattr(transport, '_scheme', None) or repr(transport)
 
240
            if direction == 'read':
 
241
                dir_char = '>'
 
242
            elif direction == 'write':
 
243
                dir_char = '<'
 
244
            else:
 
245
                dir_char = '?'
 
246
            msg = ("%.7s %s %6dKB %5dKB/s" %
 
247
                    (scheme, dir_char, self._total_byte_count>>10, int(rate)>>10,))
228
248
            self._transport_update_time = now
229
249
            self._last_repaint = now
230
250
            self._bytes_since_update = 0