Python Forum
what version ... os.get_terminal_geometry() ...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what version ... os.get_terminal_geometry() ...
#8
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
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
RE: what version ... os.get_terminal_geometry() ... - by Skaperen - Jul-19-2018, 12:29 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020