progress: check stderr.isatty() before each print stable
authorAugie Fackler <durin42@gmail.com>
Sun, 27 Jun 2010 22:20:47 -0500
branchstable
changeset 11458 ec21d91c79b3
parent 11457 2ec346160783
child 11459 59af1d65029c
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`.
hgext/progress.py
--- a/hgext/progress.py	Sat Jun 26 23:00:58 2010 +0200
+++ b/hgext/progress.py	Sun Jun 27 22:20:47 2010 -0500
@@ -51,6 +51,9 @@
 def spacejoin(*args):
     return ' '.join(s for s in args if s)
 
+def shouldprint(ui):
+    return sys.stderr.isatty() or ui.configbool('progress', 'assume-tty')
+
 class progbar(object):
     def __init__(self, ui):
         self.ui = ui
@@ -69,6 +72,8 @@
             default=['topic', 'bar', 'number'])
 
     def show(self, topic, pos, item, unit, total):
+        if not shouldprint(self.ui):
+            return
         termwidth = self.width()
         self.printed = True
         head = ''
@@ -137,9 +142,13 @@
         sys.stderr.flush()
 
     def clear(self):
+        if not shouldprint(self.ui):
+            return
         sys.stderr.write('\r%s\r' % (' ' * self.width()))
 
     def complete(self):
+        if not shouldprint(self.ui):
+            return
         if self.ui.configbool('progress', 'clear-complete', default=True):
             self.clear()
         else:
@@ -177,8 +186,7 @@
     # setconfig('progress', 'disable', 'True') to disable this extension
     if ui.configbool('progress', 'disable'):
         return
-    if ((sys.stderr.isatty() or ui.configbool('progress', 'assume-tty'))
-        and not ui.debugflag and not ui.quiet):
+    if shouldprint(ui) and not ui.debugflag and not ui.quiet:
         # we instantiate one globally shared progress bar to avoid
         # competing progress bars when multiple UI objects get created
         global sharedprog