changeset 11012:81631f0cf13b

win32: detect console width on Windows Original version by anatoly techtonik <techtonik@gmail.com> Following advices from similar bzr code.
author Patrick Mezard <pmezard@gmail.com>
date Sun, 25 Apr 2010 18:27:12 +0200
parents 648130161e4d
children 45351e65230b
files mercurial/win32.py
diffstat 1 files changed, 14 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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