3
Set of functions to work with console on Windows.
4
Author: Alexander Belchenko (e-mail: bialix AT ukr.net)
10
# We can cope without it; use a separate variable to help pyflakes
18
WIN32_STDIN_HANDLE = -10
19
WIN32_STDOUT_HANDLE = -11
20
WIN32_STDERR_HANDLE = -12
23
def get_console_size(defaultx=80, defaulty=25):
24
""" Return size of current console.
26
This function try to determine actual size of current working
27
console window and return tuple (sizex, sizey) if success,
28
or default size (defaultx, defaulty) otherwise.
30
Dependencies: ctypes should be installed.
34
return (defaultx, defaulty)
36
# To avoid problem with redirecting output via pipe
37
# need to use stderr instead of stdout
38
h = ctypes.windll.kernel32.GetStdHandle(WIN32_STDERR_HANDLE)
39
csbi = ctypes.create_string_buffer(22)
40
res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
43
(bufx, bufy, curx, cury, wattr,
44
left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
45
sizex = right - left + 1
46
sizey = bottom - top + 1
49
return (defaultx, defaulty)