G
Greg Hurrell
I'd like to be able to determine the width of the terminal in which my
command-line tool is running.
I made a quick program to find out the value of TIOCGWINSZ on my
system (Mac OS X 10.4.8, running on Intel):
#include <sys/ioctl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
printf("%d\n", TIOCGWINSZ);
return 0;
}
The value is 1074295912 (0x40087468). Then I used the following Ruby
code based on something I found in the archives:
TIOCGWINSZ = 0x40087468
str = [0, 0, 0, 0].pack('SSSS')
if STDIN.ioctl(TIOCGWINSZ, str) >= 0
rows, cols, xpixels, ypixels = str.unpack("SSSS")
p rows, cols, xpixels, ypixels
else
puts "Unable to get window size"
end
This returns the correct values:
[55, 132, 792, 770]
That is, 55 rows, 132 columns. I'd like to know if there's a more
portable way of doing this... About the only semi-portable way I can
think of is wrapping this up in a C extension; at least that way
(most) people can compile it locally. Suggestions?
Cheers,
Greg
command-line tool is running.
I made a quick program to find out the value of TIOCGWINSZ on my
system (Mac OS X 10.4.8, running on Intel):
#include <sys/ioctl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
printf("%d\n", TIOCGWINSZ);
return 0;
}
The value is 1074295912 (0x40087468). Then I used the following Ruby
code based on something I found in the archives:
TIOCGWINSZ = 0x40087468
str = [0, 0, 0, 0].pack('SSSS')
if STDIN.ioctl(TIOCGWINSZ, str) >= 0
rows, cols, xpixels, ypixels = str.unpack("SSSS")
p rows, cols, xpixels, ypixels
else
puts "Unable to get window size"
end
This returns the correct values:
[55, 132, 792, 770]
That is, 55 rows, 132 columns. I'd like to know if there's a more
portable way of doing this... About the only semi-portable way I can
think of is wrapping this up in a C extension; at least that way
(most) people can compile it locally. Suggestions?
Cheers,
Greg