/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/ui/text.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-12-18 10:09:49 UTC
  • mfrom: (4871.5.4 admin-guide-submit)
  • Revision ID: pqm@pqm.ubuntu.com-20091218100949-2c1ityvnbqjtdf3g
(nmb) Add backup section to admin-guide

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2008, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
32
32
    progress,
33
33
    osutils,
34
34
    symbol_versioning,
35
 
    trace,
36
35
    )
37
36
 
38
37
""")
60
59
        self.stderr = stderr
61
60
        # paints progress, network activity, etc
62
61
        self._progress_view = self.make_progress_view()
63
 
 
64
 
    def be_quiet(self, state):
65
 
        if state and not self._quiet:
66
 
            self.clear_term()
67
 
        UIFactory.be_quiet(self, state)
68
 
        self._progress_view = self.make_progress_view()
69
 
 
 
62
        
70
63
    def clear_term(self):
71
64
        """Prepare the terminal for output.
72
65
 
152
145
    def make_progress_view(self):
153
146
        """Construct and return a new ProgressView subclass for this UI.
154
147
        """
155
 
        # with --quiet, never any progress view
156
 
        # <https://bugs.launchpad.net/bzr/+bug/320035>.  Otherwise if the
157
 
        # user specifically requests either text or no progress bars, always
158
 
        # do that.  otherwise, guess based on $TERM and tty presence.
159
 
        if self.is_quiet():
160
 
            return NullProgressView()
161
 
        elif os.environ.get('BZR_PROGRESS_BAR') == 'text':
 
148
        # if the user specifically requests either text or no progress bars,
 
149
        # always do that.  otherwise, guess based on $TERM and tty presence.
 
150
        if os.environ.get('BZR_PROGRESS_BAR') == 'text':
162
151
            return TextProgressView(self.stderr)
163
152
        elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
164
153
            return NullProgressView()
214
203
        self._progress_view.show_transport_activity(transport,
215
204
            direction, byte_count)
216
205
 
217
 
    def log_transport_activity(self, display=False):
218
 
        """See UIFactory.log_transport_activity()"""
219
 
        log = getattr(self._progress_view, 'log_transport_activity', None)
220
 
        if log is not None:
221
 
            log(display=display)
222
 
 
223
206
    def show_error(self, msg):
224
207
        self.clear_term()
225
208
        self.stderr.write("bzr: error: %s\n" % msg)
229
212
 
230
213
    def show_warning(self, msg):
231
214
        self.clear_term()
232
 
        if isinstance(msg, unicode):
233
 
            te = osutils.get_terminal_encoding()
234
 
            msg = msg.encode(te, 'replace')
235
215
        self.stderr.write("bzr: warning: %s\n" % msg)
236
216
 
237
217
    def _progress_updated(self, task):
241
221
            warnings.warn("%r updated but no tasks are active" %
242
222
                (task,))
243
223
        elif task != self._task_stack[-1]:
244
 
            # We used to check it was the top task, but it's hard to always
245
 
            # get this right and it's not necessarily useful: any actual
246
 
            # problems will be evident in use
247
 
            #warnings.warn("%r is not the top progress task %r" %
248
 
            #     (task, self._task_stack[-1]))
249
 
            pass
 
224
            warnings.warn("%r is not the top progress task %r" %
 
225
                (task, self._task_stack[-1]))
250
226
        self._progress_view.show_progress(task)
251
227
 
252
228
    def _progress_all_finished(self):
253
229
        self._progress_view.clear()
254
230
 
255
 
    def show_user_warning(self, warning_id, **message_args):
256
 
        """Show a text message to the user.
257
 
 
258
 
        Explicitly not for warnings about bzr apis, deprecations or internals.
259
 
        """
260
 
        # eventually trace.warning should migrate here, to avoid logging and
261
 
        # be easier to test; that has a lot of test fallout so for now just
262
 
        # new code can call this
263
 
        if warning_id not in self.suppressed_warnings:
264
 
            self.stderr.write(self.format_user_warning(warning_id, message_args) +
265
 
                '\n')
266
 
 
267
231
 
