Python Forum

Full Version: terminal/console geometry
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
on linux/unix/posix i can do this:

rows, columns = subprocess.check_output(['stty', 'size']).split()
to get terminal geometry
is there a more portable way for all other platforms?
From Python 3.3 --> there is os.get_terminal_size()
And the higher level call shutil.get_terminal_size().
Both work when i test with cmder(Window-10 Python 3.6).
The documentation says that os.get_terminal_size() only works on Linux and Windows, but is not clear about shutil.get_terminal_size(). Does anyone know of the shutil version works on a Mac?
import curses

screen = curses.initscr()
screen.getmaxyx()
It returns a (h, w) tuple

curses.endwin() # at the end of this to restore the normal terminal settings
(Jan-15-2017, 11:48 AM)wavic Wrote: [ -> ]
import curses

screen = curses.initscr()
screen.getmaxyx()
It returns a (h, w) tuple

curses.endwin() # at the end of this to restore the normal terminal settings

it works on 2.7.12 so more likely also on 2.4

but the way it messes up the screen may be a show stopper.
This is why curses.endwin() have to be in the code end of the snipped. From the documentation:

Quote:
curses.endwin()
De-initialize the library, and return terminal to normal status.
i can see forward to cases where that would still fail (block some serial ports, or the curses library itself being blocked).  has this been tested for terminal type settings that curses cannot function with?
Hm! I don't know what you are doing with it. How about that way:

>>> import blessings
>>> t = blessings.Terminal()
>>> print '{}x{}'.format(t.height, t.width)
24x80
>>> 
The good thing here... If you change the virtual terminal window size, t.height and t.width are changed accordingly.
(Jan-16-2017, 08:25 AM)wavic Wrote: [ -> ]Hm! I don't know what you are doing with it. How about that way:

>>> import blessings
>>> t = blessings.Terminal()
>>> print '{}x{}'.format(t.height, t.width)
24x80
>>> 
The good thing here... If you change the virtual terminal window size, t.height and t.width are changed accordingly.

that module cannot be found.  i am distributing a script that needs to know.  here is the solution i came up with:
def get_terminal_geometry(fd=0):
    import fcntl,struct,termios
    return struct.unpack('4H',fcntl.ioctl(fd,termios.TIOCGWINSZ,struct.pack('4H',0,0,0,0)))[1::-1]
Output:
lt1/forums /home/forums 12> py2 Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> def get_terminal_geometry(fd=0): ...     import fcntl,struct,termios ...     return struct.unpack('4H',fcntl.ioctl(fd,termios.TIOCGWINSZ,struct.pack('4H',0,0,0,0)))[1::-1] ... >>> t=get_terminal_geometry() >>> print '{}x{}'.format(t[1],t[0]) 46x166 >>> lt1/forums /home/forums 13> py3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> def get_terminal_geometry(fd=0): ...     import fcntl,struct,termios ...     return struct.unpack('4H',fcntl.ioctl(fd,termios.TIOCGWINSZ,struct.pack('4H',0,0,0,0)))[1::-1] ... >>> t=get_terminal_geometry() >>> print('{}x{}'.format(t[1], t[0])) 46x166 >>> lt1/forums /home/forums 14>
It's on the PyPi and installable with pip
Anyway, I have to see fcntl, struct, termios. Didn't use them yet
Pages: 1 2