comparison mercurial/progress.py @ 30324:cb1ea3cc44b5

progress: obtain stderr from ui This will help Python 3 porting.
author Yuya Nishihara <yuya@tcha.org>
date Thu, 20 Oct 2016 22:12:48 +0900
parents 222b8170d69e
children c3ef33fd0058
comparison
equal deleted inserted replaced
30323:5f7d13d3bd4d 30324:cb1ea3cc44b5
5 # This software may be used and distributed according to the terms of the 5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 7
8 from __future__ import absolute_import 8 from __future__ import absolute_import
9 9
10 import sys
11 import threading 10 import threading
12 import time 11 import time
13 12
14 from .i18n import _ 13 from .i18n import _
15 from . import encoding 14 from . import encoding
17 def spacejoin(*args): 16 def spacejoin(*args):
18 return ' '.join(s for s in args if s) 17 return ' '.join(s for s in args if s)
19 18
20 def shouldprint(ui): 19 def shouldprint(ui):
21 return not (ui.quiet or ui.plain('progress')) and ( 20 return not (ui.quiet or ui.plain('progress')) and (
22 ui._isatty(sys.stderr) or ui.configbool('progress', 'assume-tty')) 21 ui._isatty(ui.ferr) or ui.configbool('progress', 'assume-tty'))
23 22
24 def fmtremaining(seconds): 23 def fmtremaining(seconds):
25 """format a number of remaining seconds in human readable way 24 """format a number of remaining seconds in human readable way
26 25
27 This will properly display seconds, minutes, hours, days if needed""" 26 This will properly display seconds, minutes, hours, days if needed"""
156 ' ' * int(abs(amt))) 155 ' ' * int(abs(amt)))
157 prog = ''.join(('[', bar , ']')) 156 prog = ''.join(('[', bar , ']'))
158 out = spacejoin(head, prog, tail) 157 out = spacejoin(head, prog, tail)
159 else: 158 else:
160 out = spacejoin(head, tail) 159 out = spacejoin(head, tail)
161 sys.stderr.write('\r' + encoding.trim(out, termwidth)) 160 self.ui.ferr.write('\r' + encoding.trim(out, termwidth))
162 self.lasttopic = topic 161 self.lasttopic = topic
163 sys.stderr.flush() 162 self.ui.ferr.flush()
164 163
165 def clear(self): 164 def clear(self):
166 if not self.printed or not self.lastprint or not shouldprint(self.ui): 165 if not self.printed or not self.lastprint or not shouldprint(self.ui):
167 return 166 return
168 sys.stderr.write('\r%s\r' % (' ' * self.width())) 167 self.ui.ferr.write('\r%s\r' % (' ' * self.width()))
169 if self.printed: 168 if self.printed:
170 # force immediate re-paint of progress bar 169 # force immediate re-paint of progress bar
171 self.lastprint = 0 170 self.lastprint = 0
172 171
173 def complete(self): 172 def complete(self):
174 if not shouldprint(self.ui): 173 if not shouldprint(self.ui):
175 return 174 return
176 if self.ui.configbool('progress', 'clear-complete', default=True): 175 if self.ui.configbool('progress', 'clear-complete', default=True):
177 self.clear() 176 self.clear()
178 else: 177 else:
179 sys.stderr.write('\n') 178 self.ui.ferr.write('\n')
180 sys.stderr.flush() 179 self.ui.ferr.flush()
181 180
182 def width(self): 181 def width(self):
183 tw = self.ui.termwidth() 182 tw = self.ui.termwidth()
184 return min(int(self.ui.config('progress', 'width', default=tw)), tw) 183 return min(int(self.ui.config('progress', 'width', default=tw)), tw)
185 184