Best way of finding terminal width/height?

Joel Hedlund joel.hedlund at gmail.com
Sun Feb 5 14:42:07 EST 2006


 > Which details?  We'd be happy to explain the code. Not that
 > you need to understand the details to use the code.

OK, why '1234' in here, and what's termios.TIOCGWINSZ, and how should I 
have known this was the way too do it?
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')

Am I interpreting C structs here, and if so - why is python giving me C 
structs? And what's 'hh' anyway?
struct.unpack('hh', ... )

Why 0, 1 and 2?
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)

 > I don't know if it will work on MS Windows or not.

Linux and unix are my main concerns, but it would be neat to know if it 
would work on Win/Mac.

What OS:es set the COLS/ROWS env vars? What OS:es can leverage the 
termios module? I have a hunch that we have 100% overlap there, and then 
this solution leaves me at square one (arbitrary choice of 80*25) for 
the others OS:es (or am I wrong?). How do Win/Mac people do this?

 > What's unpythonic about the example you found?

Maybe I did bit of poor wording there, but In my experience python 
generally has a high level of abstraction, which provides linguistically 
appealing (as in "in english") solutions to almost any problem. Like for 
example how os.path.isfile(s) tells me if my string s corresponds to a 
file. I guess that's what I mean really. I sort of expected to find 
something like my terminal_size() example in the built-in modules. I 
didn't expect to have to do that struct fcntl ioctl boogey to solve this 
relatively simple (?) problem.

Thanks for your help!
/Joel



More information about the Python-list mailing list