console.console_size will use shutil.get_terminal_size if possible

This commit is contained in:
Prodesire 2017-12-30 23:14:32 +08:00
parent b1693db382
commit ab160b4adf
2 changed files with 80 additions and 70 deletions

View File

@ -1,13 +1,15 @@
Console
-------
.. py:function:: pydu.console.console_size()
.. py:function:: pydu.console.console_size(fallback=(80, 25))
For Windows, return (width, height) of available window area, (80, 25)
For Windows, return (width, height) of available window area, fallback
if no console is allocated.
For POSIX system, return (width, height) of console terminal. (80, 25)
For POSIX system, return (width, height) of console terminal, fallback
on IOError, i.e. when no console is allocated.
For other system, return (80, 25).
For other system, return fallback.
Fallback defaults to (80, 25) which is the default size used by many
terminal emulators.
>>> from pydu.console import console_size
>>> console_size()

View File

@ -1,16 +1,24 @@
import sys
import shutil
from .platform import WINDOWS, POSIX
# http://bitbucket.org/techtonik/python-pager
def console_size():
if hasattr(shutil, 'get_terminal_size'):
# Actually (80, 25) is the default size used by many terminal emulators
def console_size(fallback=(80, 25)):
return shutil.get_terminal_size(fallback=fallback)
else:
# http://bitbucket.org/techtonik/python-pager
def console_size(fallback=(80, 25)):
"""
For Windows, return (width, height) of available window area, (80, 25)
For Windows, return (width, height) of available window area, fallback
if no console is allocated.
For POSIX system, return (width, height) of console terminal. (80, 25)
For POSIX system, return (width, height) of console terminal, fallback
on IOError, i.e. when no console is allocated.
For other system, return (80, 25).
For other system, return fallback.
Fallback defaults to (80, 25) which is the default size used by many
terminal emulators.
"""
if WINDOWS:
@ -27,26 +35,26 @@ def console_size():
# CONSOLE_SCREEN_BUFFER_INFO Structure
class COORD(Structure):
_fields_ = [("X", SHORT), ("Y", SHORT)]
_fields_ = [('X', SHORT), ('Y', SHORT)]
class SMALL_RECT(Structure):
_fields_ = [("Left", SHORT), ("Top", SHORT),
("Right", SHORT), ("Bottom", SHORT)]
_fields_ = [('Left', SHORT), ('Top', SHORT),
('Right', SHORT), ('Bottom', SHORT)]
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
_fields_ = [("dwSize", COORD),
("dwCursorPosition", COORD),
("wAttributes", WORD),
("srWindow", SMALL_RECT),
("dwMaximumWindowSize", DWORD)]
_fields_ = [('dwSize', COORD),
('dwCursorPosition', COORD),
('wAttributes', WORD),
('srWindow', SMALL_RECT),
('dwMaximumWindowSize', DWORD)]
sbi = CONSOLE_SCREEN_BUFFER_INFO()
ret = windll.kernel32.GetConsoleScreenBufferInfo(
console_handle, byref(sbi))
if ret == 0:
return 80, 25
return ((sbi.srWindow.Right - sbi.srWindow.Left + 1) or 80,
(sbi.srWindow.Bottom - sbi.srWindow.Top + 1) or 25)
return fallback
return ((sbi.srWindow.Right - sbi.srWindow.Left + 1) or fallback[0],
(sbi.srWindow.Bottom - sbi.srWindow.Top + 1) or fallback[1])
elif POSIX:
# http://www.kernel.org/doc/man-pages/online/pages/man4/tty_ioctl.4.html
@ -69,6 +77,6 @@ def console_size():
# for example IOError: [Errno 25] Inappropriate ioctl for device
# when output is redirected
pass
return winsize[1] or 80, winsize[0] or 25
return winsize[1] or fallback[0], winsize[0] or fallback[1]
return 80, 25
return fallback