win32: detect console width on Windows
authorPatrick Mezard <pmezard@gmail.com>
Sun, 25 Apr 2010 18:27:12 +0200
changeset 11012 81631f0cf13b
parent 11011 648130161e4d
child 11013 45351e65230b
win32: detect console width on Windows Original version by anatoly techtonik <techtonik@gmail.com> Following advices from similar bzr code.
mercurial/win32.py
--- a/mercurial/win32.py	Mon Apr 26 22:42:46 2010 +0200
+++ b/mercurial/win32.py	Sun Apr 25 18:27:12 2010 +0200
@@ -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,16 @@
 
     pid =  win32process.GetCurrentProcessId()
     win32gui.EnumWindows(callback, pid)
+
+def termwidth_():
+    try:
+        # Query stderr to avoid problems with redirections
+        screenbuf = win32console.GetStdHandle(win32console.STD_ERROR_HANDLE)
+        try:
+            window = screenbuf.GetConsoleScreenBufferInfo()['Window']
+            width = window.Right - window.Left
+            return width
+        finally:
+            screenbuf.Detach()
+    except pywintypes.error:
+        return 79