Posts: 4,647
Threads: 1,494
Joined: Sep 2016
on linux/unix/posix i can do this:
1 |
rows, columns = subprocess.check_output([ 'stty' , 'size' ]).split()
|
to get terminal geometry
is there a more portable way for all other platforms?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 7,317
Threads: 123
Joined: Sep 2016
Jan-15-2017, 03:59 AM
(This post was last modified: Jan-15-2017, 03:59 AM by snippsat.)
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).
Posts: 4,220
Threads: 97
Joined: Sep 2016
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?
Posts: 2,953
Threads: 48
Joined: Sep 2016
Jan-15-2017, 11:48 AM
(This post was last modified: Jan-15-2017, 11:48 AM by wavic.)
1 2 3 4 |
import curses
screen = curses.initscr()
screen.getmaxyx()
|
It returns a (h, w) tuple
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
(Jan-15-2017, 11:48 AM)wavic Wrote:
1 2 3 4 |
import curses
screen = curses.initscr()
screen.getmaxyx()
|
It returns a (h, w) tuple
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.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 2,953
Threads: 48
Joined: Sep 2016
This is why curses.endwin() have to be in the code end of the snipped. From the documentation:
Quote:De-initialize the library, and return terminal to normal status.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
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?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 2,953
Threads: 48
Joined: Sep 2016
Jan-16-2017, 08:25 AM
(This post was last modified: Jan-16-2017, 08:25 AM by wavic.)
Hm! I don't know what you are doing with it. How about that way:
1 2 3 4 5 |
>>> 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.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
(Jan-16-2017, 08:25 AM)wavic Wrote: Hm! I don't know what you are doing with it. How about that way:
1 2 3 4 5 |
>>> 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:
1 2 3 |
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>
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 2,953
Threads: 48
Joined: Sep 2016
It's on the PyPi and installable with pip
Anyway, I have to see fcntl, struct, termios. Didn't use them yet
|