本文整理汇总了Python中shutil.get_terminal_size方法的典型用法代码示例。如果您正苦于以下问题:Python shutil.get_terminal_size方法的具体用法?Python shutil.get_terminal_size怎么用?Python shutil.get_terminal_size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shutil
的用法示例。
在下文中一共展示了shutil.get_terminal_size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_terminal_size
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def get_terminal_size():
"""
Detect terminal size and return tuple = (width, height).
Only to be used when running in a terminal. Note that the IPython notebook,
IPython zmq frontends, or IDLE do not run in a terminal,
"""
import platform
if PY3:
return shutil.get_terminal_size()
current_os = platform.system()
tuple_xy = None
if current_os == 'Windows':
tuple_xy = _get_terminal_size_windows()
if tuple_xy is None:
tuple_xy = _get_terminal_size_tput()
# needed for window's python in cygwin's xterm!
if (current_os == 'Linux' or current_os == 'Darwin' or
current_os.startswith('CYGWIN')):
tuple_xy = _get_terminal_size_linux()
if tuple_xy is None:
tuple_xy = (80, 25) # default value
return tuple_xy
示例2: do_pp
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def do_pp(self, arg):
"""[width]pp expression
Pretty-print the value of the expression.
"""
width = getattr(arg, "cmd_count", None)
try:
val = self._getval(arg)
except:
return
if width is None:
try:
width, _ = self.get_terminal_size()
except Exception as exc:
self.message("warning: could not get terminal size ({})".format(exc))
width = None
try:
pprint.pprint(val, self.stdout, width=width)
except:
exc_info = sys.exc_info()[:2]
self.error(traceback.format_exception_only(*exc_info)[-1].strip())
示例3: get_terminal_size
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def get_terminal_size():
fallback = (80, 24)
try:
from shutil import get_terminal_size
except ImportError:
try:
import termios
import fcntl
import struct
call = fcntl.ioctl(0, termios.TIOCGWINSZ, "\x00"*8)
height, width = struct.unpack("hhhh", call)[:2]
except (SystemExit, KeyboardInterrupt):
raise
except:
width = int(os.environ.get('COLUMNS', fallback[0]))
height = int(os.environ.get('COLUMNS', fallback[1]))
# Work around above returning width, height = 0, 0 in Emacs
width = width if width != 0 else fallback[0]
height = height if height != 0 else fallback[1]
return width, height
else:
return get_terminal_size(fallback)
示例4: refresh
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def refresh(self, cur_len):
terminal_width = get_terminal_size().columns # 获取终端宽度
info = "%s '%s'... %.2f%%" % (
self.prefix_info,
self.title,
cur_len / self.total * 100,
)
while len(info) > terminal_width - 20:
self.title = self.title[0:-4] + "..."
info = "%s '%s'... %.2f%%" % (
self.prefix_info,
self.title,
cur_len / self.total * 100,
)
end_str = "\r" if cur_len < self.total else "\n"
print(info, end=end_str)
示例5: test_stty_match
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def test_stty_match(self):
"""Check if stty returns the same results ignoring env
This test will fail if stdin and stdout are connected to
different terminals with different sizes. Nevertheless, such
situations should be pretty rare.
"""
try:
size = subprocess.check_output(['stty', 'size']).decode().split()
except (FileNotFoundError, subprocess.CalledProcessError):
self.skipTest("stty invocation failed")
expected = (int(size[1]), int(size[0])) # reversed order
with support.EnvironmentVarGuard() as env:
del env['LINES']
del env['COLUMNS']
actual = shutil.get_terminal_size()
self.assertEqual(expected, actual)
示例6: get_data
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def get_data(self, wrap=True):
if wrap:
terminal_size = shutil.get_terminal_size((80, 40))
wrap_columns = terminal_size.columns
if wrap_columns > int(config.CONFIG.parser['info_display_max_columns']):
wrap_columns = int(config.CONFIG.parser['info_display_max_columns'])
return '\n'.join([textwrap.fill(x, wrap_columns) for x in ''.join(self.fed).split('\n')])
return ''.join(self.fed)
示例7: get_width
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def get_width() -> int:
"""
Get the width of the terminal window
"""
width, _ = shutil.get_terminal_size(TERMINAL_SIZE_FALLBACK)
return width
示例8: __init__
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def __init__(self, prog):
terminal_width = shutil.get_terminal_size().columns
os.environ['COLUMNS'] = str(terminal_width)
max_help_position = min(max(24, terminal_width // 3), 40)
super().__init__(prog, max_help_position=max_help_position)
示例9: _getdimensions
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def _getdimensions():
if py33:
import shutil
size = shutil.get_terminal_size()
return size.lines, size.columns
else:
import termios, fcntl, struct
call = fcntl.ioctl(1, termios.TIOCGWINSZ, "\000" * 8)
height, width = struct.unpack("hhhh", call)[:2]
return height, width
示例10: get_terminal_width
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def get_terminal_width() -> int:
return shutil.get_terminal_size().columns
示例11: _format_exc_for_sticky
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def _format_exc_for_sticky(self, exc):
if len(exc) != 2:
return "pdbpp: got unexpected __exception__: %r" % (exc,)
exc_type, exc_value = exc
s = ''
try:
try:
s = exc_type.__name__
except AttributeError:
s = str(exc_type)
if exc_value is not None:
s += ': '
s += str(exc_value)
except KeyboardInterrupt:
raise
except Exception as exc:
try:
s += '(unprintable exception: %r)' % (exc,)
except:
s += '(unprintable exception)'
else:
# Use first line only, limited to terminal width.
s = s.replace("\r", r"\r").replace("\n", r"\n")
width, _ = self.get_terminal_size()
if len(s) > width:
s = s[:width - 1] + "…"
if self.config.highlight:
s = Color.set(self.config.line_number_color, s)
return s
示例12: handle
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def handle(self, project: Project, options: argparse.Namespace) -> None:
result = project.get_repository().search(options.query)
terminal_width = None
if sys.stdout.isatty():
terminal_width = get_terminal_size()[0]
print_results(result, project.environment.get_working_set(), terminal_width)
示例13: get_terminal_size
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
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())
示例14: size
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def size(fallback=(80, 24)) -> tuple:
"""Get the size of the terminal window."""
return tuple(shutil.get_terminal_size(fallback=fallback))
示例15: get_terminal_size
# 需要导入模块: import shutil [as 别名]
# 或者: from shutil import get_terminal_size [as 别名]
def get_terminal_size():
"""
Detect terminal size and return tuple = (width, height).
Only to be used when running in a terminal. Note that the IPython notebook,
IPython zmq frontends, or IDLE do not run in a terminal,
"""
import platform
if PY3:
return shutil.get_terminal_size()
current_os = platform.system()
tuple_xy = None
if current_os == 'Windows':
tuple_xy = _get_terminal_size_windows()
if tuple_xy is None:
tuple_xy = _get_terminal_size_tput()
# needed for window's python in cygwin's xterm!
if current_os == 'Linux' or \
current_os == 'Darwin' or \
current_os.startswith('CYGWIN'):
tuple_xy = _get_terminal_size_linux()
if tuple_xy is None:
tuple_xy = (80, 25) # default value
return tuple_xy