Python Forum
terminal/console geometry - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: terminal/console geometry (/thread-1597.html)

Pages: 1 2


terminal/console geometry - Skaperen - Jan-15-2017

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?


RE: terminal/console geometry - snippsat - Jan-15-2017

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).


RE: terminal/console geometry - ichabod801 - Jan-15-2017

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?


RE: terminal/console geometry - wavic - Jan-15-2017

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



RE: terminal/console geometry - Skaperen - Jan-16-2017

(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.


RE: terminal/console geometry - wavic - Jan-16-2017

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.



RE: terminal/console geometry - Skaperen - Jan-16-2017

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?


RE: terminal/console geometry - wavic - Jan-16-2017

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.


RE: terminal/console geometry - Skaperen - Jan-29-2017

(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>



RE: terminal/console geometry - wavic - Jan-29-2017

It's on the PyPi and installable with pip
Anyway, I have to see fcntl, struct, termios. Didn't use them yet