diff 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
line wrap: on
line diff
--- a/hgext/progress.py	Mon Mar 29 15:16:05 2010 -0500
+++ b/hgext/progress.py	Mon Mar 29 16:44:24 2010 -0500
@@ -33,6 +33,8 @@
                  # (that is, min(width, term width) will be used)
   clear-complete = True # clear the progress bar after it's done
   disable = False # if true, don't show a progress bar
+  assume-tty = False # if true, ALWAYS show a progress bar, unless
+                     # disable is given
 
 Valid entries for the format field are topic, bar, number, unit, and
 item. item defaults to the last 20 characters of the item, but this
@@ -131,18 +133,18 @@
             out = spacejoin(head, prog, tail)
         else:
             out = spacejoin(head, tail)
-        sys.stdout.write('\r' + out[:termwidth])
-        sys.stdout.flush()
+        sys.stderr.write('\r' + out[:termwidth])
+        sys.stderr.flush()
 
     def clear(self):
-        sys.stdout.write('\r%s\r' % (' ' * self.width()))
+        sys.stderr.write('\r%s\r' % (' ' * self.width()))
 
     def complete(self):
         if self.ui.configbool('progress', 'clear-complete', default=True):
             self.clear()
         else:
-            sys.stdout.write('\n')
-        sys.stdout.flush()
+            sys.stderr.write('\n')
+        sys.stderr.flush()
 
     def width(self):
         tw = util.termwidth()
@@ -175,7 +177,8 @@
     # setconfig('progress', 'disable', 'True') to disable this extension
     if ui.configbool('progress', 'disable'):
         return
-    if ui.interactive() and not ui.debugflag and not ui.quiet:
+    if ((sys.stderr.isatty() or ui.configbool('progress', 'assume-tty'))
+        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