author | Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
Mon, 15 Mar 2010 19:55:52 +0100 | |
branch | stable |
changeset 10701 | 35893dcfd40c |
parent 10656 | f6ee02933af9 |
child 10788 | ca6ba6cac6cd |
permissions | -rw-r--r-- |
10434 | 1 |
# progress.py show progress bars for some actions |
2 |
# |
|
3 |
# Copyright (C) 2010 Augie Fackler <durin42@gmail.com> |
|
4 |
# |
|
5 |
# This program is free software; you can redistribute it and/or modify it |
|
6 |
# under the terms of the GNU General Public License as published by the |
|
7 |
# Free Software Foundation; either version 2 of the License, or (at your |
|
8 |
# option) any later version. |
|
9 |
# |
|
10 |
# This program is distributed in the hope that it will be useful, but |
|
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
13 |
# Public License for more details. |
|
14 |
# |
|
15 |
# You should have received a copy of the GNU General Public License along |
|
16 |
# with this program; if not, write to the Free Software Foundation, Inc., |
|
17 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
||
19 |
"""show progress bars for some actions |
|
20 |
||
10450 | 21 |
This extension uses the progress information logged by hg commands |
22 |
to draw progress bars that are as informative as possible. Some progress |
|
10434 | 23 |
bars only offer indeterminate information, while others have a definite |
24 |
end point. |
|
25 |
||
26 |
The following settings are available:: |
|
27 |
||
28 |
[progress] |
|
29 |
delay = 3 # number of seconds (float) before showing the progress bar |
|
30 |
refresh = 0.1 # time in seconds between refreshes of the progress bar |
|
31 |
format = topic bar number # format of the progress bar |
|
32 |
width = <none> # if set, the maximum width of the progress information |
|
33 |
# (that is, min(width, term width) will be used) |
|
34 |
clear-complete = True # clear the progress bar after it's done |
|
10656
f6ee02933af9
progress: document progress.disable config option
Augie Fackler <durin42@gmail.com>
parents:
10594
diff
changeset
|
35 |
disable = False # if true, don't show a progress bar |
10434 | 36 |
|
10471
132eb7128ad5
progress: use inline literals in help string
Martin Geisler <mg@lazybytes.net>
parents:
10464
diff
changeset
|
37 |
Valid entries for the format field are topic, bar, number, unit, and |
132eb7128ad5
progress: use inline literals in help string
Martin Geisler <mg@lazybytes.net>
parents:
10464
diff
changeset
|
38 |
item. item defaults to the last 20 characters of the item, but this |
132eb7128ad5
progress: use inline literals in help string
Martin Geisler <mg@lazybytes.net>
parents:
10464
diff
changeset
|
39 |
can be changed by adding either ``-<num>`` which would take the last |
132eb7128ad5
progress: use inline literals in help string
Martin Geisler <mg@lazybytes.net>
parents:
10464
diff
changeset
|
40 |
num characters, or ``+<num>`` for the first num characters. |
10434 | 41 |
""" |
42 |
||
43 |
import sys |
|
44 |
import time |
|
45 |
||
46 |
from mercurial import extensions |
|
47 |
from mercurial import util |
|
48 |
||
49 |
def spacejoin(*args): |
|
10452
59f8fff4f887
progress: simplify spacejoin()
Brodie Rao <me+hg@dackz.net>
parents:
10450
diff
changeset
|
50 |
return ' '.join(s for s in args if s) |
10434 | 51 |
|
52 |
class progbar(object): |
|
53 |
def __init__(self, ui): |
|
54 |
self.ui = ui |
|
55 |
self.resetstate() |
|
56 |
||
57 |
def resetstate(self): |
|
58 |
self.topics = [] |
|
59 |
self.printed = False |
|
60 |
self.lastprint = time.time() + float(self.ui.config( |
|
61 |
'progress', 'delay', default=3)) |
|
62 |
self.indetcount = 0 |
|
63 |
self.refresh = float(self.ui.config( |
|
64 |
'progress', 'refresh', default=0.1)) |
|
65 |
self.order = self.ui.configlist( |
|
66 |
'progress', 'format', |
|
67 |
default=['topic', 'bar', 'number']) |
|
68 |
||
69 |
def show(self, topic, pos, item, unit, total): |
|
70 |
termwidth = self.width() |
|
71 |
self.printed = True |
|
72 |
head = '' |
|
73 |
needprogress = False |
|
74 |
tail = '' |
|
75 |
for indicator in self.order: |
|
76 |
add = '' |
|
77 |
if indicator == 'topic': |
|
78 |
add = topic |
|
79 |
elif indicator == 'number': |
|
80 |
if total: |
|
81 |
add = ('% ' + str(len(str(total))) + |
|
82 |
's/%s') % (pos, total) |
|
83 |
else: |
|
84 |
add = str(pos) |
|
85 |
elif indicator.startswith('item') and item: |
|
86 |
slice = 'end' |
|
87 |
if '-' in indicator: |
|
88 |
wid = int(indicator.split('-')[1]) |
|
89 |
elif '+' in indicator: |
|
90 |
slice = 'beginning' |
|
91 |
wid = int(indicator.split('+')[1]) |
|
92 |
else: |
|
93 |
wid = 20 |
|
94 |
if slice == 'end': |
|
95 |
add = item[-wid:] |
|
96 |
else: |
|
97 |
add = item[:wid] |
|
98 |
add += (wid - len(add)) * ' ' |
|
99 |
elif indicator == 'bar': |
|
100 |
add = '' |
|
101 |
needprogress = True |
|
102 |
elif indicator == 'unit' and unit: |
|
103 |
add = unit |
|
104 |
if not needprogress: |
|
105 |
head = spacejoin(head, add) |
|
106 |
else: |
|
107 |
tail = spacejoin(add, tail) |
|
108 |
if needprogress: |
|
109 |
used = 0 |
|
110 |
if head: |
|
111 |
used += len(head) + 1 |
|
112 |
if tail: |
|
113 |
used += len(tail) + 1 |
|
114 |
progwidth = termwidth - used - 3 |
|
115 |
if total: |
|
116 |
amt = pos * progwidth // total |
|
10453
7edc649f9f7e
progress: make determinate bar more like wget progress bar
Brodie Rao <me+hg@dackz.net>
parents:
10452
diff
changeset
|
117 |
bar = '=' * (amt - 1) |
7edc649f9f7e
progress: make determinate bar more like wget progress bar
Brodie Rao <me+hg@dackz.net>
parents:
10452
diff
changeset
|
118 |
if amt > 0: |
7edc649f9f7e
progress: make determinate bar more like wget progress bar
Brodie Rao <me+hg@dackz.net>
parents:
10452
diff
changeset
|
119 |
bar += '>' |
7edc649f9f7e
progress: make determinate bar more like wget progress bar
Brodie Rao <me+hg@dackz.net>
parents:
10452
diff
changeset
|
120 |
bar += ' ' * (progwidth - amt) |
10434 | 121 |
else: |
122 |
progwidth -= 3 |
|
123 |
self.indetcount += 1 |
|
124 |
# mod the count by twice the width so we can make the |
|
125 |
# cursor bounce between the right and left sides |
|
126 |
amt = self.indetcount % (2 * progwidth) |
|
127 |
amt -= progwidth |
|
128 |
bar = (' ' * int(progwidth - abs(amt)) + '<=>' + |
|
129 |
' ' * int(abs(amt))) |
|
130 |
prog = ''.join(('[', bar , ']')) |
|
131 |
out = spacejoin(head, prog, tail) |
|
132 |
else: |
|
133 |
out = spacejoin(head, tail) |
|
134 |
sys.stdout.write('\r' + out[:termwidth]) |
|
135 |
sys.stdout.flush() |
|
136 |
||
137 |
def clear(self): |
|
138 |
sys.stdout.write('\r%s\r' % (' ' * self.width())) |
|
139 |
||
10439
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
140 |
def complete(self): |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
141 |
if self.ui.configbool('progress', 'clear-complete', default=True): |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
142 |
self.clear() |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
143 |
else: |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
144 |
sys.stdout.write('\n') |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
145 |
sys.stdout.flush() |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
146 |
|
10434 | 147 |
def width(self): |
148 |
tw = util.termwidth() |
|
149 |
return min(int(self.ui.config('progress', 'width', default=tw)), tw) |
|
150 |
||
151 |
def progress(self, orig, topic, pos, item='', unit='', total=None): |
|
10439
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
152 |
if pos is None: |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
153 |
if self.topics and self.topics[-1] == topic and self.printed: |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
154 |
self.complete() |
10441
dc0d1ca2d378
progress: only reset state if finishing progress for the current topic
Augie Fackler <durin42@gmail.com>
parents:
10439
diff
changeset
|
155 |
self.resetstate() |
10439
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
156 |
else: |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
157 |
if topic not in self.topics: |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
158 |
self.topics.append(topic) |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
159 |
now = time.time() |
10464
149ad0a3ec91
progress: make progress.refresh=0 always display the progress line
Patrick Mezard <pmezard@gmail.com>
parents:
10463
diff
changeset
|
160 |
if (now - self.lastprint >= self.refresh |
149ad0a3ec91
progress: make progress.refresh=0 always display the progress line
Patrick Mezard <pmezard@gmail.com>
parents:
10463
diff
changeset
|
161 |
and topic == self.topics[-1]): |
10439
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
162 |
self.lastprint = now |
509f4ed56509
progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10434
diff
changeset
|
163 |
self.show(topic, pos, item, unit, total) |
10434 | 164 |
return orig(topic, pos, item=item, unit=unit, total=total) |
165 |
||
166 |
def write(self, orig, *args): |
|
167 |
if self.printed: |
|
168 |
self.clear() |
|
169 |
return orig(*args) |
|
170 |
||
171 |
sharedprog = None |
|
172 |
||
173 |
def uisetup(ui): |
|
10540
dd9d057465c1
progress: provide an explicit disable method for developers
Steve Borho <steve@borho.org>
parents:
10523
diff
changeset
|
174 |
# Apps that derive a class from ui.ui() can use |
dd9d057465c1
progress: provide an explicit disable method for developers
Steve Borho <steve@borho.org>
parents:
10523
diff
changeset
|
175 |
# setconfig('progress', 'disable', 'True') to disable this extension |
dd9d057465c1
progress: provide an explicit disable method for developers
Steve Borho <steve@borho.org>
parents:
10523
diff
changeset
|
176 |
if ui.configbool('progress', 'disable'): |
dd9d057465c1
progress: provide an explicit disable method for developers
Steve Borho <steve@borho.org>
parents:
10523
diff
changeset
|
177 |
return |
10523
4bab7c3db4e1
make the progress extension honor ui.quiet
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents:
10471
diff
changeset
|
178 |
if ui.interactive() and not ui.debugflag and not ui.quiet: |
10434 | 179 |
# we instantiate one globally shared progress bar to avoid |
180 |
# competing progress bars when multiple UI objects get created |
|
181 |
global sharedprog |
|
182 |
if not sharedprog: |
|
183 |
sharedprog = progbar(ui) |
|
184 |
extensions.wrapfunction(ui, 'progress', sharedprog.progress) |
|
185 |
extensions.wrapfunction(ui, 'write', sharedprog.write) |
|
10594
08716b8ba9fb
progress: clear progress before writing to stderr
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
10540
diff
changeset
|
186 |
extensions.wrapfunction(ui, 'write_err', sharedprog.write) |
10434 | 187 |
|
188 |
def reposetup(ui, repo): |
|
189 |
uisetup(repo.ui) |