[compat] Allow overriding by only COLUMNS or LINES in compat_get_terminal_size

Now the semantic of this function is identical to
shutil.get_terminal_size() in Python 3.3+. The new behavior also
corresponds to the old get_term_width(), which is removed in
003c69a84b
This commit is contained in:
Yen Chi Hsuan 2015-09-13 20:04:27 +08:00 committed by Jaime Marquínez Ferrándiz
parent 5495937f46
commit 13118a50b8

View File

@ -416,7 +416,7 @@ def _compat_add_option(self, *args, **kwargs):
else: else:
_terminal_size = collections.namedtuple('terminal_size', ['columns', 'lines']) _terminal_size = collections.namedtuple('terminal_size', ['columns', 'lines'])
def compat_get_terminal_size(): def compat_get_terminal_size(fallback=(80, 24)):
columns = compat_getenv('COLUMNS', None) columns = compat_getenv('COLUMNS', None)
if columns: if columns:
columns = int(columns) columns = int(columns)
@ -428,14 +428,20 @@ def compat_get_terminal_size():
else: else:
lines = None lines = None
try: if columns <= 0 or lines <= 0:
sp = subprocess.Popen( try:
['stty', 'size'], sp = subprocess.Popen(
stdout=subprocess.PIPE, stderr=subprocess.PIPE) ['stty', 'size'],
out, err = sp.communicate() stdout=subprocess.PIPE, stderr=subprocess.PIPE)
lines, columns = map(int, out.split()) out, err = sp.communicate()
except Exception: _columns, _lines = map(int, out.split())
pass except Exception:
_columns, _lines = _terminal_size(*fallback)
if columns <= 0:
columns = _columns
if lines <= 0:
lines = _lines
return _terminal_size(columns, lines) return _terminal_size(columns, lines)
try: try: