瀏覽代碼

Use python Posix support to get tty size

stty is not portable outside *nix
Note: shutil.get_terminal_size require python 3.3

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 6 年之前
父節點
當前提交
452880af7c
共有 3 個文件被更改,包括 13 次插入6 次删除
  1. 10 5
      compose/cli/formatter.py
  2. 1 0
      requirements.txt
  3. 2 1
      setup.py

+ 10 - 5
compose/cli/formatter.py

@@ -2,20 +2,25 @@ from __future__ import absolute_import
 from __future__ import unicode_literals
 
 import logging
-import os
+import shutil
 
 import six
 import texttable
 
 from compose.cli import colors
 
+if hasattr(shutil, "get_terminal_size"):
+    from shutil import get_terminal_size
+else:
+    from backports.shutil_get_terminal_size import get_terminal_size
+
 
 def get_tty_width():
-    tty_size = os.popen('stty size 2> /dev/null', 'r').read().split()
-    if len(tty_size) != 2:
+    try:
+        width, _ = get_terminal_size()
+        return int(width)
+    except OSError:
         return 0
-    _, width = tty_size
-    return int(width)
 
 
 class Formatter:

+ 1 - 0
requirements.txt

@@ -1,3 +1,4 @@
+backports.shutil_get_terminal_size==1.0.0
 backports.ssl-match-hostname==3.5.0.1; python_version < '3'
 cached-property==1.3.0
 certifi==2017.4.17

+ 2 - 1
setup.py

@@ -55,7 +55,8 @@ extras_require = {
     ':python_version < "3.2"': ['subprocess32 >= 3.5.4, < 4'],
     ':python_version < "3.4"': ['enum34 >= 1.0.4, < 2'],
     ':python_version < "3.5"': ['backports.ssl_match_hostname >= 3.5, < 4'],
-    ':python_version < "3.3"': ['ipaddress >= 1.0.16, < 2'],
+    ':python_version < "3.3"': ['backports.shutil_get_terminal_size == 1.0.0',
+                                'ipaddress >= 1.0.16, < 2'],
     ':sys_platform == "win32"': ['colorama >= 0.4, < 1'],
     'socks': ['PySocks >= 1.5.6, != 1.5.7, < 2'],
 }