##// END OF EJS Templates
Merge pull request #10587 from Carreau/vendor-terminal-size...
Thomas Kluyver -
r23698:b8b3943a merge
parent child Browse files
Show More
@@ -0,0 +1,131 b''
1 # vendored version of backports.get_terminal_size as nemesapece package are a
2 # mess and break, especially on ubuntu. This file is under MIT Licence.
3 # See https://pypi.python.org/pypi/backports.shutil_get_terminal_size
4 #
5 # commit: afc5714b1545a5a3aa44cfb5e078d39165bf76ab (Feb 20, 2016)
6 # from
7 # https://github.com/chrippa/backports.shutil_get_terminal_size
8 #
9 # The MIT License (MIT)
10 #
11 # Copyright (c) 2014 Christopher Rosell
12 #
13 # Permission is hereby granted, free of charge, to any person obtaining a copy
14 # of this software and associated documentation files (the "Software"), to deal
15 # in the Software without restriction, including without limitation the rights
16 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 # copies of the Software, and to permit persons to whom the Software is
18 # furnished to do so, subject to the following conditions:
19 #
20 # The above copyright notice and this permission notice shall be included in
21 # all copies or substantial portions of the Software.
22 #
23 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 # THE SOFTWARE.
30 #
31 """This is a backport of shutil.get_terminal_size from Python 3.3.
32
33 The original implementation is in C, but here we use the ctypes and
34 fcntl modules to create a pure Python version of os.get_terminal_size.
35 """
36
37 import os
38 import struct
39 import sys
40
41 from collections import namedtuple
42
43 __all__ = ["get_terminal_size"]
44
45
46 terminal_size = namedtuple("terminal_size", "columns lines")
47
48 try:
49 from ctypes import windll, create_string_buffer, WinError
50
51 _handle_ids = {
52 0: -10,
53 1: -11,
54 2: -12,
55 }
56
57 def _get_terminal_size(fd):
58 handle = windll.kernel32.GetStdHandle(_handle_ids[fd])
59 if handle == 0:
60 raise OSError('handle cannot be retrieved')
61 if handle == -1:
62 raise WinError()
63 csbi = create_string_buffer(22)
64 res = windll.kernel32.GetConsoleScreenBufferInfo(handle, csbi)
65 if res:
66 res = struct.unpack("hhhhHhhhhhh", csbi.raw)
67 left, top, right, bottom = res[5:9]
68 columns = right - left + 1
69 lines = bottom - top + 1
70 return terminal_size(columns, lines)
71 else:
72 raise WinError()
73
74 except ImportError:
75 import fcntl
76 import termios
77
78 def _get_terminal_size(fd):
79 try:
80 res = fcntl.ioctl(fd, termios.TIOCGWINSZ, b"\x00" * 4)
81 except IOError as e:
82 raise OSError(e)
83 lines, columns = struct.unpack("hh", res)
84
85 return terminal_size(columns, lines)
86
87
88 def get_terminal_size(fallback=(80, 24)):
89 """Get the size of the terminal window.
90
91 For each of the two dimensions, the environment variable, COLUMNS
92 and LINES respectively, is checked. If the variable is defined and
93 the value is a positive integer, it is used.
94
95 When COLUMNS or LINES is not defined, which is the common case,
96 the terminal connected to sys.__stdout__ is queried
97 by invoking os.get_terminal_size.
98
99 If the terminal size cannot be successfully queried, either because
100 the system doesn't support querying, or because we are not
101 connected to a terminal, the value given in fallback parameter
102 is used. Fallback defaults to (80, 24) which is the default
103 size used by many terminal emulators.
104
105 The value returned is a named tuple of type os.terminal_size.
106 """
107 # Try the environment first
108 try:
109 columns = int(os.environ["COLUMNS"])
110 except (KeyError, ValueError):
111 columns = 0
112
113 try:
114 lines = int(os.environ["LINES"])
115 except (KeyError, ValueError):
116 lines = 0
117
118 # Only query if necessary
119 if columns <= 0 or lines <= 0:
120 try:
121 size = _get_terminal_size(sys.__stdout__.fileno())
122 except (NameError, OSError):
123 size = terminal_size(*fallback)
124
125 if columns <= 0:
126 columns = size.columns
127 if lines <= 0:
128 lines = size.lines
129
130 return terminal_size(columns, lines)
131
@@ -9,6 +9,8 b' Authors:'
9 9 * Alexander Belchenko (e-mail: bialix AT ukr.net)
10 10 """
11 11
12 from __future__ import absolute_import
13
12 14 # Copyright (c) IPython Development Team.
13 15 # Distributed under the terms of the Modified BSD License.
14 16
@@ -19,7 +21,10 b' try:'
19 21 from shutil import get_terminal_size as _get_terminal_size
20 22 except ImportError:
21 23 # use backport on Python 2
24 try:
22 25 from backports.shutil_get_terminal_size import get_terminal_size as _get_terminal_size
26 except ImportError:
27 from ._get_terminal_size import _get_terminal_size
23 28
24 29 from . import py3compat
25 30
General Comments 0
You need to be logged in to leave comments. Login now