[PATCH] Detect console width on Windows

anatoly techtonik techtonik at gmail.com
Sun Apr 25 03:44:51 CDT 2010


On Sun, Apr 25, 2010 at 11:01 AM, Dirkjan Ochtman <dirkjan at ochtman.nl> wrote:
>
> You should probably just move the current termwidth() to posix.py and
> implement your new termwidth() in win32.py. Don't call it
> get_console_width(), that doesn't mash well with our coding style (see
> the CodingStyle page in the wiki).

Done.

# HG changeset patch
# Parent 33119d0252c1aa46da82a22aeb067fcd172ce804
# User anatoly techtonik <techtonik at gmail.com>
Detect console width on Windows

diff -r 33119d0252c1 mercurial/posix.py
--- a/mercurial/posix.py	Sun Apr 18 14:20:08 2010 -0700
+++ b/mercurial/posix.py	Sun Apr 25 11:40:21 2010 +0300
@@ -264,3 +264,32 @@

 def gethgcmd():
     return sys.argv[:1]
+
+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.stderr, sys.stdout, sys.stdin):
+            try:
+                try:
+                    fd = dev.fileno()
+                except AttributeError:
+                    continue
+                if not os.isatty(fd):
+                    continue
+                arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
+                return array.array('h', arri)[1]
+            except ValueError:
+                pass
+            except IOError, e:
+                if e[0] == errno.EINVAL:
+                    pass
+                else:
+                    raise
+    except ImportError:
+        pass
+    return 80
diff -r 33119d0252c1 mercurial/util.py
--- a/mercurial/util.py	Sun Apr 18 14:20:08 2010 -0700
+++ b/mercurial/util.py	Sun Apr 25 11:40:21 2010 +0300
@@ -546,6 +546,15 @@
     """
     pass

+def termwidth():
+    """Detect width of console, return 80 if not detected"""
+    if 'COLUMNS' in os.environ:
+        try:
+            return int(os.environ['COLUMNS'])
+        except ValueError:
+            pass
+    return 80
+
 if os.name == 'nt':
     from windows import *
 else:
@@ -1245,35 +1254,6 @@
     # 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.stderr, sys.stdout, sys.stdin):
-            try:
-                try:
-                    fd = dev.fileno()
-                except AttributeError:
-                    continue
-                if not os.isatty(fd):
-                    continue
-                arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
-                return array.array('h', arri)[1]
-            except ValueError:
-                pass
-            except IOError, e:
-                if e[0] == errno.EINVAL:
-                    pass
-                else:
-                    raise
-    except ImportError:
-        pass
-    return 80
-
 def wrap(line, hangindent, width=None):
     if width is None:
         width = termwidth() - 2
diff -r 33119d0252c1 mercurial/win32.py
--- a/mercurial/win32.py	Sun Apr 18 14:20:08 2010 -0700
+++ b/mercurial/win32.py	Sun Apr 25 11:40:21 2010 +0300
@@ -16,7 +16,7 @@
 import win32api

 import errno, os, sys, pywintypes, win32con, win32file, win32process
-import winerror, win32gui
+import winerror, win32gui, win32console
 import osutil, encoding
 from win32com.shell import shell, shellcon

@@ -189,3 +189,14 @@

     pid =  win32process.GetCurrentProcessId()
     win32gui.EnumWindows(callback, pid)
+
+def termwidth():
+    screenbuf = win32console.GetStdHandle(win32console.STD_OUTPUT_HANDLE)
+    width = 80
+    try:
+        width = screenbuf.GetConsoleScreenBufferInfo()['Window'].Right
+        width += 1
+    except pywintypes.error:
+        pass
+    screenbuf.Detach()
+    return width

-- 
anatoly t.


More information about the Mercurial-devel mailing list