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
HighLine can do this:
require "highline/system_extensions.rb"
cols, rows = HighLine::SystemExtensions.terminal_size
Hope that helps.
James Edward Gray II
···
On Mar 5, 2007, at 11:25 AM, Greg Hurrell wrote:
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?
Thanks, James. That seems to cover just about all platforms, although
it introduces another dependency or two (HighLine itself, plus
HighLine's own dependencies). I guess that's the way it is though...
Cheers,
Greg
···
On 5 mar, 18:34, James Edward Gray II <j...@grayproductions.net> wrote:
HighLine can do this:
require "highline/system_extensions.rb"
cols, rows = HighLine::SystemExtensions.terminal_size
I use highline in a bunch of internal projects for CLI gui's, it totally rocks(thx JEGII&Gregory!). But I want these to be standalone gems with no dependencies so I embed highline in my own gems. Its only a few files and easy to embed so it makes sense to ship it with the gem that needs it.
Cheers-
-- Ezra Zygmuntowicz-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
···
On Mar 5, 2007, at 10:35 AM, Greg Hurrell wrote:
On 5 mar, 18:34, James Edward Gray II <j...@grayproductions.net> > wrote:
HighLine can do this:
require "highline/system_extensions.rb"
cols, rows = HighLine::SystemExtensions.terminal_size
Thanks, James. That seems to cover just about all platforms, although
it introduces another dependency or two (HighLine itself, plus
HighLine's own dependencies). I guess that's the way it is though...
Cheers,
Greg