~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

  • Committer: Martin Pool
  • Date: 2010-01-15 04:17:57 UTC
  • mto: This revision was merged to the branch mainline in revision 5019.
  • Revision ID: mbp@sourcefrog.net-20100115041757-cd8pu9o5a511jc8q
Rip out most remaining uses of DummyProgressBar

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 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
18
18
"""Text UI, write output to the console.
19
19
"""
20
20
 
 
21
import codecs
 
22
import getpass
21
23
import os
22
24
import sys
23
25
import time
 
26
import warnings
24
27
 
25
28
from bzrlib.lazy_import import lazy_import
26
29
lazy_import(globals(), """
27
 
import codecs
28
 
import getpass
29
 
import warnings
30
 
 
31
30
from bzrlib import (
32
31
    debug,
33
32
    progress,
34
33
    osutils,
 
34
    symbol_versioning,
35
35
    trace,
36
36
    )
37
37
 
60
60
        self.stderr = stderr
61
61
        # paints progress, network activity, etc
62
62
        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
 
 
 
63
        
70
64
    def clear_term(self):
71
65
        """Prepare the terminal for output.
72
66
 
114
108
                password = password[:-1]
115
109
        return password
116
110
 
117
 
    def get_password(self, prompt=u'', **kwargs):
 
111
    def get_password(self, prompt='', **kwargs):
118
112
        """Prompt the user for a password.
119
113
 
120
114
        :param prompt: The prompt to present the user
152
146
    def make_progress_view(self):
153
147
        """Construct and return a new ProgressView subclass for this UI.
154
148
        """
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':
 
149
        # if the user specifically requests either text or no progress bars,
 
150
        # always do that.  otherwise, guess based on $TERM and tty presence.
 
151
        if os.environ.get('BZR_PROGRESS_BAR') == 'text':
162
152
            return TextProgressView(self.stderr)
163
153
        elif os.environ.get('BZR_PROGRESS_BAR') == 'none':
164
154
            return NullProgressView()
198
188
        :param kwargs: Dictionary of arguments to insert into the prompt,
199
189
            to allow UIs to reformat the prompt.
200
190
        """
201
 
        if type(prompt) != unicode:
202
 
            raise ValueError("prompt %r not a unicode string" % prompt)
203
191
        if kwargs:
204
192
            # See <https://launchpad.net/bugs/365891>
205
193
            prompt = prompt % kwargs
231
219
 
232
220
    def show_warning(self, msg):
233
221
        self.clear_term()
234
 
        if isinstance(msg, unicode):
235
 
            te = osutils.get_terminal_encoding()
236
 
            msg = msg.encode(te, 'replace')
237
222
        self.stderr.write("bzr: warning: %s\n" % msg)
238
223
 
239
224
    def _progress_updated(self, task):
243
228
            warnings.warn("%r updated but no tasks are active" %
244
229
                (task,))
245
230
        elif task != self._task_stack[-1]:
246
 
            # We used to check it was the top task, but it's hard to always
247
 
            # get this right and it's not necessarily useful: any actual
248
 
            # problems will be evident in use
249
 
            #warnings.warn("%r is not the top progress task %r" %
250
 
            #     (task, self._task_stack[-1]))
251
 
            pass
 
231
            warnings.warn("%r is not the top progress task %r" %
 
232
                (task, self._task_stack[-1]))
252
233
        self._progress_view.show_progress(task)
253
234
 
254
235
    def _progress_all_finished(self):
255
236
        self._progress_view.clear()
256
237
 
257
 
    def show_user_warning(self, warning_id, **message_args):
258
 
        """Show a text message to the user.
259
 
 
260
 
        Explicitly not for warnings about bzr apis, deprecations or internals.
261
 
        """
262
 
        # eventually trace.warning should migrate here, to avoid logging and
263
 
        # be easier to test; that has a lot of test fallout so for now just
264
 
        # new code can call this
265
 
        if warning_id not in self.suppressed_warnings:
266
 
            self.stderr.write(self.format_user_warning(warning_id, message_args) +
267
 
                '\n')
268
 
 
269
238
 
270
239
class TextProgressView(object):
271
240
    """Display of progress bar and other information on a tty.
302
271
        # correspond reliably to overall command progress
303
272
        self.enable_bar = False
304
273
 
305
 
    def _avail_width(self):
306
 
        # we need one extra space for terminals that wrap on last char
307
 
        w = osutils.terminal_width() 
308
 
        if w is None:
309
 
            return None
310
 
        else:
311
 
            return w - 1
312
 
 
313
274
    def _show_line(self, s):
314
275
        # sys.stderr.write("progress %r\n" % s)
315
 
        width = self._avail_width()
 
276
        width = osutils.terminal_width()
316
277
        if width is not None:
 
278
            # we need one extra space for terminals that wrap on last char
 
279
            width = width - 1
317
280
            s = '%-*.*s' % (width, width, s)
318
281
        self._term_file.write('\r' + s + '\r')
319
282
 
356
319
            return ''
357
320
 
358
321
    def _format_task(self, task):
359
 
        """Format task-specific parts of progress bar.
360
 
 
361
 
        :returns: (text_part, counter_part) both unicode strings.
362
 
        """
363
322
        if not task.show_count:
364
323
            s = ''
365
324
        elif task.current_cnt is not None and task.total_cnt is not None:
375
334
            t = t._parent_task
376
335
            if t.msg:
377
336
                m = t.msg + ':' + m
378
 
        return m, s
 
337
        return m + s
379
338
 
380
339
    def _render_line(self):
381
340
        bar_string = self._render_bar()
382
341
        if self._last_task:
383
 
            task_part, counter_part = self._format_task(self._last_task)
 
342
            task_msg = self._format_task(self._last_task)
384
343
        else:
385
 
            task_part = counter_part = ''
 
344
            task_msg = ''
386
345
        if self._last_task and not self._last_task.show_transport_activity:
387
346
            trans = ''
388
347
        else:
389
348
            trans = self._last_transport_msg
390
 
        # the bar separates the transport activity from the message, so even
391
 
        # if there's no bar or spinner, we must show something if both those
392
 
        # fields are present
393
 
        if (task_part or trans) and not bar_string:
394
 
            bar_string = '| '
395
 
        # preferentially truncate the task message if we don't have enough
396
 
        # space
397
 
        avail_width = self._avail_width()
398
 
        if avail_width is not None:
399
 
            # if terminal avail_width is unknown, don't truncate
400
 
            current_len = len(bar_string) + len(trans) + len(task_part) + len(counter_part)
401
 
            gap = current_len - avail_width
402
 
            if gap > 0:
403
 
                task_part = task_part[:-gap-2] + '..'
404
 
        s = trans + bar_string + task_part + counter_part
405
 
        if avail_width is not None:
406
 
            if len(s) < avail_width:
407
 
                s = s.ljust(avail_width)
408
 
            elif len(s) > avail_width:
409
 
                s = s[:avail_width]
410
 
        return s
 
349
            if trans:
 
350
                trans += ' | '
 
351
        return (bar_string + trans + task_msg)
411
352
 
412
353
    def _repaint(self):
413
354
        s = self._render_line()
466
407
        elif now >= (self._transport_update_time + 0.5):
467
408
            # guard against clock stepping backwards, and don't update too
468
409
            # often
469
 
            rate = (self._bytes_since_update
470
 
                    / (now - self._transport_update_time))
471
 
            # using base-10 units (see HACKING.txt).
472
 
            msg = ("%6dkB %5dkB/s " %
473
 
                    (self._total_byte_count / 1000, int(rate) / 1000,))
 
410
            rate = self._bytes_since_update / (now - self._transport_update_time)
 
411
            msg = ("%6dKB %5dKB/s" %
 
412
                    (self._total_byte_count>>10, int(rate)>>10,))
474
413
            self._transport_update_time = now
475
414
            self._last_repaint = now
476
415
            self._bytes_since_update = 0
486
425
                transfer_time = 0.001
487
426
            bps = self._total_byte_count / transfer_time
488
427
 
489
 
        # using base-10 units (see HACKING.txt).
490
 
        msg = ('Transferred: %.0fkB'
491
 
               ' (%.1fkB/s r:%.0fkB w:%.0fkB'
492
 
               % (self._total_byte_count / 1000.,
493
 
                  bps / 1000.,
494
 
                  self._bytes_by_direction['read'] / 1000.,
495
 
                  self._bytes_by_direction['write'] / 1000.,
 
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.,
496
434
                 ))
497
435
        if self._bytes_by_direction['unknown'] > 0:
498
 
            msg += ' u:%.0fkB)' % (
499
 
                self._bytes_by_direction['unknown'] / 1000.
 
436
            msg += ' u:%.0fK)' % (
 
437
                self._bytes_by_direction['unknown'] / 1024.
500
438
                )
501
439
        else:
502
440
            msg += ')'