~ipython-contrib/+junk/ipython-zmq

« back to all changes in this revision

Viewing changes to IPython/winconsole.py

  • Committer: ville
  • Date: 2008-02-16 09:50:47 UTC
  • mto: (0.12.1 ipython_main)
  • mto: This revision was merged to the branch mainline in revision 990.
  • Revision ID: ville@ville-pc-20080216095047-500x6dluki1iz40o
initialization (no svn history)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Set of functions to work with console on Windows.
 
2
"""
 
3
 
 
4
#*****************************************************************************
 
5
#       Copyright (C) 2005 Alexander Belchenko <bialix@ukr.net>
 
6
#
 
7
#                This file is placed in the public domain.
 
8
#
 
9
#*****************************************************************************
 
10
 
 
11
__author__  = 'Alexander Belchenko (e-mail: bialix AT ukr.net)'
 
12
__license__ = 'Public domain'
 
13
 
 
14
import struct
 
15
 
 
16
try:
 
17
    import ctypes
 
18
except ImportError:
 
19
    ctypes = None
 
20
 
 
21
def get_console_size(defaultx=80, defaulty=25):
 
22
    """ Return size of current console.
 
23
 
 
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.
 
27
 
 
28
    Dependencies: ctypes should be installed.
 
29
    """
 
30
    if ctypes is None:
 
31
        # no ctypes is found
 
32
        return (defaultx, defaulty)
 
33
 
 
34
    h = ctypes.windll.kernel32.GetStdHandle(-11)
 
35
    csbi = ctypes.create_string_buffer(22)
 
36
    res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
 
37
    
 
38
    if res:
 
39
        (bufx, bufy, curx, cury, wattr,
 
40
         left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh",
 
41
                                                               csbi.raw)
 
42
        sizex = right - left + 1
 
43
        sizey = bottom - top + 1
 
44
        return (sizex, sizey)
 
45
    else:
 
46
        return (defaultx, defaulty)