/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: Martin Pool
  • Date: 2009-03-12 07:09:28 UTC
  • mto: This revision was merged to the branch mainline in revision 4144.
  • Revision ID: mbp@sourcefrog.net-20090312070928-f110be8twil0w4ye
If one ProgressTask has no count, it passes through that of its child

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2008, 2009 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
 
 
19
"""Text UI, write output to the console.
 
20
"""
 
21
 
 
22
import sys
 
23
import time
 
24
import warnings
 
25
 
 
26
from bzrlib.lazy_import import lazy_import
 
27
lazy_import(globals(), """
 
28
import getpass
 
29
 
 
30
from bzrlib import (
 
31
    progress,
 
32
    osutils,
 
33
    symbol_versioning,
 
34
    )
 
35
 
 
36
""")
 
37
 
 
38
from bzrlib.ui import CLIUIFactory
 
39
 
 
40
 
 
41
class TextUIFactory(CLIUIFactory):
 
42
    """A UI factory for Text user interefaces."""
 
43
 
 
44
    def __init__(self,
 
45
                 bar_type=None,
 
46
                 stdin=None,
 
47
                 stdout=None,
 
48
                 stderr=None):
 
49
        """Create a TextUIFactory.
 
50
 
 
51
        :param bar_type: The type of progress bar to create. It defaults to
 
52
                         letting the bzrlib.progress.ProgressBar factory auto
 
53
                         select.   Deprecated.
 
54
        """
 
55
        super(TextUIFactory, self).__init__(stdin=stdin,
 
56
                stdout=stdout, stderr=stderr)
 
57
        if bar_type:
 
58
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 11, 0))
 
59
                % "bar_type parameter")
 
60
        # paints progress, network activity, etc
 
61
        self._progress_view = TextProgressView(self.stderr)
 
62
 
 
63
    def prompt(self, prompt):
 
64
        """Emit prompt on the CLI."""
 
65
        self.stdout.write(prompt)
 
66
 
 
67
    def clear_term(self):
 
68
        """Prepare the terminal for output.
 
69
 
 
70
        This will, clear any progress bars, and leave the cursor at the
 
71
        leftmost position."""
 
72
        # XXX: If this is preparing to write to stdout, but that's for example
 
73
        # directed into a file rather than to the terminal, and the progress
 
74
        # bar _is_ going to the terminal, we shouldn't need
 
75
        # to clear it.  We might need to separately check for the case of
 
76
        self._progress_view.clear()
 
77
 
 
78
    def note(self, msg):
 
79
        """Write an already-formatted message, clearing the progress bar if necessary."""
 
80
        self.clear_term()
 
81
        self.stdout.write(msg + '\n')
 
82
 
 
83
    def report_transport_activity(self, transport, byte_count, direction):
 
84
        """Called by transports as they do IO.
 
85
 
 
86
        This may update a progress bar, spinner, or similar display.
 
87
        By default it does nothing.
 
88
        """
 
89
        self._progress_view.show_transport_activity(byte_count)
 
90
 
 
91
    def _progress_updated(self, task):
 
92
        """A task has been updated and wants to be displayed.
 
93
        """
 
94
        if not self._task_stack:
 
95
            warnings.warn("%r updated but no tasks are active" %
 
96
                (task,))
 
97
        elif task != self._task_stack[-1]:
 
98
            warnings.warn("%r is not the top progress task %r" %
 
99
                (task, self._task_stack[-1]))
 
100
        self._progress_view.show_progress(task)
 
101
 
 
102
    def _progress_all_finished(self):
 
103
        self._progress_view.clear()
 
104
 
 
105
 
 
106
class TextProgressView(object):
 
107
    """Display of progress bar and other information on a tty.
 
108
 
 
109
    This shows one line of text, including possibly a network indicator, spinner,
 
110
    progress bar, message, etc.
 
111
 
 
112
    One instance of this is created and held by the UI, and fed updates when a
 
113
    task wants to be painted.
 
114
 
 
115
    Transports feed data to this through the ui_factory object.
 
116
 
 
117
    The Progress views can comprise a tree with _parent_task pointers, but
 
118
    this only prints the stack from the nominated current task up to the root.
 
119
    """
 
120
 
 
121
    def __init__(self, term_file):
 
122
        self._term_file = term_file
 
123
        # true when there's output on the screen we may need to clear
 
124
        self._have_output = False
 
125
        # XXX: We could listen for SIGWINCH and update the terminal width...
 
126
        self._width = osutils.terminal_width()
 
127
        self._last_transport_msg = ''
 
128
        self._spin_pos = 0
 
129
        # time we last repainted the screen
 
130
        self._last_repaint = 0
 
131
        # time we last got information about transport activity
 
