[PATCH] churn: simplify code to get terminal width

Christian Ebert blacktrash at gmx.net
Wed Sep 12 05:59:07 CDT 2007


Resubmitting in case it has been forgotten.

# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1189594476 -7200
# Node ID 60346ca9b033a941c4318816f368cecd1337a75d
# Parent  81575b7b505e49dcb5744fc857286c0de3a36dbc
churn: simplify code to get terminal width

diff --git a/contrib/churn.py b/contrib/churn.py
--- a/contrib/churn.py
+++ b/contrib/churn.py
@@ -22,20 +22,16 @@ def get_tty_width():
         except ValueError:
             pass
     try:
-        import termios, fcntl, struct
-        buf = 'abcd'
+        import termios, array, fcntl
         for dev in (sys.stdout, sys.stdin):
             try:
-                if buf != 'abcd':
-                    break
                 fd = dev.fileno()
                 if not os.isatty(fd):
                     continue
-                buf = fcntl.ioctl(fd, termios.TIOCGWINSZ, buf)
+                arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
+                return array.array('h', arri)[1]
             except ValueError:
                 pass
-        if buf != 'abcd':
-           return struct.unpack('hh', buf)[1]
     except ImportError:
         pass
     return 80



More information about the Mercurial-devel mailing list