1
1
"""
2
- get_terminal_size() -- return width and height of terminal as a tuple
3
-
4
- code from:
5
- http://stackoverflow.com/questions/566746/how-to-get-console- window-width-in-
6
- python
7
-
8
- written by
9
- Harco Kuppens (http://stackoverflow.com/users/825214/harco-kuppens)
10
-
11
- It is mentioned in the stackoverflow response that this code works
12
- on linux, os x, windows and cygwin (windows).
2
+ Terminal utilities.
13
3
"""
14
4
from __future__ import print_function
15
5
16
- import os
17
- import shutil
18
- import subprocess
19
-
20
- __all__ = ['get_terminal_size' , 'is_terminal' ]
21
-
22
-
23
- def get_terminal_size ():
24
- """
25
- Detect terminal size and return tuple = (width, height).
26
-
27
- Only to be used when running in a terminal. Note that the IPython notebook,
28
- IPython zmq frontends, or IDLE do not run in a terminal,
29
- """
30
-
31
- return shutil .get_terminal_size ()
6
+ __all__ = ['is_terminal' ]
32
7
33
8
34
9
def is_terminal ():
@@ -46,89 +21,3 @@ def is_terminal():
46
21
return False
47
22
else : # IPython in a terminal
48
23
return True
49
-
50
-
51
- def _get_terminal_size_windows ():
52
-
53
- try :
54
- from ctypes import windll , create_string_buffer
55
-
56
- # stdin handle is -10
57
- # stdout handle is -11
58
- # stderr handle is -12
59
-
60
- h = windll .kernel32 .GetStdHandle (- 12 )
61
- csbi = create_string_buffer (22 )
62
- res = windll .kernel32 .GetConsoleScreenBufferInfo (h , csbi )
63
- except (AttributeError , ValueError ):
64
- return None
65
- if res :
66
- import struct
67
- (bufx , bufy , curx , cury , wattr , left , top , right , bottom , maxx ,
68
- maxy ) = struct .unpack ("hhhhHhhhhhh" , csbi .raw )
69
- sizex = right - left + 1
70
- sizey = bottom - top + 1
71
- return sizex , sizey
72
- else :
73
- return None
74
-
75
-
76
- def _get_terminal_size_tput ():
77
- # get terminal width
78
- # src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width
79
- # -height-of-a-terminal-window
80
-
81
- try :
82
- proc = subprocess .Popen (["tput" , "cols" ],
83
- stdin = subprocess .PIPE ,
84
- stdout = subprocess .PIPE )
85
- output_cols = proc .communicate (input = None )
86
- proc = subprocess .Popen (["tput" , "lines" ],
87
- stdin = subprocess .PIPE ,
88
- stdout = subprocess .PIPE )
89
- output_rows = proc .communicate (input = None )
90
- except OSError :
91
- return None
92
-
93
- try :
94
- # Some terminals (e.g. spyder) may report a terminal size of '',
95
- # making the `int` fail.
96
-
97
- cols = int (output_cols [0 ])
98
- rows = int (output_rows [0 ])
99
- return cols , rows
100
- except (ValueError , IndexError ):
101
- return None
102
-
103
-
104
- def _get_terminal_size_linux ():
105
- def ioctl_GWINSZ (fd ):
106
- try :
107
- import fcntl
108
- import termios
109
- import struct
110
- cr = struct .unpack (
111
- 'hh' , fcntl .ioctl (fd , termios .TIOCGWINSZ , '1234' ))
112
- except (struct .error , IOError ):
113
- return None
114
- return cr
115
- cr = ioctl_GWINSZ (0 ) or ioctl_GWINSZ (1 ) or ioctl_GWINSZ (2 )
116
- if not cr :
117
- try :
118
- fd = os .open (os .ctermid (), os .O_RDONLY )
119
- cr = ioctl_GWINSZ (fd )
120
- os .close (fd )
121
- except OSError :
122
- pass
123
- if not cr or cr == (0 , 0 ):
124
- try :
125
- from os import environ as env
126
- cr = (env ['LINES' ], env ['COLUMNS' ])
127
- except (ValueError , KeyError ):
128
- return None
129
- return int (cr [1 ]), int (cr [0 ])
130
-
131
-
132
- if __name__ == "__main__" :
133
- sizex , sizey = get_terminal_size ()
134
- print ('width = {w} height = {h}' .format (w = sizex , h = sizey ))
0 commit comments