[PATCH v2] progress: check stderr.isatty() before each print

Augie Fackler durin42 at gmail.com
Sun Jun 27 22:26:10 CDT 2010


# HG changeset patch
# User Augie Fackler <durin42 at gmail.com>
# Date 1277695247 18000
# Branch stable
# Node ID eefb5d1f2c45e686ac501ccba0b24f5da968e920
# Parent  51021f4c80b5dcf608626ddc24b21c155df2cae8
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`.

diff --git a/hgext/progress.py b/hgext/progress.py
--- a/hgext/progress.py
+++ b/hgext/progress.py
@@ -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


More information about the Mercurial-devel mailing list