~bzr-pqm/bzr/bzr.dev

1185.16.86 by mbp at sourcefrog
- win32 get_console_size from Alexander
1
"""
2
Set of functions to work with console on Windows.
3
Author: Alexander Belchenko (e-mail: bialix AT ukr.net)
4
License: Public domain
5
"""
6
7
import struct
8
9
try:
10
   import ctypes
11
except ImportError:
12
   ctypes = None
13
14
15
def get_console_size(defaultx=80, defaulty=25):
16
   """ Return size of current console.
17
18
   This function try to determine actual size of current working
19
   console window and return tuple (sizex, sizey) if success,
20
   or default size (defaultx, defaulty) otherwise.
21
22
   Dependencies: ctypes should be installed.
23
   """
24
   if ctypes is None:
25
       # no ctypes is found
26
       return (defaultx, defaulty)
27
28
   h = ctypes.windll.kernel32.GetStdHandle(-11)
29
   csbi = ctypes.create_string_buffer(22)
30
   res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
31
32
   if res:
33
       (bufx, bufy, curx, cury, wattr,
34
        left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
35
       sizex = right - left + 1
36
       sizey = bottom - top + 1
37
       return (sizex, sizey)
38
   else:
39
       return (defaultx, defaulty)
40