Python Forum

Full Version: what version ... os.get_terminal_geometry() ...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
in what version of Python did os.get_terminal_geometry() show up? my docs only say it is specific to Python3.
there's a os.get_terminal_size, but not get_terminal_geometry
There is a get_terminal_geometry in pyutils
sorry, wrong name out of habit. i am looking to upgrade my get_terminal_geometry() to use os.get_terminal_size() when it can. i am thinking about which test to use: if 'get_terminal_size' in dir(os): or to test the version.
on Linux you can read results of 'stty size'
Larz60p@linux-nnem: ~:$stty size
31 142
programmatically from os like:
x, y = os.popen('stty size', 'r').read().split()
on windows
from win32api import GetSystemMetrics

print "Width =", GetSystemMetrics(0)
print "Height =", GetSystemMetrics(1)
Looks like os.get_terminal_size() was added in python 3.3.
It also looks like you should be using shutil.get_terminal_size() instead.
i prefer, where possible, to avoid running another process just to get a little bit of information. so i like the idea of doing system or library (module in python) calls. i have invoked stty often in my bash scripts. but if i don't have to in python, i like that. in on script i needed the host's IP address. but i needed it soon, so i invoked the ifconfig command and got it from there. not all scripts do things the same way. but if i do on thing quite a lot, i make it into a function or module or command or whatever it needs.

(Jul-15-2018, 09:41 AM)stranac Wrote: [ -> ]Looks like os.get_terminal_size() was added in python 3.3.
It also looks like you should be using shutil.get_terminal_size() instead.

my get_terminal_geometry() function would have had a gap of 3.0 to 3.2 since it had code to get the geometry itself in version 2. now i will test for version 3.3 or if os is already imported, see if 'get_terminal_size' in dir(os).

so, why should i be using shutil.get_terminal_size() instead?
The one from shutil is recommended by the docs.
It provides some additional checks and fallbacks, so I guess it's more reliable.
ok, it will be easy to switch to shutil. i decided that since i would need to do an import anyway (of sys to check the minor version) that i could just do from os import get_terminal_size in try/except where i def get_terminal_geometry(): one of two different ways (eliminating testing this in the function call). replacing os with shutil will be trivial.

the code:
try:
    from shutil import get_terminal_size
    def get_terminal_geometry(fd=1):
        """Get current terminal geometry (width,heght)
    
function        get_terminal_geometry
purpose         get terminal geometry (width,heght)
arguments       (1,fd=) open file or file descriptor of tty to get geometry from
return          (width,height)
"""
        return get_terminal_size(fd)
except ImportError:
    def get_terminal_geometry(fd=1):
        """Get current terminal geometry (width,heght)
    
function        get_terminal_geometry
purpose         get terminal geometry (width,heght)
arguments       (1,fd=) open file or file descriptor of tty to get geometry from
return          (width,height)
"""
        import fcntl,os,struct,termios
        if 'fileno' in dir(fd):
            fd=fd.fileno()
        try:
            height,width=struct.unpack('4H',fcntl.ioctl(fd,
                termios.TIOCGWINSZ,
                struct.pack('4H',0,0,0,0)))[:2]
        except:
            height,width=None,None
        if height is None or width is None:
            try:
                fd = os.open('/dev/tty',os.O_WRONLY)
                height,width=struct.unpack('4H',fcntl.ioctl(fd,
                    termios.TIOCGWINSZ,
                    struct.pack('4H',0,0,0,0)))[:2]
                os.close(fd)
            except:
                height,width=None,None
            if height is None or width is None:
                height,width=None,None
        return width,height