# HG changeset patch # User Idan Kamara # Date 2011-06-01 21:43:34 # Node ID 76f295eaed8614e14a13f587df1a3ac532cae3f5 # Parent 175e4b9d8a9624100972ee7faf2183c70e8e35b1 util: add helper function isatty(fd) to check for tty-ness diff --git a/hgext/pager.py b/hgext/pager.py --- a/hgext/pager.py +++ b/hgext/pager.py @@ -60,7 +60,7 @@ from mercurial.i18n import _ 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 @@ def _runpager(p): 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 @@ def _runpager(p): 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 --git a/hgext/progress.py b/hgext/progress.py --- a/hgext/progress.py +++ b/hgext/progress.py @@ -46,14 +46,14 @@ characters. 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 --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -473,12 +473,9 @@ class ui(object): ''' 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 @@ class ui(object): 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 --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -1591,3 +1591,9 @@ def removeauth(u): u = url(u) u.user = u.passwd = None return str(u) + +def isatty(fd): + try: + return fd.isatty() + except AttributeError: + return False