~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

  • Committer: Martin von Gagern
  • Date: 2011-09-19 08:49:15 UTC
  • mto: This revision was merged to the branch mainline in revision 6148.
  • Revision ID: martin.vgagern@gmx.net-20110919084915-vbumflqq3xqm1vez
Avoid using deprecated api in the unit tests for bzrlib.missing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
23
21
import os
24
22
import sys
25
23
import time
26
 
import warnings
27
24
 
28
25
from bzrlib.lazy_import import lazy_import
29
26
lazy_import(globals(), """
 
27
import codecs
 
28
import getpass
 
29
import warnings
 
30
 
30
31
from bzrlib import (
31
32
    debug,
32
33
    progress,
33
34
    osutils,
34
 
    symbol_versioning,
35
35
    trace,
36
36
    )
37
37
 
38
38
""")
39
39
 
40
 
from bzrlib.osutils import watch_sigwinch
41
 
 
42
40
from bzrlib.ui import (
43
41
    UIFactory,
44
42
    NullProgressView,
62
60
        self.stderr = stderr
63
61
        # paints progress, network activity, etc
64
62
        self._progress_view = self.make_progress_view()
65
 
        # hook up the signals to watch for terminal size changes
66
 
        watch_sigwinch()
67
63
 
68
64
    def be_quiet(self, state):
69
65
        if state and not self._quiet:
118
114
                password = password[:-1]
119
115
        return password
120
116
 
121
 
    def get_password(self, prompt='', **kwargs):
 
117
    def get_password(self, prompt=u'', **kwargs):
122
118
        """Prompt the user for a password.
123
119
 
124
120
        :param prompt: The prompt to present the user
157
153
        """Construct and return a new ProgressView subclass for this UI.
158
154
        """
159
155
        # with --quiet, never any progress view
160
 
        # <https://bugs.edge.launchpad.net/bzr/+bug/320035>.  Otherwise if the
 
156
        # <https://bugs.launchpad.net/bzr/+bug/320035>.  Otherwise if the
161
157
        # user specifically requests either text or no progress bars, always
162
158
        # do that.  otherwise, guess based on $TERM and tty presence.
163
159
        if self.is_quiet():
202
198
        :param kwargs: Dictionary of arguments to insert into the prompt,
203
199
            to allow UIs to reformat the prompt.
204
200
        """
 
201
        if type(prompt) != unicode:
 
202
            raise ValueError("prompt %r not a unicode string" % prompt)
205
203
        if kwargs:
206
204
            # See <https://launchpad.net/bugs/365891>
207
205
            prompt = prompt % kwargs
233
231
 
234
232
    def show_warning(self, msg):
235
233
        self.clear_term()
 
234
        if isinstance(msg, unicode):
 
235
            te = osutils.get_terminal_encoding()
 
236
            msg = msg.encode(te, 'replace')
236
237
        self.stderr.write("bzr: warning: %s\n" % msg)
237
238
 
238
239
    def _progress_updated(self, task):
301
302
        # correspond reliably to overall command progress
302
303
        self.enable_bar = False
303
304
 
 
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
 
304
313
    def _show_line(self, s):
305
314
        # sys.stderr.write("progress %r\n" % s)
306
 
        width = osutils.terminal_width()
 
315
        width = self._avail_width()
307
316
        if width is not None:
308
 
            # we need one extra space for terminals that wrap on last char
309
 
            width = width - 1
310
317
            s = '%-*.*s' % (width, width, s)
311
318
        self._term_file.write('\r' + s + '\r')
312
319
 
349
356
            return ''
350
357
 
351
358
    def _format_task(self, task):
 
359
        """Format task-specific parts of progress bar.
 
360
 
 
361
        :returns: (text_part, counter_part) both unicode strings.
 
362
        """
352
363
        if not task.show_count:
353
364
            s = ''
354
365
        elif task.current_cnt is not None and task.total_cnt is not None:
364
375
            t = t._parent_task
365
376
            if t.msg:
366
377
                m = t.msg + ':' + m
367
 
        return m + s
 
378
        return m, s
368
379
 
369
380
    def _render_line(self):
370
381
        bar_string = self._render_bar()
371
382
        if self._last_task:
372
 
            task_msg = self._format_task(self._last_task)
 
383
            task_part, counter_part = self._format_task(self._last_task)
373
384
        else:
374
 
            task_msg = ''
 
385
            task_part = counter_part = ''
375
386
        if self._last_task and not self._last_task.show_transport_activity:
376
387
            trans = ''
377
388
        else:
378
389
            trans = self._last_transport_msg
379
 
            if trans:
380
 
                trans += ' | '
381
 
        return (bar_string + trans + task_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
382
411
 
383
412
    def _repaint(self):
384
413
        s = self._render_line()
440
469
            rate = (self._bytes_since_update
441
470
                    / (now - self._transport_update_time))
442
471
            # using base-10 units (see HACKING.txt).
443
 
            msg = ("%6dkB %5dkB/s" %
 
472
            msg = ("%6dkB %5dkB/s " %
444
473
                    (self._total_byte_count / 1000, int(rate) / 1000,))
445
474
            self._transport_update_time = now
446
475
            self._last_repaint = now