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