268
232
class TextProgressView(object):
269
233
    """Display of progress bar and other information on a tty.
293
257
        self._last_task = None
294
258
        self._total_byte_count = 0
295
259
        self._bytes_since_update = 0
296
 
        self._bytes_by_direction = {'unknown': 0, 'read': 0, 'write': 0}
297
 
        self._first_byte_time = None
298
260
        self._fraction = 0
299
261
        # force the progress bar to be off, as at the moment it doesn't 
300
262
        # correspond reliably to overall command progress
301
263
        self.enable_bar = False
302
264
 
303
 
    def _avail_width(self):
304
 
        # we need one extra space for terminals that wrap on last char
305
 
        w = osutils.terminal_width() 
306
 
        if w is None:
307
 
            return None
308
 
        else:
309
 
            return w - 1
310
 
 
311
265
    def _show_line(self, s):
312
266
        # sys.stderr.write("progress %r\n" % s)
313
 
        width = self._avail_width()
 
267
        width = osutils.terminal_width()
314
268
        if width is not None:
 
269
            # we need one extra space for terminals that wrap on last char
 
270
            width = width - 1
315
271
            s = '%-*.*s' % (width, width, s)
316
272
        self._term_file.write('\r' + s + '\r')
317
273
 
345
301
            markers = int(round(float(cols) * completion_fraction)) - 1
346
302
            bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
347
303
            return bar_str
348
 
        elif (self._last_task is None) or self._last_task.show_spinner:
 
304
        elif self._last_task.show_spinner:
349
305
            # The last task wanted just a spinner, no bar
350
306
            spin_str =  r'/-\|'[self._spin_pos % 4]
351
307
            self._spin_pos += 1
354
310
            return ''
355
311
 
356
312
    def _format_task(self, task):
357
 
        """Format task-specific parts of progress bar.
358
 
 
359
 
        :returns: (text_part, counter_part) both unicode strings.
360
 
        """
361
313
        if not task.show_count:
362
314
            s = ''
363
315
        elif task.current_cnt is not None and task.total_cnt is not None:
373
325
            t = t._parent_task
374
326
            if t.msg:
375
327
                m = t.msg + ':' + m
376
 
        return m, s
 
328
        return m + s
377
329
 
378
330
    def _render_line(self):
379
331
        bar_string = self._render_bar()
380
332
        if self._last_task:
381
 
            task_part, counter_part = self._format_task(self._last_task)
 
333
            task_msg = self._format_task(self._last_task)
382
334
        else:
383
 
            task_part = counter_part = ''
 
335
            task_msg = ''
384
336
        if self._last_task and not self._last_task.show_transport_activity:
385
337
            trans = ''
386
338
        else:
387
339
            trans = self._last_transport_msg
388
 
        # the bar separates the transport activity from the message, so even
389
 
        # if there's no bar or spinner, we must show something if both those
390
 
        # fields are present
391
 
        if (task_part or trans) and not bar_string:
392
 
            bar_string = '| '
393
 
        # preferentially truncate the task message if we don't have enough
394
 
        # space
395
 
        avail_width = self._avail_width()
396
 
        if avail_width is not None:
397
 
            # if terminal avail_width is unknown, don't truncate
398
 
            current_len = len(bar_string) + len(trans) + len(task_part) + len(counter_part)
399
 
            gap = current_len - avail_width
400
 
            if gap > 0:
401
 
                task_part = task_part[:-gap-2] + '..'
402
 
        s = trans + bar_string + task_part + counter_part
403
 
        if avail_width is not None:
404
 
            if len(s) < avail_width:
405
 
                s = s.ljust(avail_width)
406
 
            elif len(s) > avail_width:
407
 
                s = s[:avail_width]
408
 
        return s
 
340
            if trans:
 
341
                trans += ' | '
 
342
        return (bar_string + trans + task_msg)
409
343
 
410
344
    def _repaint(self):
411
345
        s = self._render_line()
435
369
        This may update a progress bar, spinner, or similar display.
436
370
        By default it does nothing.
437
371
        """
438
 
        # XXX: there should be a transport activity model, and that too should
