7
7
#
8
8
# Copyright © 2014-2020 Dominic Davis-Foster <[email protected] >
9
9
#
10
- # get_terminal_size, _get_terminal_size_windows, _get_terminal_size_tput and _get_terminal_size_posix
11
- # from https://gist.github.com/jtriley/1108174
12
- # Copyright © 2011 jtriley
13
- #
14
10
# Parts of the docstrings based on the Python 3.8.2 Documentation
15
11
# Licensed under the Python Software Foundation License Version 2.
16
12
# Copyright © 2001-2020 Python Software Foundation. All rights reserved.
62
58
# stdlib
63
59
import inspect
64
60
import os
65
- import platform
66
61
import pprint
67
- import shlex
68
- import struct
69
- import subprocess
62
+ import shutil
70
63
import textwrap
71
- from typing import Any , Optional , Tuple
64
+ from typing import Tuple
65
+
66
+ # this package
67
+ from domdf_python_tools import __version__
68
+ from domdf_python_tools .utils import deprecated
72
69
73
70
__all__ = [
74
71
"clear" ,
@@ -139,6 +136,12 @@ def overtype(*objects, sep: str = ' ', end: str = '', file=None, flush: bool = F
139
136
print (* objects , sep = sep , end = end , file = file , flush = flush )
140
137
141
138
139
+ @deprecated (
140
+ deprecated_in = "1.0.0" ,
141
+ removed_in = "2.0.0" ,
142
+ current_version = __version__ ,
143
+ details = "Use :func:`shutil.get_terminal_size` instead." ,
144
+ )
142
145
def get_terminal_size () -> Tuple [int , int ]: # pragma: no cover
143
146
"""
144
147
Get width and height of console.
@@ -148,94 +151,7 @@ def get_terminal_size() -> Tuple[int, int]: # pragma: no cover
148
151
:return: Screen width and screen height.
149
152
"""
150
153
151
- current_os = platform .system ()
152
- tuple_xy = None
153
-
154
- if current_os == "Windows" :
155
- tuple_xy = _get_terminal_size_windows ()
156
- if tuple_xy is None :
157
- tuple_xy = _get_terminal_size_tput ()
158
- # needed for window's python in cygwin's xterm!
159
-
160
- if current_os in {"Linux" , "Darwin" } or current_os .startswith ("CYGWIN" ):
161
- tuple_xy = _get_terminal_size_posix ()
162
-
163
- if tuple_xy is None :
164
- print ("default" )
165
- tuple_xy = (80 , 25 ) # default value
166
-
167
- return tuple_xy
168
-
169
-
170
- def _get_terminal_size_windows () -> Optional [Tuple [int , int ]]: # pragma: no cover
171
- try :
172
-
173
- # stdlib
174
- from ctypes import create_string_buffer , windll # type: ignore
175
-
176
- # stdin handle is -10
177
- # stdout handle is -11
178
- # stderr handle is -12
179
- h = windll .kernel32 .GetStdHandle (- 12 )
180
- csbi = create_string_buffer (22 )
181
- res = windll .kernel32 .GetConsoleScreenBufferInfo (h , csbi )
182
-
183
- if res :
184
- (buf_x , buf_y , cur_x , cur_y , wattr , left , top , right , bottom , maxx ,
185
- maxy ) = struct .unpack ("hhhhHhhhhhh" , csbi .raw )
186
- size_x = right - left + 1
187
- size_y = bottom - top + 1
188
-
189
- return size_x , size_y
190
- except Exception : # nosec: B110
191
- pass
192
-
193
- return None
194
-
195
-
196
- def _get_terminal_size_tput () -> Optional [Tuple [int , int ]]: # pragma: no cover
197
- # get terminal width
198
- # src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window
199
- try :
200
- cols = int (subprocess .check_call (shlex .split ("tput cols" )))
201
- rows = int (subprocess .check_call (shlex .split ("tput lines" )))
202
- return cols , rows
203
- except Exception : # nosec: B110
204
- return None
205
-
206
-
207
- def _get_terminal_size_posix () -> Optional [Tuple [int , int ]]: # pragma: no cover
208
-
209
- # stdlib
210
- import fcntl
211
- import termios
212
-
213
- def ioctl_GWINSZ (fd : int ) -> Optional [Tuple [Any , ...]]:
214
- try :
215
- cr = struct .unpack ("hh" , fcntl .ioctl (fd , termios .TIOCGWINSZ , b"1234" ))
216
- return cr
217
- except Exception : # nosec: B110
218
- pass
219
-
220
- return None
221
-
222
- cr = ioctl_GWINSZ (0 ) or ioctl_GWINSZ (1 ) or ioctl_GWINSZ (2 )
223
-
224
- if not cr :
225
- try :
226
- fd = os .open (os .ctermid (), os .O_RDONLY ) # type: ignore
227
- cr = ioctl_GWINSZ (fd )
228
- os .close (fd )
229
- except Exception : # nosec: B110
230
- pass
231
-
232
- if not cr :
233
- try :
234
- cr = (os .environ ["LINES" ], os .environ ["COLUMNS" ])
235
- except Exception : # nosec: B110
236
- return None
237
-
238
- return int (cr [1 ]), int (cr [0 ])
154
+ return shutil .get_terminal_size ((80 , 25 ))
239
155
240
156
241
157
class Echo :
0 commit comments