11# adapted from github.com/onsim/shutils
22import os
3- import sys
3+ import shutil
44
55
66def terminal_size (fallback = (80 , 24 )):
@@ -11,8 +11,7 @@ def terminal_size(fallback=(80, 24)):
1111 the value is a positive integer, it is used.
1212
1313 When COLUMNS or LINES is not defined, which is the common case,
14- the terminal connected to sys.__stdout__ is queried
15- by invoking os.get_terminal_size.
14+ shutil.get_terminal_size is used to determine the current terminal size.
1615
1716 If the terminal size cannot be successfully queried, either because
1817 the system doesn't support querying, or because we are not
@@ -36,10 +35,11 @@ def terminal_size(fallback=(80, 24)):
3635 # only query if necessary
3736 if columns <= 0 or lines <= 0 :
3837 try :
39- size = os .get_terminal_size (sys .__stdout__ .fileno ())
40- except (AttributeError , ValueError , OSError ):
41- # stdout is None, closed, detached, or not a terminal, or
42- # os.get_terminal_size() is unsupported
38+ size = shutil .get_terminal_size (fallback )
39+ except (OSError , ValueError ):
40+ # shutil.get_terminal_size failed because the runtime does not have an
41+ # attached terminal. Fall back to the provided default which mirrors
42+ # the behaviour of shutil.get_terminal_size when a fallback is supplied.
4343 size = os .terminal_size (fallback )
4444 if columns <= 0 :
4545 columns = size .columns or fallback [0 ]
0 commit comments