~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/text.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-02-11 04:02:41 UTC
  • mfrom: (5017.2.2 tariff)
  • Revision ID: pqm@pqm.ubuntu.com-20100211040241-w6n021dz0uus341n
(mbp) add import-tariff tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-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
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
 
 
 
63
        
64
64
    def be_quiet(self, state):
65
65
        if state and not self._quiet:
66
66
            self.clear_term()
114
114
                password = password[:-1]
115
115
        return password
116
116
 
117
 
    def get_password(self, prompt=u'', **kwargs):
 
117
    def get_password(self, prompt='', **kwargs):
118
118
        """Prompt the user for a password.
119
119
 
120
120
        :param prompt: The prompt to present the user
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():
198
198
        :param kwargs: Dictionary of arguments to insert into the prompt,
199
199
            to allow UIs to reformat the prompt.
200
200
        """
201
 
        if type(prompt) != unicode:
202
 
            raise ValueError("prompt %r not a unicode string" % prompt)
203
201
        if kwargs:
204
202
            # See <https://launchpad.net/bugs/365891>
205
203
            prompt = prompt % kwargs
231
229
 
232
230
    def show_warning(self, msg):
233
231
        self.clear_term()
234
 
        if isinstance(msg, unicode):
235
 
            te = osutils.get_terminal_encoding()
236
 
            msg = msg.encode(te, 'replace')
237
232
        self.stderr.write("bzr: warning: %s\n" % msg)
238
233
 
239
234
    def _progress_updated(self, task):
254
249
    def _progress_all_finished(self):
255
250
        self._progress_view.clear()
256
251
 
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
252
 
270
253
class TextProgressView(object):
271
254
    """Display of progress bar and other information on a tty.
302
285
        # correspond reliably to overall command progress
303
286
        self.enable_bar = False
304
287
 
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
288
    def _show_line(self, s):
314
289
        # sys.stderr.write("progress %r\n" % s)
315
 
        width = self._avail_width()
 
290
        width = osutils.terminal_width()
316
291
        if width is not None:
 
292
            # we need one extra space for terminals that wrap on last char
 
293
            width = width - 1
317
294
            s = '%-*.*s' % (width, width, s)
318
295
        self._term_file.write('\r' + s + '\r')
319
296
 
356
333
            return ''
357
334
 
358
335
    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
336
        if not task.show_count:
364
337
            s = ''
365
338
        elif task.current_cnt is not None and task.total_cnt is not None:
375
348
            t = t._parent_task
376
349
            if t.msg:
377
350
                m = t.msg + ':' + m
378
 
        return m, s
 
351
        return m + s
379
352
 
380
353
    def _render_line(self):
381
354
        bar_string = self._render_bar()
382
355
        if self._last_task:
383
 
            task_part, counter_part = self._format_task(self._last_task)
 
356
            task_msg = self._format_task(self._last_task)
384
357
        else:
385
 
            task_part = counter_part = ''
 
358
            task_msg = ''
386
359
        if self._last_task and not self._last_task.show_transport_activity:
387
360
            trans = ''
388
361
        else:
389
362
            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
 
363
            if trans:
 
364
                trans += ' | '
 
365
        return (bar_string + trans + task_msg)
411
366
 
412
367
    def _repaint(self):
413
368
        s = self._render_line()
469
424
            rate = (self._bytes_since_update
470
425
                    / (now - self._transport_update_time))
471
426
            # using base-10 units (see HACKING.txt).
472
 
            msg = ("%6dkB %5dkB/s " %
 
427
            msg = ("%6dkB %5dkB/s" %
473
428
                    (self._total_byte_count / 1000, int(rate) / 1000,))
474
429
            self._transport_update_time = now
475
430
            self._last_repaint = now