annotate hgext/progress.py @ 13139:f4dd6aa16805

progress: explain format strings to translators
author Martin Geisler <mg@aragost.com>
date Wed, 15 Dec 2010 09:35:32 +0100
parents 24e3349cba8e
children 43575c67add3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
1 # progress.py show progress bars for some actions
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
2 #
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
3 # Copyright (C) 2010 Augie Fackler <durin42@gmail.com>
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
4 #
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
5 # This program is free software; you can redistribute it and/or modify it
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
6 # under the terms of the GNU General Public License as published by the
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
7 # Free Software Foundation; either version 2 of the License, or (at your
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
8 # option) any later version.
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
9 #
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
10 # This program is distributed in the hope that it will be useful, but
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
13 # Public License for more details.
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
14 #
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License along
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
16 # with this program; if not, write to the Free Software Foundation, Inc.,
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
18
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
19 """show progress bars for some actions
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
20
10450
b4fd900569b1 progress: fix description
timeless <timeless@mozdev.org>
parents: 10441
diff changeset
21 This extension uses the progress information logged by hg commands
b4fd900569b1 progress: fix description
timeless <timeless@mozdev.org>
parents: 10441
diff changeset
22 to draw progress bars that are as informative as possible. Some progress
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
23 bars only offer indeterminate information, while others have a definite
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
24 end point.
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
25
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
26 The following settings are available::
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
27
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
28 [progress]
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
29 delay = 3 # number of seconds (float) before showing the progress bar
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
30 refresh = 0.1 # time in seconds between refreshes of the progress bar
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
31 format = topic bar number # format of the progress bar
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
32 width = <none> # if set, the maximum width of the progress information
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
33 # (that is, min(width, term width) will be used)
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
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
10788
ca6ba6cac6cd progress: use stderr instead of stdout; check stderr.isatty()
Augie Fackler <durin42@gmail.com>
parents: 10656
diff changeset
36 assume-tty = False # if true, ALWAYS show a progress bar, unless
ca6ba6cac6cd progress: use stderr instead of stdout; check stderr.isatty()
Augie Fackler <durin42@gmail.com>
parents: 10656
diff changeset
37 # disable is given
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
38
10471
132eb7128ad5 progress: use inline literals in help string
Martin Geisler <mg@lazybytes.net>
parents: 10464
diff changeset
39 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
40 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
41 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
42 num characters, or ``+<num>`` for the first num characters.
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
43 """
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
44
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
45 import sys
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
46 import time
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
47
13131
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
48 from mercurial.i18n import _
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
49 from mercurial import util
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
50
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
51 def spacejoin(*args):
10452
59f8fff4f887 progress: simplify spacejoin()
Brodie Rao <me+hg@dackz.net>
parents: 10450
diff changeset
52 return ' '.join(s for s in args if s)
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
53
11458
ec21d91c79b3 progress: check stderr.isatty() before each print
Augie Fackler <durin42@gmail.com>
parents: 10891
diff changeset
54 def shouldprint(ui):
12654
646eb9337c87 progress: make sure stderr has isatty before calling (issue2191)
Augie Fackler <durin42@gmail.com>
parents: 11555
diff changeset
55 return (getattr(sys.stderr, 'isatty', None) and
646eb9337c87 progress: make sure stderr has isatty before calling (issue2191)
Augie Fackler <durin42@gmail.com>
parents: 11555
diff changeset
56 (sys.stderr.isatty() or ui.configbool('progress', 'assume-tty')))
11458
ec21d91c79b3 progress: check stderr.isatty() before each print
Augie Fackler <durin42@gmail.com>
parents: 10891
diff changeset
57
13132
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
58 def fmtremaining(seconds):
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
59 if seconds < 60:
13139
f4dd6aa16805 progress: explain format strings to translators
Martin Geisler <mg@aragost.com>
parents: 13132
diff changeset
60 # i18n: format XX seconds as "XXs"
13132
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
61 return _("%02ds") % (seconds)
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
62 minutes = seconds // 60
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
63 if minutes < 60:
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
64 seconds -= minutes * 60
13139
f4dd6aa16805 progress: explain format strings to translators
Martin Geisler <mg@aragost.com>
parents: 13132
diff changeset
65 # i18n: format X minutes and YY seconds as "XmYYs"
13132
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
66 return _("%dm%02ds") % (minutes, seconds)
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
67 # we're going to ignore seconds in this case
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
68 minutes += 1
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
69 hours = minutes // 60
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
70 minutes -= hours * 60
13139
f4dd6aa16805 progress: explain format strings to translators
Martin Geisler <mg@aragost.com>
parents: 13132
diff changeset
71 # i18n: format X hours and YY minutes as "XhYYm"
13132
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
72 return _("%dh%02dm") % (hours, minutes)
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
73
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
74 class progbar(object):
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
75 def __init__(self, ui):
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
76 self.ui = ui
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
77 self.resetstate()
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
78
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
79 def resetstate(self):
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
80 self.topics = []
13130
f139f34ba330 progress: react more reasonably to nested progress topics
Augie Fackler <durin42@gmail.com>
parents: 12689
diff changeset
81 self.topicstates = {}
13131
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
82 self.starttimes = {}
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
83 self.startvals = {}
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
84 self.printed = False
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
85 self.lastprint = time.time() + float(self.ui.config(
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
86 'progress', 'delay', default=3))
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
87 self.indetcount = 0
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
88 self.refresh = float(self.ui.config(
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
89 'progress', 'refresh', default=0.1))
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
90 self.order = self.ui.configlist(
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
91 'progress', 'format',
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
92 default=['topic', 'bar', 'number'])
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
93
13131
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
94 def show(self, now, topic, pos, item, unit, total):
11458
ec21d91c79b3 progress: check stderr.isatty() before each print
Augie Fackler <durin42@gmail.com>
parents: 10891
diff changeset
95 if not shouldprint(self.ui):
ec21d91c79b3 progress: check stderr.isatty() before each print
Augie Fackler <durin42@gmail.com>
parents: 10891
diff changeset
96 return
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
97 termwidth = self.width()
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
98 self.printed = True
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
99 head = ''
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
100 needprogress = False
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
101 tail = ''
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
102 for indicator in self.order:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
103 add = ''
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
104 if indicator == 'topic':
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
105 add = topic
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
106 elif indicator == 'number':
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
107 if total:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
108 add = ('% ' + str(len(str(total))) +
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
109 's/%s') % (pos, total)
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
110 else:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
111 add = str(pos)
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
112 elif indicator.startswith('item') and item:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
113 slice = 'end'
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
114 if '-' in indicator:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
115 wid = int(indicator.split('-')[1])
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
116 elif '+' in indicator:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
117 slice = 'beginning'
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
118 wid = int(indicator.split('+')[1])
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
119 else:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
120 wid = 20
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
121 if slice == 'end':
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
122 add = item[-wid:]
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
123 else:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
124 add = item[:wid]
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
125 add += (wid - len(add)) * ' '
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
126 elif indicator == 'bar':
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
127 add = ''
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
128 needprogress = True
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
129 elif indicator == 'unit' and unit:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
130 add = unit
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
131 if not needprogress:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
132 head = spacejoin(head, add)
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
133 else:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
134 tail = spacejoin(add, tail)
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
135 if needprogress:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
136 used = 0
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
137 if head:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
138 used += len(head) + 1
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
139 if tail:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
140 used += len(tail) + 1
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
141 progwidth = termwidth - used - 3
10891
83af68e38be3 progress: fall back to indeterminate progress if position is >= total
Augie Fackler <durin42@gmail.com>
parents: 10815
diff changeset
142 if total and pos <= total:
13131
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
143 initial = self.startvals[topic]
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
144 target = total - initial
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
145 delta = pos - initial
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
146 if delta > 0:
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
147 elapsed = now - self.starttimes[topic]
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
148 if elapsed > float(
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
149 self.ui.config('progress', 'estimate', default=2)):
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
150 seconds = (elapsed * (target - delta)) // delta + 1
13132
24e3349cba8e progress: refactor for readability and show XXs instead of 0mXXs.
Augie Fackler <durin42@gmail.com>
parents: 13131
diff changeset
151 remaining = fmtremaining(seconds)
13131
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
152 progwidth -= len(remaining) + 1
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
153 tail = spacejoin(tail, remaining)
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
154 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
155 bar = '=' * (amt - 1)
7edc649f9f7e progress: make determinate bar more like wget progress bar
Brodie Rao <me+hg@dackz.net>
parents: 10452
diff changeset
156 if amt > 0:
7edc649f9f7e progress: make determinate bar more like wget progress bar
Brodie Rao <me+hg@dackz.net>
parents: 10452
diff changeset
157 bar += '>'
7edc649f9f7e progress: make determinate bar more like wget progress bar
Brodie Rao <me+hg@dackz.net>
parents: 10452
diff changeset
158 bar += ' ' * (progwidth - amt)
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
159 else:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
160 progwidth -= 3
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
161 self.indetcount += 1
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
162 # mod the count by twice the width so we can make the
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
163 # cursor bounce between the right and left sides
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
164 amt = self.indetcount % (2 * progwidth)
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
165 amt -= progwidth
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
166 bar = (' ' * int(progwidth - abs(amt)) + '<=>' +
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
167 ' ' * int(abs(amt)))
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
168 prog = ''.join(('[', bar , ']'))
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
169 out = spacejoin(head, prog, tail)
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
170 else:
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
171 out = spacejoin(head, tail)
10788
ca6ba6cac6cd progress: use stderr instead of stdout; check stderr.isatty()
Augie Fackler <durin42@gmail.com>
parents: 10656
diff changeset
172 sys.stderr.write('\r' + out[:termwidth])
ca6ba6cac6cd progress: use stderr instead of stdout; check stderr.isatty()
Augie Fackler <durin42@gmail.com>
parents: 10656
diff changeset
173 sys.stderr.flush()
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
174
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
175 def clear(self):
11458
ec21d91c79b3 progress: check stderr.isatty() before each print
Augie Fackler <durin42@gmail.com>
parents: 10891
diff changeset
176 if not shouldprint(self.ui):
ec21d91c79b3 progress: check stderr.isatty() before each print
Augie Fackler <durin42@gmail.com>
parents: 10891
diff changeset
177 return
10788
ca6ba6cac6cd progress: use stderr instead of stdout; check stderr.isatty()
Augie Fackler <durin42@gmail.com>
parents: 10656
diff changeset
178 sys.stderr.write('\r%s\r' % (' ' * self.width()))
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
179
10439
509f4ed56509 progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10434
diff changeset
180 def complete(self):
11458
ec21d91c79b3 progress: check stderr.isatty() before each print
Augie Fackler <durin42@gmail.com>
parents: 10891
diff changeset
181 if not shouldprint(self.ui):
ec21d91c79b3 progress: check stderr.isatty() before each print
Augie Fackler <durin42@gmail.com>
parents: 10891
diff changeset
182 return
10439
509f4ed56509 progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10434
diff changeset
183 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
184 self.clear()
509f4ed56509 progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10434
diff changeset
185 else:
10788
ca6ba6cac6cd progress: use stderr instead of stdout; check stderr.isatty()
Augie Fackler <durin42@gmail.com>
parents: 10656
diff changeset
186 sys.stderr.write('\n')
ca6ba6cac6cd progress: use stderr instead of stdout; check stderr.isatty()
Augie Fackler <durin42@gmail.com>
parents: 10656
diff changeset
187 sys.stderr.flush()
10439
509f4ed56509 progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10434
diff changeset
188
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
189 def width(self):
12689
c52c629ce19e termwidth: move to ui.ui from util
Augie Fackler <durin42@gmail.com>
parents: 12654
diff changeset
190 tw = self.ui.termwidth()
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
191 return min(int(self.ui.config('progress', 'width', default=tw)), tw)
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
192
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
193 def progress(self, topic, pos, item='', unit='', total=None):
13131
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
194 now = time.time()
10439
509f4ed56509 progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10434
diff changeset
195 if pos is None:
13131
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
196 self.starttimes.pop(topic, None)
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
197 self.startvals.pop(topic, None)
13130
f139f34ba330 progress: react more reasonably to nested progress topics
Augie Fackler <durin42@gmail.com>
parents: 12689
diff changeset
198 self.topicstates.pop(topic, None)
f139f34ba330 progress: react more reasonably to nested progress topics
Augie Fackler <durin42@gmail.com>
parents: 12689
diff changeset
199 # reset the progress bar if this is the outermost topic
f139f34ba330 progress: react more reasonably to nested progress topics
Augie Fackler <durin42@gmail.com>
parents: 12689
diff changeset
200 if self.topics and self.topics[0] == topic and self.printed:
10439
509f4ed56509 progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10434
diff changeset
201 self.complete()
10441
dc0d1ca2d378 progress: only reset state if finishing progress for the current topic
Augie Fackler <durin42@gmail.com>
parents: 10439
diff changeset
202 self.resetstate()
13130
f139f34ba330 progress: react more reasonably to nested progress topics
Augie Fackler <durin42@gmail.com>
parents: 12689
diff changeset
203 # truncate the list of topics assuming all topics within
f139f34ba330 progress: react more reasonably to nested progress topics
Augie Fackler <durin42@gmail.com>
parents: 12689
diff changeset
204 # this one are also closed
f139f34ba330 progress: react more reasonably to nested progress topics
Augie Fackler <durin42@gmail.com>
parents: 12689
diff changeset
205 if topic in self.topics:
f139f34ba330 progress: react more reasonably to nested progress topics
Augie Fackler <durin42@gmail.com>
parents: 12689
diff changeset
206 self.topics = self.topics[:self.topics.index(topic)]
10439
509f4ed56509 progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10434
diff changeset
207 else:
509f4ed56509 progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10434
diff changeset
208 if topic not in self.topics:
13131
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
209 self.starttimes[topic] = now
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
210 self.startvals[topic] = pos
10439
509f4ed56509 progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10434
diff changeset
211 self.topics.append(topic)
13130
f139f34ba330 progress: react more reasonably to nested progress topics
Augie Fackler <durin42@gmail.com>
parents: 12689
diff changeset
212 self.topicstates[topic] = pos, item, unit, total
f139f34ba330 progress: react more reasonably to nested progress topics
Augie Fackler <durin42@gmail.com>
parents: 12689
diff changeset
213 if now - self.lastprint >= self.refresh and self.topics:
10439
509f4ed56509 progress: correctly handle empty progress topic
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10434
diff changeset
214 self.lastprint = now
13130
f139f34ba330 progress: react more reasonably to nested progress topics
Augie Fackler <durin42@gmail.com>
parents: 12689
diff changeset
215 current = self.topics[-1]
13131
c9ae7e096994 progress: Add estimated time remaining for long tasks
timeless <timeless@gmail.com>
parents: 13130
diff changeset
216 self.show(now, topic, *self.topicstates[topic])
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
217
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
218 def uisetup(ui):
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
219 class progressui(ui.__class__):
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
220 _progbar = None
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
221
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
222 def progress(self, *args, **opts):
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
223 self._progbar.progress(*args, **opts)
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
224 return super(progressui, self).progress(*args, **opts)
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
225
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
226 def write(self, *args, **opts):
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
227 if self._progbar.printed:
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
228 self._progbar.clear()
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
229 return super(progressui, self).write(*args, **opts)
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
230
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
231 def write_err(self, *args, **opts):
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
232 if self._progbar.printed:
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
233 self._progbar.clear()
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
234 return super(progressui, self).write_err(*args, **opts)
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
235
10540
dd9d057465c1 progress: provide an explicit disable method for developers
Steve Borho <steve@borho.org>
parents: 10523
diff changeset
236 # 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
237 # 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
238 if ui.configbool('progress', 'disable'):
dd9d057465c1 progress: provide an explicit disable method for developers
Steve Borho <steve@borho.org>
parents: 10523
diff changeset
239 return
11458
ec21d91c79b3 progress: check stderr.isatty() before each print
Augie Fackler <durin42@gmail.com>
parents: 10891
diff changeset
240 if shouldprint(ui) and not ui.debugflag and not ui.quiet:
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
241 ui.__class__ = progressui
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
242 # we instantiate one globally shared progress bar to avoid
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
243 # competing progress bars when multiple UI objects get created
11555
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
244 if not progressui._progbar:
d8d0fc3988ca color/progress: subclass ui instead of using wrapfunction (issue2096)
Brodie Rao <brodie@bitheap.org>
parents: 11458
diff changeset
245 progressui._progbar = progbar(ui)
10434
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
246
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
247 def reposetup(ui, repo):
ad104a786d35 Progress bar extension
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
248 uisetup(repo.ui)