/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: Vincent Ladeuil
  • Date: 2010-01-25 17:48:22 UTC
  • mto: (4987.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4988.
  • Revision ID: v.ladeuil+lp@free.fr-20100125174822-nce4l19sbwx83jvq
Deploying the new overrideAttr facility further reduces the complexity
and make the code clearer.

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, 2010 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
60
60
        self.stderr = stderr
61
61
        # paints progress, network activity, etc
62
62
        self._progress_view = self.make_progress_view()
63
 
 
 
63
        
64
64
    def be_quiet(self, state):
65
65
        if state and not self._quiet:
66
66
            self.clear_term()
153
153
        """Construct and return a new ProgressView subclass for this UI.
154
154
        """
155
155
        # with --quiet, never any progress view
156
 
        # <https://bugs.launchpad.net/bzr/+bug/320035>.  Otherwise if the
 
156
        # <https://bugs.edge.launchpad.net/bzr/+bug/320035>.  Otherwise if the
157
157
        # user specifically requests either text or no progress bars, always
158
158
        # do that.  otherwise, guess based on $TERM and tty presence.
159
159
        if self.is_quiet():
229
229
 
230
230
    def show_warning(self, msg):
231
231
        self.clear_term()
232
 
        if isinstance(msg, unicode):
233
 
            te = osutils.get_terminal_encoding()
234
 
            msg = msg.encode(te, 'replace')
235
232
        self.stderr.write("bzr: warning: %s\n" % msg)
236
233
 
237
234
    def _progress_updated(self, task):
241
238
            warnings.warn("%r updated but no tasks are active" %
242
239
                (task,))
243
240
        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
 
241
            warnings.warn("%r is not the top progress task %r" %
 
242
                (task, self._task_stack[-1]))
250
243
        self._progress_view.show_progress(task)
251
244
 
252
245
    def _progress_all_finished(self):
253
246
        self._progress_view.clear()
254
247
 
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
248
 
268
249
class TextProgressView(object):
269
250
    """Display of progress bar and other information on a tty.
300
281
        # correspond reliably to overall command progress
301
282
        self.enable_bar = False
302
283
 
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
284
    def _show_line(self, s):
312
285
        # sys.stderr.write("progress %r\n" % s)
313
 
        width = self._avail_width()
 
286
        width = osutils.terminal_width()
314
287
        if width is not None:
 
288
            # we need one extra space for terminals that wrap on last char
 
289
            width = width - 1
315
290
            s = '%-*.*s' % (width, width, s)
316
291
        self._term_file.write('\r' + s + '\r')
317
292
 
354
329
            return ''
355
330
 
356
331
    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
332
        if not task.show_count:
362
333
            s = ''
363
334
        elif task.current_cnt is not None and task.total_cnt is not None:
373
344
            t = t._parent_task
374
345
            if t.msg:
375
346
                m = t.msg + ':' + m
376
 
        return m, s
 
347
        return m + s
377
348
 
378
349
    def _render_line(self):
379
350
        bar_string = self._render_bar()
380
351
        if self._last_task:
381
 
            task_part, counter_part = self._format_task(self._last_task)
 
352
            task_msg = self._format_task(self._last_task)
382
353
        else:
383
 
            task_part = counter_part = ''
 
354
            task_msg = ''
384
355
        if self._last_task and not self._last_task.show_transport_activity:
385
356
            trans = ''
386
357
        else:
387
358
            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
 
359
            if trans:
 
360
                trans += ' | '
 
361
        return (bar_string + trans + task_msg)
409
362
 
410
363
    def _repaint(self):
411
364
        s = self._render_line()
464
417
        elif now >= (self._transport_update_time + 0.5):
465
418
            # guard against clock stepping backwards, and don't update too
466
419
            # 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,))
 
420
            rate = self._bytes_since_update / (now - self._transport_update_time)
 
421
            msg = ("%6dKB %5dKB/s" %
 
422
                    (self._total_byte_count>>10, int(rate)>>10,))
472
423
            self._transport_update_time = now
473
424
            self._last_repaint = now
474
425
            self._bytes_since_update = 0
484
435
                transfer_time = 0.001
485
436
            bps = self._total_byte_count / transfer_time
486
437
 
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.,
 
438
        msg = ('Transferred: %.0fKiB'
 
439
               ' (%.1fK/s r:%.0fK w:%.0fK'
 
440
               % (self._total_byte_count / 1024.,
 
441
                  bps / 1024.,
 
442
                  self._bytes_by_direction['read'] / 1024.,
 
443
                  self._bytes_by_direction['write'] / 1024.,
494
444
                 ))
495
445
        if self._bytes_by_direction['unknown'] > 0:
496
 
            msg += ' u:%.0fkB)' % (
497
 
                self._bytes_by_direction['unknown'] / 1000.
 
446
            msg += ' u:%.0fK)' % (
 
447
                self._bytes_by_direction['unknown'] / 1024.
498
448
                )
499
449
        else:
500
450
            msg += ')'