Get terminal size in perl and python
Kalle Svensson
kalle at gnupung.net
Thu Mar 8 17:16:34 EST 2001
Sez Grant Edwards:
> In article <mailman.984079092.10641.python-list at python.org>, Kalle Svensson wrote:
> >Sez Egbert Bouwman:
> >> My perl correspondent uses:
> >> use Term::ReadKey;
> >> ($cols,$lines)=GetTerminalSize()
> >> What is the python equivalent ?
> >
> >I'd use os.environ["COLUMNS"] and os.environ["LINES"], but I've no idea
> >about how portable they are.
>
> In most Unixes you'd be better off using the ioctl() call (the
> details of which I can't remember at the moment -- I thought it
> was in the termios struct, but I can't find it).
Ah, yes. Some research gave me this (i686 Linux 2.2.17, Python 2.1a2):
###### START ######
import struct, fcntl, IOCTL, sys
s = struct.pack("HHHH", 0, 0, 0, 0)
while 1:
if raw_input("") == "q":
break
lines, cols = struct.unpack("HHHH", fcntl.ioctl(sys.stdout.fileno(),
IOCTL.TIOCGWINSZ, s))[:2]
print lines, "lines,", cols, "cols."
####### END #######
If you don't have a IOCTL module, don't want to make you own, and have the
same platform as I, replace IOCTL.TIOCGWINSZ with 0x5413.
Lovely beasts, those ioctls.
Peace,
Kalle
--
Email: kalle at gnupung.net | You can tune a filesystem, but you
Web: http://www.gnupung.net/ | can't tune a fish. -- man tunefs(8)
PGP fingerprint: 0C56 B171 8159 327F 1824 F5DE 74D7 80D7 BF3B B1DD
[ Not signed due to lossage. Blame Microsoft Outlook Express. ]
More information about the Python-list
mailing list