progress: use stderr instead of stdout; check stderr.isatty() stable
authorAugie Fackler <durin42@gmail.com>
Mon, 29 Mar 2010 16:44:24 -0500
branchstable
changeset 10788 ca6ba6cac6cd
parent 10784 716e76f89e3a
child 10789 1b45468d3deb
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.
hgext/progress.py
tests/test-progress
--- 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
--- a/tests/test-progress	Mon Mar 29 15:16:05 2010 -0500
+++ b/tests/test-progress	Mon Mar 29 16:44:24 2010 -0500
@@ -31,30 +31,29 @@
 echo "[extensions]" >> $HGRCPATH
 echo "progress=" >> $HGRCPATH
 echo "loop=`pwd`/loop.py" >> $HGRCPATH
-echo "[ui]" >> $HGRCPATH
-echo "interactive=1" >> $HGRCPATH
+echo "[progress]" >> $HGRCPATH
+echo "assume-tty=1" >> $HGRCPATH
 
 echo '% test default params, display nothing because of delay'
-hg -y loop 3 | python filtercr.py
+hg -y loop 3 2>&1 | python filtercr.py
 
-echo "[progress]" >> $HGRCPATH
 echo "delay=0" >> $HGRCPATH
 echo "refresh=0" >> $HGRCPATH
 
 echo '% test with delay=0, refresh=0'
-hg -y loop 3 | python filtercr.py
+hg -y loop 3 2>&1 | python filtercr.py
 
 echo '% test refresh is taken in account'
-hg -y --config progress.refresh=100 loop 3 | python filtercr.py
+hg -y --config progress.refresh=100 loop 3 2>&1 | python filtercr.py
 
 echo '% test format options 1'
-hg -y --config 'progress.format=number topic item+2' loop 2 | python filtercr.py
+hg -y --config 'progress.format=number topic item+2' loop 2 2>&1 | python filtercr.py
 
 echo '% test format options 2'
-hg -y --config 'progress.format=number item-3 bar' loop 2 | python filtercr.py
+hg -y --config 'progress.format=number item-3 bar' loop 2 2>&1 | python filtercr.py
 
 echo '% test format options and indeterminate progress'
-hg -y --config 'progress.format=number item bar' loop -- -2 | python filtercr.py
+hg -y --config 'progress.format=number item bar' loop -- -2 2>&1 | python filtercr.py
 
 echo '% test immediate progress completion'
-hg -y loop 0 | python filtercr.py
+hg -y loop 0 2>&1 | python filtercr.py