3
Set of functions to work with console on Windows.
 
 
4
Author: Alexander Belchenko (e-mail: bialix AT ukr.net)
 
 
16
WIN32_STDIN_HANDLE = -10
 
 
17
WIN32_STDOUT_HANDLE = -11
 
 
18
WIN32_STDERR_HANDLE = -12
 
 
21
def get_console_size(defaultx=80, defaulty=25):
 
 
22
   """ Return size of current console.
 
 
24
   This function try to determine actual size of current working
 
 
25
   console window and return tuple (sizex, sizey) if success,
 
 
26
   or default size (defaultx, defaulty) otherwise.
 
 
28
   Dependencies: ctypes should be installed.
 
 
32
       return (defaultx, defaulty)
 
 
34
   # To avoid problem with redirecting output via pipe
 
 
35
   # need to use stderr instead of stdout
 
 
36
   h = ctypes.windll.kernel32.GetStdHandle(WIN32_STDERR_HANDLE)
 
 
37
   csbi = ctypes.create_string_buffer(22)
 
 
38
   res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
 
 
41
       (bufx, bufy, curx, cury, wattr,
 
 
42
        left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
 
 
43
       sizex = right - left + 1
 
 
44
       sizey = bottom - top + 1
 
 
47
       return (defaultx, defaulty)