33
44from . import converters
55
6+ Dimensions = typing .Tuple [int , int ]
7+ OptionalDimensions = typing .Optional [Dimensions ]
68
7- def get_terminal_size () -> typing .Tuple [int , int ]: # pragma: no cover
9+
10+ def get_terminal_size () -> Dimensions : # pragma: no cover
811 '''Get the current size of your terminal
912
1013 Multiple returns are not always a good idea, but in this case it greatly
@@ -62,35 +65,36 @@ def get_terminal_size() -> typing.Tuple[int, int]: # pragma: no cover
6265 pass
6366
6467 try :
65- w , h = _get_terminal_size_linux ()
66- if w and h :
67- return w , h
68+ # The method can return None so we don't unpack it
69+ wh = _get_terminal_size_linux ()
70+ if wh is not None and all (wh ):
71+ return wh
6872 except Exception : # pragma: no cover
6973 pass
7074
7175 try :
7276 # Windows detection doesn't always work, let's try anyhow
73- w , h = _get_terminal_size_windows ()
74- if w and h :
75- return w , h
77+ wh = _get_terminal_size_windows ()
78+ if wh is not None and all ( wh ) :
79+ return wh
7680 except Exception : # pragma: no cover
7781 pass
7882
7983 try :
8084 # needed for window's python in cygwin's xterm!
81- w , h = _get_terminal_size_tput ()
82- if w and h :
83- return w , h
85+ wh = _get_terminal_size_tput ()
86+ if wh is not None and all ( wh ) :
87+ return wh
8488 except Exception : # pragma: no cover
8589 pass
8690
8791 return 79 , 24
8892
8993
90- def _get_terminal_size_windows (): # pragma: no cover
94+ def _get_terminal_size_windows () -> OptionalDimensions : # pragma: no cover
9195 res = None
9296 try :
93- from ctypes import windll , create_string_buffer
97+ from ctypes import windll , create_string_buffer # type: ignore
9498
9599 # stdin handle is -10
96100 # stdout handle is -11
@@ -115,7 +119,7 @@ def _get_terminal_size_windows(): # pragma: no cover
115119 return None
116120
117121
118- def _get_terminal_size_tput (): # pragma: no cover
122+ def _get_terminal_size_tput () -> OptionalDimensions : # pragma: no cover
119123 # get terminal width src: http://stackoverflow.com/questions/263890/
120124 try :
121125 import subprocess
@@ -141,19 +145,19 @@ def _get_terminal_size_tput(): # pragma: no cover
141145 return None
142146
143147
144- def _get_terminal_size_linux (): # pragma: no cover
148+ def _get_terminal_size_linux () -> OptionalDimensions : # pragma: no cover
145149 def ioctl_GWINSZ (fd ):
146150 try :
147151 import fcntl
148152 import termios
149153 import struct
150154
151- size = struct .unpack (
152- 'hh' , fcntl .ioctl (fd , termios .TIOCGWINSZ , '1234' )
155+ return struct .unpack (
156+ 'hh' ,
157+ fcntl .ioctl (fd , termios .TIOCGWINSZ , '1234' ), # type: ignore
153158 )
154159 except Exception :
155160 return None
156- return size
157161
158162 size = ioctl_GWINSZ (0 ) or ioctl_GWINSZ (1 ) or ioctl_GWINSZ (2 )
159163
0 commit comments