util: add helper function isatty(fd) to check for tty-ness
authorIdan Kamara <idankk86@gmail.com>
Thu, 02 Jun 2011 00:43:34 +0300
changeset 14515 76f295eaed86
parent 14514 175e4b9d8a96
child 14516 842a9179132c
util: add helper function isatty(fd) to check for tty-ness
hgext/pager.py
hgext/progress.py
mercurial/ui.py
mercurial/util.py
--- a/hgext/pager.py	Thu Jun 02 18:52:31 2011 +0800
+++ b/hgext/pager.py	Thu Jun 02 00:43:34 2011 +0300
@@ -60,7 +60,7 @@
 def _runpager(p):
     if not hasattr(os, 'fork'):
         sys.stdout = util.popen(p, 'wb')
-        if sys.stderr.isatty():
+        if util.isatty(sys.stderr):
             sys.stderr = sys.stdout
         return
     fdin, fdout = os.pipe()
@@ -68,7 +68,7 @@
     if pid == 0:
         os.close(fdin)
         os.dup2(fdout, sys.stdout.fileno())
-        if sys.stderr.isatty():
+        if util.isatty(sys.stderr):
             os.dup2(fdout, sys.stderr.fileno())
         os.close(fdout)
         return
@@ -86,12 +86,13 @@
             raise
 
 def uisetup(ui):
-    if ui.plain():
+    if ui.plain() or '--debugger' in sys.argv or not util.isatty(sys.stdout):
         return
 
     def pagecmd(orig, ui, options, cmd, cmdfunc):
         p = ui.config("pager", "pager", os.environ.get("PAGER"))
-        if p and sys.stdout.isatty() and '--debugger' not in sys.argv:
+
+        if p:
             attend = ui.configlist('pager', 'attend', attended)
             auto = options['pager'] == 'auto'
             always = util.parsebool(options['pager'])
--- a/hgext/progress.py	Thu Jun 02 18:52:31 2011 +0800
+++ b/hgext/progress.py	Thu Jun 02 00:43:34 2011 +0300
@@ -46,14 +46,14 @@
 import sys
 import time
 
+from mercurial import util
 from mercurial.i18n import _
 
 def spacejoin(*args):
     return ' '.join(s for s in args if s)
 
 def shouldprint(ui):
-    return (getattr(sys.stderr, 'isatty', None) and
-            (sys.stderr.isatty() or ui.configbool('progress', 'assume-tty')))
+    return (util.isatty(sys.stderr) or ui.configbool('progress', 'assume-tty'))
 
 def fmtremaining(seconds):
     if seconds < 60:
--- a/mercurial/ui.py	Thu Jun 02 18:52:31 2011 +0800
+++ b/mercurial/ui.py	Thu Jun 02 00:43:34 2011 +0300
@@ -473,12 +473,9 @@
         '''
         i = self.configbool("ui", "interactive", None)
         if i is None:
-            try:
-                return sys.stdin.isatty()
-            except AttributeError:
-                # some environments replace stdin without implementing isatty
-                # usually those are non-interactive
-                return False
+            # some environments replace stdin without implementing isatty
+            # usually those are non-interactive
+            return util.isatty(sys.stdin)
 
         return i
 
@@ -514,17 +511,14 @@
 
         i = self.configbool("ui", "formatted", None)
         if i is None:
-            try:
-                return sys.stdout.isatty()
-            except AttributeError:
-                # some environments replace stdout without implementing isatty
-                # usually those are non-interactive
-                return False
+            # some environments replace stdout without implementing isatty
+            # usually those are non-interactive
+            return util.isatty(sys.stdout)
 
         return i
 
     def _readline(self, prompt=''):
-        if sys.stdin.isatty():
+        if util.isatty(sys.stdin):
             try:
                 # magically add command line editing support, where
                 # available
--- a/mercurial/util.py	Thu Jun 02 18:52:31 2011 +0800
+++ b/mercurial/util.py	Thu Jun 02 00:43:34 2011 +0300
@@ -1591,3 +1591,9 @@
     u = url(u)
     u.user = u.passwd = None
     return str(u)
+
+def isatty(fd):
+    try:
+        return fd.isatty()
+    except AttributeError:
+        return False