/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3948.2.2 by Martin Pool
Corrections to finishing progress bars
1
# Copyright (C) 2005, 2008, 2009 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.49.21 by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.49.21 by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.49.21 by John Arbash Meinel
Refactored bzrlib/ui.py into a module with the possibility for multiple ui forms.
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
1996.3.27 by John Arbash Meinel
lazy import getpass in bzrlib.ui.text
22
import sys
3882.7.5 by Martin Pool
Further mockup of transport-based activity indicator.
23
import time
3948.2.2 by Martin Pool
Corrections to finishing progress bars
24
import warnings
1996.3.27 by John Arbash Meinel
lazy import getpass in bzrlib.ui.text
25
26
from bzrlib.lazy_import import lazy_import
27
lazy_import(globals(), """
1185.49.22 by John Arbash Meinel
Added get_password to the UIFactory, using it inside of sftp.py
28
import getpass
1996.3.27 by John Arbash Meinel
lazy import getpass in bzrlib.ui.text
29
30
from bzrlib import (
31
    progress,
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
32
    osutils,
3882.8.8 by Martin Pool
Progress and UI test cleanups
33
    symbol_versioning,
1996.3.27 by John Arbash Meinel
lazy import getpass in bzrlib.ui.text
34
    )
3882.8.8 by Martin Pool
Progress and UI test cleanups
35
1996.3.27 by John Arbash Meinel
lazy import getpass in bzrlib.ui.text
36
""")
37
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
38
from bzrlib.ui import CLIUIFactory
39
40
41
class TextUIFactory(CLIUIFactory):
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
42
    """A UI factory for Text user interefaces."""
43
1692.3.3 by Robert Collins
Get run_bzr in tests to always assign a new, clean ui factory.
44
    def __init__(self,
45
                 bar_type=None,
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
46
                 stdin=None,
1692.3.3 by Robert Collins
Get run_bzr in tests to always assign a new, clean ui factory.
47
                 stdout=None,
48
                 stderr=None):
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
49
        """Create a TextUIFactory.
50
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
51
        :param bar_type: The type of progress bar to create. It defaults to
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
52
                         letting the bzrlib.progress.ProgressBar factory auto
3882.8.3 by Martin Pool
Move display of transport throughput into TextProgressView
53
                         select.   Deprecated.
1681.1.2 by Robert Collins
* bzrlib.ui.text.TextUIFactory now accepts a bar_type parameter which
54
        """
3882.8.11 by Martin Pool
Choose the UIFactory class depending on the terminal capabilities
55
        super(TextUIFactory, self).__init__(stdin=stdin,
56
                stdout=stdout, stderr=stderr)
3882.8.6 by Martin Pool
TextUIFactory ignores and deprecates the bar_type parameter
57
        if bar_type:
58
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 11, 0))
59
                % "bar_type parameter")
3882.7.7 by Martin Pool
Change progress bars to a more MVC style
60
        # paints progress, network activity, etc
3882.8.9 by Martin Pool
Move TextProgressView to ui.text
61
        self._progress_view = TextProgressView(self.stderr)
1594.1.1 by Robert Collins
Introduce new bzr progress bar api. ui_factory.nested_progress_bar.
62
1687.1.4 by Robert Collins
Add bzrlib.ui.ui_factory.get_boolean().
63
    def prompt(self, prompt):
64
        """Emit prompt on the CLI."""
2294.4.1 by Vincent Ladeuil
Add a UIFactory.get_login method, fix tests.
65
        self.stdout.write(prompt)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
66
1558.8.1 by Aaron Bentley
Fix overall progress bar's interaction with 'note' and 'warning'
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."""
3882.7.6 by Martin Pool
Preliminary support for drawing network io into the progress bar
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
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
75
        # to clear it.  We might need to separately check for the case of
3882.7.7 by Martin Pool
Change progress bars to a more MVC style
76
        self._progress_view.clear()
3882.7.5 by Martin Pool
Further mockup of transport-based activity indicator.
77
3882.8.4 by Martin Pool
All UI factories should support note()
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
3882.7.5 by Martin Pool
Further mockup of transport-based activity indicator.
83
    def report_transport_activity(self, transport, byte_count, direction):
84
        """Called by transports as they do IO.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
85
3882.7.5 by Martin Pool
Further mockup of transport-based activity indicator.
86
        This may update a progress bar, spinner, or similar display.
87
        By default it does nothing.
88
        """
3882.8.3 by Martin Pool
Move display of transport throughput into TextProgressView
89
        self._progress_view.show_transport_activity(byte_count)
3882.7.7 by Martin Pool
Change progress bars to a more MVC style
90
3948.2.3 by Martin Pool
Make the interface from ProgressTask to ui more private
91
    def _progress_updated(self, task):
3882.7.7 by Martin Pool
Change progress bars to a more MVC style
92
        """A task has been updated and wants to be displayed.
