getting terminal display size?

J

jeff

I looked around a lot on the internet and couldn't find out how to do
this, how do I get the sizer (in rows and columns) of the view?
 
G

Grant Edwards

I looked around a lot on the internet and couldn't find out how to do
this, how do I get the sizer (in rows and columns) of the view?

You use the TIOCGWINSZ ioctl() call on the tty device in question.

import termios, fcntl, struct, sys

s = struct.pack("HHHH", 0, 0, 0, 0)
fd_stdout = sys.stdout.fileno()
x = fcntl.ioctl(fd_stdout, termios.TIOCGWINSZ, s)
print '(rows, cols, x pixels, y pixels) =',
print struct.unpack("HHHH", x)
 
G

Grant Edwards

You use the TIOCGWINSZ ioctl() call on the tty device in question.

import termios, fcntl, struct, sys

s = struct.pack("HHHH", 0, 0, 0, 0)
fd_stdout = sys.stdout.fileno()
x = fcntl.ioctl(fd_stdout, termios.TIOCGWINSZ, s)
print '(rows, cols, x pixels, y pixels) =',
print struct.unpack("HHHH", x)

You may also want to handle the WINCH signal so that you know
when the window size has been changed.

--
 
J

jeff

I don't really understand any of that; can you right me a function
that'll return the size as a tuple?
 
J

Joshua J. Kugler

jeff said:
I don't really understand any of that; can you right me a function
that'll return the size as a tuple?

Did you even *try* his code? I ran this:

import termios, fcntl, struct, sys

s = struct.pack("HHHH", 0, 0, 0, 0)
fd_stdout = sys.stdout.fileno()
x = fcntl.ioctl(fd_stdout, termios.TIOCGWINSZ, s)
print '(rows, cols, x pixels, y pixels) =',
print struct.unpack("HHHH", x)

And got this result:

(36, 96, 0, 0)

Looks like a tuple to me. return 'return' instead of 'print' the result.
We don't mind helping, but it's nice to know that the user has made some
attempt to understand what is going on before asking for free work to be
done.

j
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top