mercurial/util.py
changeset 7547 4949729ee9ee
parent 7537 9e186bda013d
child 7559 016a7319e76b
--- a/mercurial/util.py	Sun Dec 28 19:59:42 2008 +0100
+++ b/mercurial/util.py	Thu Dec 25 10:48:24 2008 +0200
@@ -1985,3 +1985,24 @@
 def uirepr(s):
     # Avoid double backslash in Windows path repr()
     return repr(s).replace('\\\\', '\\')
+
+def termwidth():
+    if 'COLUMNS' in os.environ:
+        try:
+            return int(os.environ['COLUMNS'])
+        except ValueError:
+            pass
+    try:
+        import termios, array, fcntl
+        for dev in (sys.stdout, sys.stdin):
+            try:
+                fd = dev.fileno()
+                if not os.isatty(fd):
+                    continue
+                arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
+                return array.array('h', arri)[1]
+            except ValueError:
+                pass
+    except ImportError:
+        pass
+    return 80