Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
terminal/console geometry
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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).
Reply
#3
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?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
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.
Reply
#8
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
(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>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
It's on the PyPi and installable with pip
Anyway, I have to see fcntl, struct, termios. Didn't use them yet
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Import Text, output curve geometry Alyner 0 1,991 Feb-03-2020, 03:05 AM
Last Post: Alyner
  How do I apply a Gaussian blur to a particular edge of geometry in Matplotlib? hbolandi 0 2,013 Feb-02-2020, 06:08 PM
Last Post: hbolandi
  [Geometry] Finding centers of each grid in pointgrid andre 2 3,072 Aug-22-2017, 12:09 PM
Last Post: andre

Forum Jump:

User Panel Messages

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