/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: John Arbash Meinel
  • Date: 2010-01-12 22:36:23 UTC
  • mfrom: (4953 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4955.
  • Revision ID: john@arbash-meinel.com-20100112223623-836x5mou0gm5vsep
merge bzr.dev 4953 to clean up the ui factory issues

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)
257
264
        self._last_task = None
258
265
        self._total_byte_count = 0
259
266
        self._bytes_since_update = 0
 
267
        self._bytes_by_direction = {'unknown': 0, 'read': 0, 'write': 0}
 
268
        self._first_byte_time = None
260
269
        self._fraction = 0
261
270
        # force the progress bar to be off, as at the moment it doesn't 
262
271
        # correspond reliably to overall command progress
301
310
            markers = int(round(float(cols) * completion_fraction)) - 1
302
311
            bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
303
312
            return bar_str
304
 
        elif self._last_task.show_spinner:
 
313
        elif (self._last_task is None) or self._last_task.show_spinner:
305
314
            # The last task wanted just a spinner, no bar
306
315
            spin_str =  r'/-\|'[self._spin_pos % 4]
307
316
            self._spin_pos += 1
369
378
        This may update a progress bar, spinner, or similar display.
370
379
        By default it does nothing.
371
380
        """
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
 
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.
383
383
        self._total_byte_count += byte_count
384
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
385
400
        now = time.time()
386
401
        if self._total_byte_count < 2000:
387
402
            # a little resistance at first, so it doesn't stay stuck at 0
401
416
            self._last_transport_msg = msg
402
417
            self._repaint()
403
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
 
404
450
 
405
451
class TextUIOutputStream(object):
406
452
    """Decorates an output stream so that the terminal is cleared before writing.