comparison hgext/progress.py @ 11458:ec21d91c79b3 stable

progress: check stderr.isatty() before each print This prevents writing progress information to a non-tty stderr if one is swapped in after startup, which happens in `hg serve`.
author Augie Fackler <durin42@gmail.com>
date Sun, 27 Jun 2010 22:20:47 -0500
parents 83af68e38be3
children d8d0fc3988ca
comparison
equal deleted inserted replaced
11457:2ec346160783 11458:ec21d91c79b3
48 from mercurial import extensions 48 from mercurial import extensions
49 from mercurial import util 49 from mercurial import util
50 50
51 def spacejoin(*args): 51 def spacejoin(*args):
52 return ' '.join(s for s in args if s) 52 return ' '.join(s for s in args if s)
53
54 def shouldprint(ui):
55 return sys.stderr.isatty() or ui.configbool('progress', 'assume-tty')
53 56
54 class progbar(object): 57 class progbar(object):
55 def __init__(self, ui): 58 def __init__(self, ui):
56 self.ui = ui 59 self.ui = ui
57 self.resetstate() 60 self.resetstate()
67 self.order = self.ui.configlist( 70 self.order = self.ui.configlist(
68 'progress', 'format', 71 'progress', 'format',
69 default=['topic', 'bar', 'number']) 72 default=['topic', 'bar', 'number'])
70 73
71 def show(self, topic, pos, item, unit, total): 74 def show(self, topic, pos, item, unit, total):
75 if not shouldprint(self.ui):
76 return
72 termwidth = self.width() 77 termwidth = self.width()
73 self.printed = True 78 self.printed = True
74 head = '' 79 head = ''
75 needprogress = False 80 needprogress = False
76 tail = '' 81 tail = ''
135 out = spacejoin(head, tail) 140 out = spacejoin(head, tail)
136 sys.stderr.write('\r' + out[:termwidth]) 141 sys.stderr.write('\r' + out[:termwidth])
137 sys.stderr.flush() 142 sys.stderr.flush()
138 143
139 def clear(self): 144 def clear(self):
145 if not shouldprint(self.ui):
146 return
140 sys.stderr.write('\r%s\r' % (' ' * self.width())) 147 sys.stderr.write('\r%s\r' % (' ' * self.width()))
141 148
142 def complete(self): 149 def complete(self):
150 if not shouldprint(self.ui):
151 return
143 if self.ui.configbool('progress', 'clear-complete', default=True): 152 if self.ui.configbool('progress', 'clear-complete', default=True):
144 self.clear() 153 self.clear()
145 else: 154 else:
146 sys.stderr.write('\n') 155 sys.stderr.write('\n')
147 sys.stderr.flush() 156 sys.stderr.flush()
175 def uisetup(ui): 184 def uisetup(ui):
176 # Apps that derive a class from ui.ui() can use 185 # Apps that derive a class from ui.ui() can use
177 # setconfig('progress', 'disable', 'True') to disable this extension 186 # setconfig('progress', 'disable', 'True') to disable this extension
178 if ui.configbool('progress', 'disable'): 187 if ui.configbool('progress', 'disable'):
179 return 188 return
180 if ((sys.stderr.isatty() or ui.configbool('progress', 'assume-tty')) 189 if shouldprint(ui) and not ui.debugflag and not ui.quiet:
181 and not ui.debugflag and not ui.quiet):
182 # we instantiate one globally shared progress bar to avoid 190 # we instantiate one globally shared progress bar to avoid
183 # competing progress bars when multiple UI objects get created 191 # competing progress bars when multiple UI objects get created
184 global sharedprog 192 global sharedprog
185 if not sharedprog: 193 if not sharedprog:
186 sharedprog = progbar(ui) 194 sharedprog = progbar(ui)