Show More
@@ -0,0 +1,103 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 | """This is a backport of shutil.get_terminal_size from Python 3.3. | |||
|
5 | ||||
|
6 | The original implementation is in C, but here we use the ctypes and | |||
|
7 | fcntl modules to create a pure Python version of os.get_terminal_size. | |||
|
8 | """ | |||
|
9 | ||||
|
10 | import os | |||
|
11 | import struct | |||
|
12 | import sys | |||
|
13 | ||||
|
14 | from collections import namedtuple | |||
|
15 | ||||
|
16 | __all__ = ["get_terminal_size"] | |||
|
17 | ||||
|
18 | ||||
|
19 | terminal_size = namedtuple("terminal_size", "columns lines") | |||
|
20 | ||||
|
21 | try: | |||
|
22 | from ctypes import windll, create_string_buffer | |||
|
23 | ||||
|
24 | _handles = { | |||
|
25 | 0: windll.kernel32.GetStdHandle(-10), | |||
|
26 | 1: windll.kernel32.GetStdHandle(-11), | |||
|
27 | 2: windll.kernel32.GetStdHandle(-12), | |||
|
28 | } | |||
|
29 | ||||
|
30 | def _get_terminal_size(fd): | |||
|
31 | columns = lines = 0 | |||
|
32 | ||||
|
33 | try: | |||
|
34 | handle = _handles[fd] | |||
|
35 | csbi = create_string_buffer(22) | |||
|
36 | res = windll.kernel32.GetConsoleScreenBufferInfo(handle, csbi) | |||
|
37 | if res: | |||
|
38 | res = struct.unpack("hhhhHhhhhhh", csbi.raw) | |||
|
39 | left, top, right, bottom = res[5:9] | |||
|
40 | columns = right - left + 1 | |||
|
41 | lines = bottom - top + 1 | |||
|
42 | except Exception: | |||
|
43 | pass | |||
|
44 | ||||
|
45 | return terminal_size(columns, lines) | |||
|
46 | ||||
|
47 | except ImportError: | |||
|
48 | import fcntl | |||
|
49 | import termios | |||
|
50 | ||||
|
51 | def _get_terminal_size(fd): | |||
|
52 | try: | |||
|
53 | res = fcntl.ioctl(fd, termios.TIOCGWINSZ, b"\x00" * 4) | |||
|
54 | lines, columns = struct.unpack("hh", res) | |||
|
55 | except Exception: | |||
|
56 | columns = lines = 0 | |||
|
57 | ||||
|
58 | return terminal_size(columns, lines) | |||
|
59 | ||||
|
60 | ||||
|
61 | def get_terminal_size(fallback=(80, 24)): | |||
|
62 | """Get the size of the terminal window. | |||
|
63 | ||||
|
64 | For each of the two dimensions, the environment variable, COLUMNS | |||
|
65 | and LINES respectively, is checked. If the variable is defined and | |||
|
66 | the value is a positive integer, it is used. | |||
|
67 | ||||
|
68 | When COLUMNS or LINES is not defined, which is the common case, | |||
|
69 | the terminal connected to sys.__stdout__ is queried | |||
|
70 | by invoking os.get_terminal_size. | |||
|
71 | ||||
|
72 | If the terminal size cannot be successfully queried, either because | |||
|
73 | the system doesn't support querying, or because we are not | |||
|
74 | connected to a terminal, the value given in fallback parameter | |||
|
75 | is used. Fallback defaults to (80, 24) which is the default | |||
|
76 | size used by many terminal emulators. | |||
|
77 | ||||
|
78 | The value returned is a named tuple of type os.terminal_size. | |||
|
79 | """ | |||
|
80 | # Try the environment first | |||
|
81 | try: | |||
|
82 | columns = int(os.environ["COLUMNS"]) | |||
|
83 | except (KeyError, ValueError): | |||
|
84 | columns = 0 | |||
|
85 | ||||
|
86 | try: | |||
|
87 | lines = int(os.environ["LINES"]) | |||
|
88 | except (KeyError, ValueError): | |||
|
89 | lines = 0 | |||
|
90 | ||||
|
91 | # Only query if necessary | |||
|
92 | if columns <= 0 or lines <= 0: | |||
|
93 | try: | |||
|
94 | size = _get_terminal_size(sys.__stdout__.fileno()) | |||
|
95 | except (NameError, OSError): | |||
|
96 | size = terminal_size(*fallback) | |||
|
97 | ||||
|
98 | if columns <= 0: | |||
|
99 | columns = size.columns | |||
|
100 | if lines <= 0: | |||
|
101 | lines = size.lines | |||
|
102 | ||||
|
103 | return terminal_size(columns, lines) |
@@ -9,6 +9,8 b' Authors:' | |||||
9 | * Alexander Belchenko (e-mail: bialix AT ukr.net) |
|
9 | * Alexander Belchenko (e-mail: bialix AT ukr.net) | |
10 | """ |
|
10 | """ | |
11 |
|
11 | |||
|
12 | from __future__ import absolute_import | |||
|
13 | ||||
12 | # Copyright (c) IPython Development Team. |
|
14 | # Copyright (c) IPython Development Team. | |
13 | # Distributed under the terms of the Modified BSD License. |
|
15 | # Distributed under the terms of the Modified BSD License. | |
14 |
|
16 | |||
@@ -19,7 +21,10 b' try:' | |||||
19 | from shutil import get_terminal_size as _get_terminal_size |
|
21 | from shutil import get_terminal_size as _get_terminal_size | |
20 | except ImportError: |
|
22 | except ImportError: | |
21 | # use backport on Python 2 |
|
23 | # use backport on Python 2 | |
|
24 | try: | |||
22 | from backports.shutil_get_terminal_size import get_terminal_size as _get_terminal_size |
|
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 | from . import py3compat |
|
29 | from . import py3compat | |
25 |
|
30 |
General Comments 0
You need to be logged in to leave comments.
Login now