439
 
        #      be seen by the progress view, rather than being poked in here.
 
372
        # XXX: Probably there should be a transport activity model, and that
 
373
        # too should be seen by the progress view, rather than being poked in
 
374
        # here.
 
375
        if not self._have_output:
 
376
            # As a workaround for <https://launchpad.net/bugs/321935> we only
 
377
            # show transport activity when there's already a progress bar
 
378
            # shown, which time the application code is expected to know to
 
379
            # clear off the progress bar when it's going to send some other
 
380
            # output.  Eventually it would be nice to have that automatically
 
381
            # synchronized.
 
382
            return
440
383
        self._total_byte_count += byte_count
441
384
        self._bytes_since_update += byte_count
442
 
        if self._first_byte_time is None:
443
 
            # Note that this isn't great, as technically it should be the time
444
 
            # when the bytes started transferring, not when they completed.
445
 
            # However, we usually start with a small request anyway.
446
 
            self._first_byte_time = time.time()
447
 
        if direction in self._bytes_by_direction:
448
 
            self._bytes_by_direction[direction] += byte_count
449
 
        else:
450
 
            self._bytes_by_direction['unknown'] += byte_count
451
 
        if 'no_activity' in debug.debug_flags:
452
 
            # Can be used as a workaround if
453
 
            # <https://launchpad.net/bugs/321935> reappears and transport
454
 
            # activity is cluttering other output.  However, thanks to
455
 
            # TextUIOutputStream this shouldn't be a problem any more.
456
 
            return
457
385
        now = time.time()
458
386
        if self._total_byte_count < 2000:
459
387
            # a little resistance at first, so it doesn't stay stuck at 0
464
392
        elif now >= (self._transport_update_time + 0.5):
465
393
            # guard against clock stepping backwards, and don't update too
466
394
            # often
467
 
            rate = (self._bytes_since_update
468
 
                    / (now - self._transport_update_time))
469
 
            # using base-10 units (see HACKING.txt).
470
 
            msg = ("%6dkB %5dkB/s " %
471
 
                    (self._total_byte_count / 1000, int(rate) / 1000,))
 
395
            rate = self._bytes_since_update / (now - self._transport_update_time)
 
396
            msg = ("%6dKB %5dKB/s" %
 
397
                    (self._total_byte_count>>10, int(rate)>>10,))
472
398
            self._transport_update_time = now
473
399
            self._last_repaint = now
474
400
            self._bytes_since_update = 0
475
401
            self._last_transport_msg = msg
476
402
            self._repaint()
477
403
 
478
 
    def _format_bytes_by_direction(self):
479
 
        if self._first_byte_time is None:
480
 
            bps = 0.0
481
 
        else:
482
 
            transfer_time = time.time() - self._first_byte_time
483
 
            if transfer_time < 0.001:
484
 
                transfer_time = 0.001
485
 
            bps = self._total_byte_count / transfer_time
486
 
 
487
 
        # using base-10 units (see HACKING.txt).
488
 
        msg = ('Transferred: %.0fkB'
489
 
               ' (%.1fkB/s r:%.0fkB w:%.0fkB'
490
 
               % (self._total_byte_count / 1000.,
491
 
                  bps / 1000.,
492
 
                  self._bytes_by_direction['read'] / 1000.,
493
 
                  self._bytes_by_direction['write'] / 1000.,
494
 
                 ))
495
 
        if self._bytes_by_direction['unknown'] > 0:
496
 
            msg += ' u:%.0fkB)' % (
497
 
                self._bytes_by_direction['unknown'] / 1000.
498
 
                )
499
 
        else:
500
 
            msg += ')'
501
 
        return msg
502
 
 
503
 
    def log_transport_activity(self, display=False):
504
 
        msg = self._format_bytes_by_direction()
505
 
        trace.mutter(msg)
506
 
        if display and self._total_byte_count > 0:
507
 
            self.clear()
508
 
            self._term_file.write(msg + '\n')
509
 
 
510
404
 
511
405
class TextUIOutputStream(object):
512
406
    """Decorates an output stream so that the terminal is cleared before writing.