[PATCH] util: add helper function isatty(fd) to check for tty-ness

Idan Kamara idankk86 at gmail.com
Thu Jun 2 11:28:51 CDT 2011


# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1306964614 -10800
# Node ID d12139042af43407d347d812b9dcd3cbdc103966
# Parent  970d8d9b69004d550ec5e33f529de118033047ab
util: add helper function isatty(fd) to check for tty-ness

diff -r 970d8d9b6900 -r d12139042af4 hgext/pager.py
--- a/hgext/pager.py	Thu Jun 02 19:16:45 2011 +0300
+++ 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'])
diff -r 970d8d9b6900 -r d12139042af4 hgext/progress.py
--- a/hgext/progress.py	Thu Jun 02 19:16:45 2011 +0300
+++ 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:
diff -r 970d8d9b6900 -r d12139042af4 mercurial/ui.py
--- a/mercurial/ui.py	Thu Jun 02 19:16:45 2011 +0300
+++ 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
diff -r 970d8d9b6900 -r d12139042af4 mercurial/util.py
--- a/mercurial/util.py	Thu Jun 02 19:16:45 2011 +0300
+++ 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


More information about the Mercurial-devel mailing list