comparison hgext/progress.py @ 10788:ca6ba6cac6cd stable

progress: use stderr instead of stdout; check stderr.isatty() This means that progress bars will continue to show on the terminal when both stdin and stdout are redirected.
author Augie Fackler <durin42@gmail.com>
date Mon, 29 Mar 2010 16:44:24 -0500
parents f6ee02933af9
children 32b213b9b22c
comparison
equal deleted inserted replaced
10784:716e76f89e3a 10788:ca6ba6cac6cd
31 format = topic bar number # format 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 32 width = <none> # if set, the maximum width of the progress information
33 # (that is, min(width, term width) will be used) 33 # (that is, min(width, term width) will be used)
34 clear-complete = True # clear the progress bar after it's done 34 clear-complete = True # clear the progress bar after it's done
35 disable = False # if true, don't show a progress bar 35 disable = False # if true, don't show a progress bar
36 assume-tty = False # if true, ALWAYS show a progress bar, unless
37 # disable is given
36 38
37 Valid entries for the format field are topic, bar, number, unit, and 39 Valid entries for the format field are topic, bar, number, unit, and
38 item. item defaults to the last 20 characters of the item, but this 40 item. item defaults to the last 20 characters of the item, but this
39 can be changed by adding either ``-<num>`` which would take the last 41 can be changed by adding either ``-<num>`` which would take the last
40 num characters, or ``+<num>`` for the first num characters. 42 num characters, or ``+<num>`` for the first num characters.
129 ' ' * int(abs(amt))) 131 ' ' * int(abs(amt)))
130 prog = ''.join(('[', bar , ']')) 132 prog = ''.join(('[', bar , ']'))
131 out = spacejoin(head, prog, tail) 133 out = spacejoin(head, prog, tail)
132 else: 134 else:
133 out = spacejoin(head, tail) 135 out = spacejoin(head, tail)
134 sys.stdout.write('\r' + out[:termwidth]) 136 sys.stderr.write('\r' + out[:termwidth])
135 sys.stdout.flush() 137 sys.stderr.flush()
136 138
137 def clear(self): 139 def clear(self):
138 sys.stdout.write('\r%s\r' % (' ' * self.width())) 140 sys.stderr.write('\r%s\r' % (' ' * self.width()))
139 141
140 def complete(self): 142 def complete(self):
141 if self.ui.configbool('progress', 'clear-complete', default=True): 143 if self.ui.configbool('progress', 'clear-complete', default=True):
142 self.clear() 144 self.clear()
143 else: 145 else:
144 sys.stdout.write('\n') 146 sys.stderr.write('\n')
145 sys.stdout.flush() 147 sys.stderr.flush()
146 148
147 def width(self): 149 def width(self):
148 tw = util.termwidth() 150 tw = util.termwidth()
149 return min(int(self.ui.config('progress', 'width', default=tw)), tw) 151 return min(int(self.ui.config('progress', 'width', default=tw)), tw)
150 152
173 def uisetup(ui): 175 def uisetup(ui):
174 # Apps that derive a class from ui.ui() can use 176 # Apps that derive a class from ui.ui() can use
175 # setconfig('progress', 'disable', 'True') to disable this extension 177 # setconfig('progress', 'disable', 'True') to disable this extension
176 if ui.configbool('progress', 'disable'): 178 if ui.configbool('progress', 'disable'):
177 return 179 return
178 if ui.interactive() and not ui.debugflag and not ui.quiet: 180 if ((sys.stderr.isatty() or ui.configbool('progress', 'assume-tty'))
181 and not ui.debugflag and not ui.quiet):
179 # we instantiate one globally shared progress bar to avoid 182 # we instantiate one globally shared progress bar to avoid
180 # competing progress bars when multiple UI objects get created 183 # competing progress bars when multiple UI objects get created
181 global sharedprog 184 global sharedprog
182 if not sharedprog: 185 if not sharedprog:
183 sharedprog = progbar(ui) 186 sharedprog = progbar(ui)