本文整理汇总了Python中termios.TIOCGWINSZ属性的典型用法代码示例。如果您正苦于以下问题:Python termios.TIOCGWINSZ属性的具体用法?Python termios.TIOCGWINSZ怎么用?Python termios.TIOCGWINSZ使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类termios
的用法示例。
在下文中一共展示了termios.TIOCGWINSZ属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_termsize
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def get_termsize():
"""Return terminal size as a tuple (height, width)."""
try:
# this works on unix machines
import struct, fcntl, termios
height, width = struct.unpack("hhhh",
fcntl.ioctl(0,termios.TIOCGWINSZ,
"\000"*8))[0:2]
if not (height and width):
height, width = 24, 79
except ImportError:
# for windows machins, use default values
# Does anyone know how to get the console size under windows?
# One approach is:
# http://code.activestate.com/recipes/440694/
height, width = 24, 79
return height, width
示例2: resize_handler
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def resize_handler(self):
"""Send the new window size to conmon."""
def wrapped(signum, frame): # pylint: disable=unused-argument
packed = fcntl.ioctl(self.pseudo_tty.stdout, termios.TIOCGWINSZ,
struct.pack('HHHH', 0, 0, 0, 0))
rows, cols, _, _ = struct.unpack('HHHH', packed)
logging.debug('Resize window(%dx%d) using %s', rows, cols,
self.pseudo_tty.control_socket)
# TODO: Need some kind of timeout in case pipe is blocked
with open(self.pseudo_tty.control_socket, 'w') as skt:
# send conmon window resize message
skt.write('1 {} {}\n'.format(rows, cols))
return wrapped
示例3: _get_terminal_size_linux
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def _get_terminal_size_linux():
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
cr = struct.unpack('hh',
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
return cr
except:
pass
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:
try:
cr = (os.environ['LINES'], os.environ['COLUMNS'])
except:
return None
return int(cr[1]), int(cr[0])
示例4: get_terminal_size
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [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)
示例5: _getTerminalSize_linux
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def _getTerminalSize_linux():
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,'1234'))
except:
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:
try:
cr = (env['LINES'], env['COLUMNS'])
except:
return None
return int(cr[1]), int(cr[0])
示例6: _get_terminal_size_linux
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def _get_terminal_size_linux():
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,'1234'))
except:
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:
try:
cr = (env['LINES'], env['COLUMNS'])
except:
return None
return int(cr[1]), int(cr[0])
示例7: size
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def size(fileno):
"""Current terminal height and width (lines and columns).
:params int fileno: file-descriptor
:returns: Tuple of two integers - lines and columns respectively.
``(None, None)`` if ``fileno`` is not a terminal
"""
if not isatty(fileno):
return None, None
try:
size = struct.unpack(
'2h', fcntl.ioctl(fileno, termios.TIOCGWINSZ, ' '))
except Exception:
size = (os.getenv('LINES', 25), os.getenv('COLUMNS', 80))
return size
示例8: _getTerminalSize_linux
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def _getTerminalSize_linux():
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
except:
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:
try:
cr = (os.environ['LINES'], os.environ['COLUMNS'])
except:
return None
return int(cr[1]), int(cr[0])
示例9: _winsize
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def _winsize(fd):
"""
Return named tuple describing size of the terminal by ``fd``.
If the given platform does not have modules :mod:`termios`,
:mod:`fcntl`, or :mod:`tty`, window size of 80 columns by 25
rows is always returned.
:arg int fd: file descriptor queries for its window size.
:raises IOError: the file descriptor ``fd`` is not a terminal.
:rtype: WINSZ
WINSZ is a :class:`collections.namedtuple` instance, whose structure
directly maps to the return value of the :const:`termios.TIOCGWINSZ`
ioctl return value. The return parameters are:
- ``ws_row``: width of terminal by its number of character cells.
- ``ws_col``: height of terminal by its number of character cells.
- ``ws_xpixel``: width of terminal by pixels (not accurate).
- ``ws_ypixel``: height of terminal by pixels (not accurate).
"""
if HAS_TTY:
data = fcntl.ioctl(fd, termios.TIOCGWINSZ, WINSZ._BUF)
return WINSZ(*struct.unpack(WINSZ._FMT, data))
return WINSZ(ws_row=25, ws_col=80, ws_xpixel=0, ws_ypixel=0)
示例10: size
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def size(fd):
"""
Return a tuple (rows,cols) representing the size of the TTY `fd`.
The provided file descriptor should be the stdout stream of the TTY.
If the TTY size cannot be determined, returns None.
"""
if not os.isatty(fd.fileno()):
return None
try:
dims = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, 'hhhh'))
except:
try:
dims = (os.environ['LINES'], os.environ['COLUMNS'])
except:
return None
return dims
示例11: _get_terminal_width_ioctl
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def _get_terminal_width_ioctl(stdout):
from fcntl import ioctl
import termios
try:
# winsize structure has 4 unsigned short fields
winsize = b'\0' * struct.calcsize('hhhh')
try:
winsize = ioctl(stdout, termios.TIOCGWINSZ, winsize)
except IOError:
return None
except TypeError:
# this is raised in unit tests as stdout is sometimes a StringIO
return None
winsize = struct.unpack('hhhh', winsize)
columns = winsize[1]
if not columns:
return None
return columns
except IOError:
return None
示例12: getTerminalSize
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def getTerminalSize():
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
except:
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 = (env.get('LINES', 25), env.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
示例13: _get_terminal_size_linux
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def _get_terminal_size_linux():
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
cr = struct.unpack('hh',
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
return cr
except:
pass
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:
try:
cr = (os.environ['LINES'], os.environ['COLUMNS'])
except:
return None
return int(cr[1]), int(cr[0])
示例14: handleResize
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def handleResize(self, signum, frame):
h,w=array('h', ioctl(sys.stderr,termios.TIOCGWINSZ,'\0'*8))[:2]
self.term_width = w
示例15: _get_terminal_size_linux
# 需要导入模块: import termios [as 别名]
# 或者: from termios import TIOCGWINSZ [as 别名]
def _get_terminal_size_linux(self):
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
cr = struct.unpack("hh", fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))
return cr
except:
pass
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:
try:
cr = (os.environ["LINES"], os.environ["COLUMNS"])
except:
return None
return int(cr[1]), int(cr[0])