Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/IPython/winconsole.py
blob: 91cb26cdc24641998020e544454c89089cc47cf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Set of functions to work with console on Windows.
"""

#*****************************************************************************
#       Copyright (C) 2005 Alexander Belchenko <bialix@ukr.net>
#
#                This file is placed in the public domain.
#
#*****************************************************************************

__author__  = 'Alexander Belchenko (e-mail: bialix AT ukr.net)'
__license__ = 'Public domain'

import struct

try:
    import ctypes
except ImportError:
    ctypes = None

def get_console_size(defaultx=80, defaulty=25):
    """ Return size of current console.

    This function try to determine actual size of current working
    console window and return tuple (sizex, sizey) if success,
    or default size (defaultx, defaulty) otherwise.

    Dependencies: ctypes should be installed.
    """
    if ctypes is None:
        # no ctypes is found
        return (defaultx, defaulty)

    h = ctypes.windll.kernel32.GetStdHandle(-11)
    csbi = ctypes.create_string_buffer(22)
    res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
    
    if res:
        (bufx, bufy, curx, cury, wattr,
         left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh",
                                                               csbi.raw)
        sizex = right - left + 1
        sizey = bottom - top + 1
        return (sizex, sizey)
    else:
        return (defaultx, defaulty)