132
        self._transport_update_time = 0
 
133
        self._last_task = None
 
134
        self._total_byte_count = 0
 
135
        self._bytes_since_update = 0
 
136
 
 
137
    def _show_line(self, s):
 
138
        n = self._width - 1
 
139
        self._term_file.write('\r%-*.*s\r' % (n, n, s))
 
140
 
 
141
    def clear(self):
 
142
        if self._have_output:
 
143
            self._show_line('')
 
144
        self._have_output = False
 
145
 
 
146
    def _render_bar(self):
 
147
        # return a string for the progress bar itself
 
148
        if (self._last_task is None) or self._last_task.show_bar:
 
149
            # If there's no task object, we show space for the bar anyhow.
 
150
            # That's because most invocations of bzr will end showing progress
 
151
            # at some point, though perhaps only after doing some initial IO.
 
152
            # It looks better to draw the progress bar initially rather than
 
153
            # to have what looks like an incomplete progress bar.
 
154
            spin_str =  r'/-\|'[self._spin_pos % 4]
 
155
            self._spin_pos += 1
 
156
            cols = 20
 
157
            completion_fraction = \
 
158
                self._last_task._overall_completion_fraction() or 0
 
159
            markers = int(round(float(cols) * completion_fraction)) - 1
 
160
            bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
 
161
            return bar_str
 
162
        elif self._last_task.show_spinner:
 
163
            # The last task wanted just a spinner, no bar
 
164
            spin_str =  r'/-\|'[self._spin_pos % 4]
 
165
            self._spin_pos += 1
 
166
            return spin_str + ' '
 
167
        else:
 
168
            return ''
 
169
 
 
170
    def _format_task(self, task):
 
171
        if not task.show_count:
 
172
            s = ''
 
173
        elif task.current_cnt is not None and task.total_cnt is not None:
 
174
            s = ' %d/%d' % (task.current_cnt, task.total_cnt)
 
175
        elif task.current_cnt is not None:
 
176
            s = ' %d' % (task.current_cnt)
 
177
        else:
 
178
            s = ''
 
179
        # compose all the parent messages
 
180
        t = task
 
181
        m = task.msg
 
182
        while t._parent_task:
 
183
            t = t._parent_task
 
184
            if t.msg:
 
185
                m = t.msg + ':' + m
 
186
        return m + s
 
187
 
 
188
    def _render_line(self):
 
189
        bar_string = self._render_bar()
 
190
        if self._last_task:
 
191
            task_msg = self._format_task(self._last_task)
 
192
        else:
 
193
            task_msg = ''
 
194
        trans = self._last_transport_msg
 
195
        if trans:
 
196
            trans += ' | '
 
197
        return (bar_string + trans + task_msg)
 
198
 
 
199
    def _repaint(self):
 
200
        s = self._render_line()
 
201
        self._show_line(s)
 
202
        self._have_output = True
 
203
 
 
204
    def show_progress(self, task):
 
205
        """Called by the task object when it has changed.
 
206
        
 
207
        :param task: The top task object; its parents are also included 
 
208
            by following links.
 
209
        """
 
210
        self._last_task = task
 
211
        # XXX: Possibly should force update if something important has changed
 
212
        # (like a new task) even if the last update was recent?
 
213
        now = time.time()
 
214
        if now < self._last_repaint + 0.1:
 
215
            return
 
216
        if now > self._transport_update_time + 10:
 
217
            # no recent activity; expire it
 
218
            self._last_transport_msg = ''
 
219
        self._last_repaint = now
 
220
        self._repaint()
 
221
 
 
222
    def show_transport_activity(self, byte_count):
 
223
        """Called by transports as they do IO.
 
224
 
 
225
        This may update a progress bar, spinner, or similar display.
 
226
        By default it does nothing.
 
227
        """
 
228
        # XXX: Probably there should be a transport activity model, and that
 
229
        # too should be seen by the progress view, rather than being poked in
 
230
        # here.
 
231
        self._total_byte_count += byte_count
 
232
        self._bytes_since_update += byte_count
 
233
        now = time.time()
 
234
        if self._transport_update_time is None:
 
235
            self._transport_update_time = now
 
236
        elif now >= (self._transport_update_time + 0.5):
 
237
            # guard against clock stepping backwards, and don't update too
 
238
            # often
 
239
            rate = self._bytes_since_update / (now - self._transport_update_time)
 
240
            msg = ("%6dkB @ %4dkB/s" %
 
241
                (self._total_byte_count>>10, int(rate)>>10,))
 
242
            self._transport_update_time = now
 
243
            self._last_repaint = now
 
244
            self._bytes_since_update = 0
 
245
            self._last_transport_msg = msg
 
246
            self._repaint()
 
247
 
 
248