~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-05-21 13:36:51 UTC
  • mfrom: (5243.2.1 readdir_cleanup)
  • Revision ID: pqm@pqm.ubuntu.com-20100521133651-p62dndo2giy5ls21
(lifeless) Some cleanups to the readdir pyrex code for a little efficiency
 and to avoid compile warnings. (John A Meinel)

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
 
38
38
""")
39
39
 
 
40
from bzrlib.osutils import watch_sigwinch
 
41
 
40
42
from bzrlib.ui import (
41
43
    UIFactory,
42
44
    NullProgressView,
60
62
        self.stderr = stderr
61
63
        # paints progress, network activity, etc
62
64
        self._progress_view = self.make_progress_view()
 
65
        # hook up the signals to watch for terminal size changes
 
66
        watch_sigwinch()
63
67
 
64
68
    def be_quiet(self, state):
65
69
        if state and not self._quiet:
114
118
                password = password[:-1]
115
119
        return password
116
120
 
117
 
    def get_password(self, prompt=u'', **kwargs):
 
121
    def get_password(self, prompt='', **kwargs):
118
122
        """Prompt the user for a password.
119
123
 
120
124
        :param prompt: The prompt to present the user
198
202
        :param kwargs: Dictionary of arguments to insert into the prompt,
199
203
            to allow UIs to reformat the prompt.
200
204
        """
201
 
        if type(prompt) != unicode:
202
 
            raise ValueError("prompt %r not a unicode string" % prompt)
203
205
        if kwargs:
204
206
            # See <https://launchpad.net/bugs/365891>
205
207
            prompt = prompt % kwargs
302
304
        # correspond reliably to overall command progress
303
305
        self.enable_bar = False
304
306
 
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
307
    def _show_line(self, s):
314
308
        # sys.stderr.write("progress %r\n" % s)
315
 
        width = self._avail_width()
 
309
        width = osutils.terminal_width()
316
310
        if width is not None:
 
311
            # we need one extra space for terminals that wrap on last char
 
312
            width = width - 1
317
313
            s = '%-*.*s' % (width, width, s)
318
314
        self._term_file.write('\r' + s + '\r')
319
315
 
356
352
            return ''
357
353
 
358
354
    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
355
        if not task.show_count:
364
356
            s = ''
365
357
        elif task.current_cnt is not None and task.total_cnt is not None:
375
367
            t = t._parent_task
376
368
            if t.msg:
377
369
                m = t.msg + ':' + m
378
 
        return m, s
 
370
        return m + s
379
371
 
380
372
    def _render_line(self):
381
373
        bar_string = self._render_bar()
382
374
        if self._last_task:
383
 
            task_part, counter_part = self._format_task(self._last_task)
 
375
            task_msg = self._format_task(self._last_task)
384
376
        else:
385
 
            task_part = counter_part = ''
 
377
            task_msg = ''
386
378
        if self._last_task and not self._last_task.show_transport_activity:
387
379
            trans = ''
388
380
        else:
389
381
            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
 
382
            if trans:
 
383
                trans += ' | '
 
384
        return (bar_string + trans + task_msg)
411
385
 
412
386
    def _repaint(self):
413
387
        s = self._render_line()
469
443
            rate = (self._bytes_since_update
470
444
                    / (now - self._transport_update_time))
471
445
            # using base-10 units (see HACKING.txt).
472
 
            msg = ("%6dkB %5dkB/s " %
 
446
            msg = ("%6dkB %5dkB/s" %
473
447
                    (self._total_byte_count / 1000, int(rate) / 1000,))
474
448
            self._transport_update_time = now
475
449
            self._last_repaint = now