/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: Andrew Bennetts
  • Date: 2010-01-08 00:05:01 UTC
  • mfrom: (4938 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4948.
  • Revision ID: andrew.bennetts@canonical.com-20100108000501-8fj5j5ub6j5bd3es
MergeĀ lp:bzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
    progress,
33
33
    osutils,
34
34
    symbol_versioning,
 
35
    trace,
35
36
    )
36
37
 
37
38
""")
203
204
        self._progress_view.show_transport_activity(transport,
204
205
            direction, byte_count)
205
206
 
 
207
    def log_transport_activity(self, display=False):
 
208
        """See UIFactory.log_transport_activity()"""
 
209
        log = getattr(self._progress_view, 'log_transport_activity', None)
 
210
        if log is not None:
 
211
            log(display=display)
 
212
 
206
213
    def show_error(self, msg):
207
214
        self.clear_term()
208
215
        self.stderr.write("bzr: error: %s\n" % msg)
248
255
        self._term_file = term_file
249
256
        # true when there's output on the screen we may need to clear
250
257
        self._have_output = False
251
 
        # XXX: We could listen for SIGWINCH and update the terminal width...
252
 
        # https://launchpad.net/bugs/316357
253
 
        self._width = osutils.terminal_width()
254
258
        self._last_transport_msg = ''
255
259
        self._spin_pos = 0
256
260
        # time we last repainted the screen
260
264
        self._last_task = None
261
265
        self._total_byte_count = 0
262
266
        self._bytes_since_update = 0
 
267
        self._bytes_by_direction = {'unknown': 0, 'read': 0, 'write': 0}
 
268
        self._first_byte_time = None
263
269
        self._fraction = 0
264
270
        # force the progress bar to be off, as at the moment it doesn't 
265
271
        # correspond reliably to overall command progress
267
273
 
268
274
    def _show_line(self, s):
269
275
        # sys.stderr.write("progress %r\n" % s)
270
 
        if self._width is not None:
271
 
            n = self._width - 1
272
 
            s = '%-*.*s' % (n, n, s)
 
276
        width = osutils.terminal_width()
 
277
        if width is not None:
 
278
            # we need one extra space for terminals that wrap on last char
 
279
            width = width - 1
 
280
            s = '%-*.*s' % (width, width, s)
273
281
        self._term_file.write('\r' + s + '\r')
274
282
 
275
283
    def clear(self):
302
310
            markers = int(round(float(cols) * completion_fraction)) - 1
303
311
            bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
304
312
            return bar_str
305
 
        elif self._last_task.show_spinner:
 
313
        elif (self._last_task is None) or self._last_task.show_spinner:
306
314
            # The last task wanted just a spinner, no bar
307
315
            spin_str =  r'/-\|'[self._spin_pos % 4]
308
316
            self._spin_pos += 1
370
378
        This may update a progress bar, spinner, or similar display.
371
379
        By default it does nothing.
372
380
        """
373
 
        # XXX: Probably there should be a transport activity model, and that
374
 
        # too should be seen by the progress view, rather than being poked in
375
 
        # here.
376
 
        if not self._have_output:
377
 
            # As a workaround for <https://launchpad.net/bugs/321935> we only
378
 
            # show transport activity when there's already a progress bar
379
 
            # shown, which time the application code is expected to know to
380
 
            # clear off the progress bar when it's going to send some other
381
 
            # output.  Eventually it would be nice to have that automatically
382
 
            # synchronized.
383
 
            return
 
381
        # XXX: there should be a transport activity model, and that too should
 
382
        #      be seen by the progress view, rather than being poked in here.
384
383
        self._total_byte_count += byte_count
385
384
        self._bytes_since_update += byte_count
 
385
        if self._first_byte_time is None:
 
386
            # Note that this isn't great, as technically it should be the time
 
387
            # when the bytes started transferring, not when they completed.
 
388
            # However, we usually start with a small request anyway.
 
389
            self._first_byte_time = time.time()
 
390
        if direction in self._bytes_by_direction:
 
391
            self._bytes_by_direction[direction] += byte_count
 
392
        else:
 
393
            self._bytes_by_direction['unknown'] += byte_count
 
394
        if 'no_activity' in debug.debug_flags:
 
395
            # Can be used as a workaround if
 
396
            # <https://launchpad.net/bugs/321935> reappears and transport
 
397
            # activity is cluttering other output.  However, thanks to
 
398
            # TextUIOutputStream this shouldn't be a problem any more.
 
399
            return
386
400
        now = time.time()
387
401
        if self._total_byte_count < 2000:
388
402
            # a little resistance at first, so it doesn't stay stuck at 0
402
416
            self._last_transport_msg = msg
403
417
            self._repaint()
404
418
 
 
419
    def _format_bytes_by_direction(self):
 
420
        if self._first_byte_time is None:
 
421
            bps = 0.0
 
422
        else:
 
423
            transfer_time = time.time() - self._first_byte_time
 
424
            if transfer_time < 0.001:
 
425
                transfer_time = 0.001
 
426
            bps = self._total_byte_count / transfer_time
 
427
 
 
428
        msg = ('Transferred: %.0fKiB'
 
429
               ' (%.1fK/s r:%.0fK w:%.0fK'
 
430
               % (self._total_byte_count / 1024.,
 
431
                  bps / 1024.,
 
432
                  self._bytes_by_direction['read'] / 1024.,
 
433
                  self._bytes_by_direction['write'] / 1024.,
 
434
                 ))
 
435
        if self._bytes_by_direction['unknown'] > 0:
 
436
            msg += ' u:%.0fK)' % (
 
437
                self._bytes_by_direction['unknown'] / 1024.
 
438
                )
 
439
        else:
 
440
            msg += ')'
 
441
        return msg
 
442
 
 
443
    def log_transport_activity(self, display=False):
 
444
        msg = self._format_bytes_by_direction()
 
445
        trace.mutter(msg)
 
446
        if display and self._total_byte_count > 0:
 
447
            self.clear()
 
448
            self._term_file.write(msg + '\n')
 
449
 
405
450
 
406
451
class TextUIOutputStream(object):
407
452
    """Decorates an output stream so that the terminal is cleared before writing.