summaryrefslogtreecommitdiff
path: root/src/pip/_internal/compat.py
diff options
context:
space:
mode:
authorPradyun Gedam <pradyunsg@gmail.com>2018-03-30 11:15:50 +0530
committerPradyun Gedam <pradyunsg@gmail.com>2018-03-30 11:15:50 +0530
commitc2a4ea47318f04b79cf23465a58338a32e69c58e (patch)
tree0c162edd5eafde0f0b158b95d53162f83adc6f66 /src/pip/_internal/compat.py
parent255a5181570285475e37b026b59f4ce9c60b2b29 (diff)
parent83cb225ded14be3e0fe81e94ac6bdded6033c92a (diff)
downloadpip-c2a4ea47318f04b79cf23465a58338a32e69c58e.tar.gz
Merge branch 'master' into refactor/reduce-action-at-distance
Diffstat (limited to 'src/pip/_internal/compat.py')
-rw-r--r--src/pip/_internal/compat.py43
1 files changed, 42 insertions, 1 deletions
diff --git a/src/pip/_internal/compat.py b/src/pip/_internal/compat.py
index 70d7730f6..c71f5e66f 100644
--- a/src/pip/_internal/compat.py
+++ b/src/pip/_internal/compat.py
@@ -6,6 +6,7 @@ import codecs
import locale
import logging
import os
+import shutil
import sys
from pip._vendor.six import text_type
@@ -23,7 +24,7 @@ except ImportError:
__all__ = [
"ipaddress", "uses_pycache", "console_to_str", "native_str",
- "get_path_uid", "stdlib_pkgs", "WINDOWS", "samefile",
+ "get_path_uid", "stdlib_pkgs", "WINDOWS", "samefile", "get_terminal_size",
]
@@ -192,3 +193,43 @@ def samefile(file1, file2):
path1 = os.path.normcase(os.path.abspath(file1))
path2 = os.path.normcase(os.path.abspath(file2))
return path1 == path2
+
+
+if hasattr(shutil, 'get_terminal_size'):
+ def get_terminal_size():
+ """
+ Returns a tuple (x, y) representing the width(x) and the height(y)
+ in characters of the terminal window.
+ """
+ return tuple(shutil.get_terminal_size())
+else:
+ def get_terminal_size():
+ """
+ Returns a tuple (x, y) representing the width(x) and the height(y)
+ in characters of the terminal window.
+ """
+ def ioctl_GWINSZ(fd):
+ try:
+ import fcntl
+ import termios
+ import struct
+ cr = struct.unpack(
+ 'hh',
+ fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')
+ )
+ except:
+ return None
+ if cr == (0, 0):
+ return None
+ return cr
+ cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
+ if not cr:
+ try:
+ fd = os.open(os.ctermid(), os.O_RDONLY)
+ cr = ioctl_GWINSZ(fd)
+ os.close(fd)
+ except:
+ pass
+ if not cr:
+ cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
+ return int(cr[1]), int(cr[0])