~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-01-15 07:34:16 UTC
  • mfrom: (3882.7.17 progress)
  • Revision ID: pqm@pqm.ubuntu.com-20090115073416-vnzvkab4dfesetj0
(mbp) transport-based progress bars

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
displays no output.
27
27
"""
28
28
 
 
29
import os
29
30
import sys
 
31
import warnings
30
32
 
31
33
from bzrlib.lazy_import import lazy_import
32
34
lazy_import(globals(), """
49
51
    """
50
52
 
51
53
    def __init__(self):
52
 
        super(UIFactory, self).__init__()
53
 
        self._progress_bar_stack = None
 
54
        self._task_stack = []
54
55
 
55
56
    def get_password(self, prompt='', **kwargs):
56
57
        """Prompt the user for a password.
73
74
        When the bar has been finished with, it should be released by calling
74
75
        bar.finished().
75
76
        """
76
 
        raise NotImplementedError(self.nested_progress_bar)
 
77
        if self._task_stack:
 
78
            t = progress.ProgressTask(self._task_stack[-1], self)
 
79
        else:
 
80
            t = progress.ProgressTask(None, self)
 
81
        self._task_stack.append(t)
 
82
        return t
 
83
 
 
84
    def progress_finished(self, task):
 
85
        if task != self._task_stack[-1]:
 
86
            warnings.warn("%r is not currently active" % (task,))
 
87
        else:
 
88
            del self._task_stack[-1]
77
89
 
78
90
    def clear_term(self):
79
91
        """Prepare the terminal for output.
104
116
            current_format_name,
105
117
            basedir)
106
118
 
 
119
    def report_transport_activity(self, transport, byte_count, direction):
 
120
        """Called by transports as they do IO.
 
121
        
 
122
        This may update a progress bar, spinner, or similar display.
 
123
        By default it does nothing.
 
124
        """
 
125
        pass
 
126
 
 
127
 
107
128
 
108
129
class CLIUIFactory(UIFactory):
109
 
    """Common behaviour for command line UI factories."""
 
130
    """Common behaviour for command line UI factories.
 
131
    
 
132
    This is suitable for dumb terminals that can't repaint existing text."""
110
133
 
111
 
    def __init__(self):
112
 
        super(CLIUIFactory, self).__init__()
113
 
        self.stdin = sys.stdin
 
134
    def __init__(self, stdin=None, stdout=None, stderr=None):
 
135
        UIFactory.__init__(self)
 
136
        self.stdin = stdin or sys.stdin
 
137
        self.stdout = stdout or sys.stdout
 
138
        self.stderr = stderr or sys.stderr
114
139
 
115
140
    def get_boolean(self, prompt):
116
141
        self.clear_term()
148
173
    def prompt(self, prompt):
149
174
        """Emit prompt on the CLI."""
150
175
 
 
176
    def clear_term(self):
 
177
        pass
 
178
 
 
179
    def show_progress(self, task):
 
180
        pass
 
181
 
 
182
    def progress_finished(self, task):
 
183
        pass
 
184
 
151
185
 
152
186
class SilentUIFactory(CLIUIFactory):
153
187
    """A UI Factory which never prints anything.
155
189
    This is the default UI, if another one is never registered.
156
190
    """
157
191
 
 
192
    def __init__(self):
 
193
        CLIUIFactory.__init__(self)
 
194
 
158
195
    def get_password(self, prompt='', **kwargs):
159
196
        return None
160
197
 
161
 
    def nested_progress_bar(self):
162
 
        if self._progress_bar_stack is None:
163
 
            self._progress_bar_stack = progress.ProgressBarStack(
164
 
                klass=progress.DummyProgress)
165
 
        return self._progress_bar_stack.get_nested()
166
 
 
167
 
    def clear_term(self):
168
 
        pass
169
 
 
170
 
    def recommend_upgrade(self, *args):
 
198
 
 
199
    def note(self, msg):
171
200
        pass
172
201
 
173
202
 
180
209
ui_factory = SilentUIFactory()
181
210
"""IMPORTANT: never import this symbol directly. ONLY ever access it as 
182
211
ui.ui_factory."""
 
212
 
 
213
 
 
214
def make_ui_for_terminal(stdin, stdout, stderr):
 
215
    """Construct and return a suitable UIFactory for a text mode program.
 
216
 
 
217
    If stdout is a smart terminal, this gets a smart UIFactory with 
 
218
    progress indicators, etc.  If it's a dumb terminal, just plain text output.
 
219
    """
 
220
    isatty = getattr(stdin, 'isatty', None)
 
221
    if isatty is None:
 
222
        cls = CLIUIFactory
 
223
    elif not isatty():
 
224
        cls = CLIUIFactory
 
225
    elif os.environ.get('TERM') in (None, 'dumb', ''):
 
226
        # e.g. emacs compile window
 
227
        cls = CLIUIFactory
 
228
    else:
 
229
        from bzrlib.ui.text import TextUIFactory
 
230
        cls = TextUIFactory
 
231
    return cls(stdin=stdin, stdout=stdout, stderr=stderr)