93
        """
4070.1.1 by Martin Pool
Be more robust about pb updates when none are active
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]:
3948.2.2 by Martin Pool
Corrections to finishing progress bars
98
            warnings.warn("%r is not the top progress task %r" %
99
                (task, self._task_stack[-1]))
3882.7.7 by Martin Pool
Change progress bars to a more MVC style
100
        self._progress_view.show_progress(task)
101
3948.2.5 by Martin Pool
rename to _progress_all_finished
102
    def _progress_all_finished(self):
3948.2.3 by Martin Pool
Make the interface from ProgressTask to ui more private
103
        self._progress_view.clear()
3882.8.9 by Martin Pool
Move TextProgressView to ui.text
104
105
106
class TextProgressView(object):
107
    """Display of progress bar and other information on a tty.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
108
109
    This shows one line of text, including possibly a network indicator, spinner,
3882.8.9 by Martin Pool
Move TextProgressView to ui.text
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.
3948.2.2 by Martin Pool
Corrections to finishing progress bars
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.
3882.8.9 by Martin Pool
Move TextProgressView to ui.text
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._task_fraction = None
134
        self._last_task = None
135
        self._total_byte_count = 0
136
        self._bytes_since_update = 0
137
138
    def _show_line(self, s):
139
        n = self._width - 1
140
        self._term_file.write('\r%-*.*s\r' % (n, n, s))
141
142
    def clear(self):
143
        if self._have_output:
144
            self._show_line('')
145
        self._have_output = False
146
147
    def _render_bar(self):
148
        # return a string for the progress bar itself
149
        if (self._last_task is not None) and self._last_task.show_bar:
150
            spin_str =  r'/-\|'[self._spin_pos % 4]
151
            self._spin_pos += 1
152
            f = self._task_fraction or 0
153
            cols = 20
154
            # number of markers highlighted in bar
155
            markers = int(round(float(cols) * f)) - 1
156
            bar_str = '[' + ('#' * markers + spin_str).ljust(cols) + '] '
157
            return bar_str
158
        elif (self._last_task is None) or self._last_task.show_spinner:
159
            spin_str =  r'/-\|'[self._spin_pos % 4]
160
            self._spin_pos += 1
161
            return spin_str + ' '
162
        else:
163
            return ''
164
165
    def _format_task(self, task):
166
        if not task.show_count:
167
            s = ''
4017.1.1 by John Arbash Meinel
Get a pb.tick() to work after calling pb.update()
168
        elif task.current_cnt is not None and task.total_cnt is not None:
3882.8.9 by Martin Pool
Move TextProgressView to ui.text
169
            s = ' %d/%d' % (task.current_cnt, task.total_cnt)
170
        elif task.current_cnt is not None:
171
            s = ' %d' % (task.current_cnt)
172
        else:
173
            s = ''
174
        self._task_fraction = task._overall_completion_fraction()
175
        # compose all the parent messages
176
        t = task
177
        m = task.msg
178
        while t._parent_task:
179
            t = t._parent_task
180
            if t.msg:
181
                m = t.msg + ':' + m
182
        return m + s
183
184
    def _repaint(self):
185
        bar_string = self._render_bar()
186
        if self._last_task:
187
            task_msg = self._format_task(self._last_task)
188
        else:
189
            task_msg = ''
190
        trans = self._last_transport_msg
191
        if trans and task_msg:
192
            trans += ' | '
193
        s = (bar_string
194
             + trans
195
             + task_msg
196
             )
197
        self._show_line(s)
198
        self._have_output = True
199
200
    def show_progress(self, task):
201
        self._last_task = task
202
        now = time.time()
203
        if now < self._last_repaint + 0.1:
204
            return
205
        if now > self._transport_update_time + 5:
206
            # no recent activity; expire it
207
            self._last_transport_msg = ''
208
        self._last_repaint = now
209
        self._repaint()
210
211
    def show_transport_activity(self, byte_count):
212
        """Called by transports as they do IO.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
213
3882.8.9 by Martin Pool
Move TextProgressView to ui.text
214
        This may update a progress bar, spinner, or similar display.
215
        By default it does nothing.
216
        """
217
        # XXX: Probably there should be a transport activity model, and that
218
        # too should be seen by the progress view, rather than being poked in
219
        # here.
220
        self._total_byte_count += byte_count
221
        self._bytes_since_update += byte_count
222
        now = time.time()
223
        if self._transport_update_time is None:
224
            self._transport_update_time = now
4043.1.1 by John Arbash Meinel
Increase the debounce time for 'transport activity' to 0.5s
225
        elif now >= (self._transport_update_time + 0.5):
3882.8.9 by Martin Pool
Move TextProgressView to ui.text
226
            # guard against clock stepping backwards, and don't update too
227
            # often
228
            rate = self._bytes_since_update / (now - self._transport_update_time)
229
            msg = ("%6dkB @ %4dkB/s" %
230
                (self._total_byte_count>>10, int(rate)>>10,))
231
            self._transport_update_time = now
232
            self._last_repaint = now
233
            self._bytes_since_update = 0
234
            self._last_transport_msg = msg
235
            self._repaint()
236
237