~ellisonbg/ipython/trunk-dev

« back to all changes in this revision

Viewing changes to IPython/utils/terminal.py

  • Committer: Brian Granger
  • Date: 2010-01-31 23:57:46 UTC
  • mfrom: (1109.1.113 ipython)
  • Revision ID: ellisonbg@gmail.com-20100131235746-rj81qa8klooepq2m
Merging from upstream trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# encoding: utf-8
 
2
"""
 
3
Utilities for working with terminals.
 
4
 
 
5
Authors:
 
6
 
 
7
* Brian E. Granger
 
8
* Fernando Perez
 
9
* Alexander Belchenko (e-mail: bialix AT ukr.net)
 
10
"""
 
11
 
 
12
#-----------------------------------------------------------------------------
 
13
#  Copyright (C) 2008-2009  The IPython Development Team
 
14
#
 
15
#  Distributed under the terms of the BSD License.  The full license is in
 
16
#  the file COPYING, distributed as part of this software.
 
17
#-----------------------------------------------------------------------------
 
18
 
 
19
#-----------------------------------------------------------------------------
 
20
# Imports
 
21
#-----------------------------------------------------------------------------
 
22
 
 
23
import os
 
24
import struct
 
25
import sys
 
26
import warnings
 
27
 
 
28
#-----------------------------------------------------------------------------
 
29
# Code
 
30
#-----------------------------------------------------------------------------
 
31
 
 
32
# This variable is part of the expected API of the module:
 
33
ignore_termtitle = True
 
34
 
 
35
 
 
36
def _term_clear():
 
37
    pass
 
38
 
 
39
 
 
40
if os.name == 'posix':
 
41
    def _term_clear():
 
42
        os.system('clear')
 
43
 
 
44
 
 
45
if sys.platform == 'win32':
 
46
    def _term_clear():
 
47
        os.system('cls')
 
48
 
 
49
 
 
50
def term_clear():
 
51
    _term_clear()
 
52
 
 
53
 
 
54
def toggle_set_term_title(val):
 
55
    """Control whether set_term_title is active or not.
 
56
 
 
57
    set_term_title() allows writing to the console titlebar.  In embedded
 
58
    widgets this can cause problems, so this call can be used to toggle it on
 
59
    or off as needed.
 
60
 
 
61
    The default state of the module is for the function to be disabled.
 
62
 
 
63
    Parameters
 
64
    ----------
 
65
      val : bool
 
66
        If True, set_term_title() actually writes to the terminal (using the
 
67
        appropriate platform-specific module).  If False, it is a no-op.
 
68
    """
 
69
    global ignore_termtitle
 
70
    ignore_termtitle = not(val)
 
71
 
 
72
 
 
73
def _set_term_title(*args,**kw):
 
74
    """Dummy no-op."""
 
75
    pass
 
76
 
 
77
 
 
78
def _set_term_title_xterm(title):
 
79
    """ Change virtual terminal title in xterm-workalikes """
 
80
    sys.stdout.write('\033]0;%s\007' % title)
 
81
 
 
82
if os.name == 'posix':
 
83
    TERM = os.environ.get('TERM','')
 
84
    if (TERM == 'xterm') or (TERM == 'xterm-color'):
 
85
        _set_term_title = _set_term_title_xterm
 
86
 
 
87
 
 
88
if sys.platform == 'win32':
 
89
    try:
 
90
        import ctypes
 
91
 
 
92
        SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW
 
93
        SetConsoleTitleW.argtypes = [ctypes.c_wchar_p]
 
94
    
 
95
        def _set_term_title(title):
 
96
            """Set terminal title using ctypes to access the Win32 APIs."""
 
97
            SetConsoleTitleW(title)
 
98
    except ImportError:
 
99
        def _set_term_title(title):
 
100
            """Set terminal title using the 'title' command."""
 
101
            global ignore_termtitle
 
102
 
 
103
            try:
 
104
                # Cannot be on network share when issuing system commands
 
105
                curr = os.getcwd()
 
106
                os.chdir("C:")
 
107
                ret = os.system("title " + title)
 
108
            finally:
 
109
                os.chdir(curr)
 
110
            if ret:
 
111
                # non-zero return code signals error, don't try again
 
112
                ignore_termtitle = True
 
113
 
 
114
 
 
115
def set_term_title(title):
 
116
    """Set terminal title using the necessary platform-dependent calls."""
 
117
    if ignore_termtitle:
 
118
        return
 
119
    _set_term_title(title)
 
120
 
 
121
 
 
122
def freeze_term_title():
 
123
    warnings.warn("This function is deprecated, use toggle_set_term_title()")
 
124
    global ignore_termtitle
 
125
    ignore_termtitle = True
 
126
 
 
127
 
 
128
def get_terminal_size(defaultx=80, defaulty=25):
 
129
    return defaultx, defaulty
 
130
 
 
131
 
 
132
if sys.platform == 'win32':
 
133
    def get_terminal_size(defaultx=80, defaulty=25):
 
134
        """Return size of current terminal console.
 
135
 
 
136
        This function try to determine actual size of current working
 
137
        console window and return tuple (sizex, sizey) if success,
 
138
        or default size (defaultx, defaulty) otherwise.
 
139
 
 
140
        Dependencies: ctypes should be installed.
 
141
 
 
142
        Author: Alexander Belchenko (e-mail: bialix AT ukr.net)
 
143
        """
 
144
        try:
 
145
            import ctypes
 
146
        except ImportError:
 
147
            return defaultx, defaulty
 
148
 
 
149
        h = ctypes.windll.kernel32.GetStdHandle(-11)
 
150
        csbi = ctypes.create_string_buffer(22)
 
151
        res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
 
152
 
 
153
        if res:
 
154
            (bufx, bufy, curx, cury, wattr,
 
155
             left, top, right, bottom, maxx, maxy) = struct.unpack(
 
156
                "hhhhHhhhhhh", csbi.raw)
 
157
            sizex = right - left + 1
 
158
            sizey = bottom - top + 1
 
159
            return (sizex, sizey)
 
160
        else:
 
161
            return (defaultx, defaulty)
 
162