I'd like to be able to print to terminal and do some reasonable formatting, without going to the trouble of learning curses. I simply need to know the dimensions of the terminal, but that info doesn't appear to be available in the ENV variables (which is what I'd expected.) Is there a way to get this info?
Thanks,
Ken
Followup: I found this code, which is supposed to do the trick:
#!/usr/bin/env ruby
TIOCGWINSZ = 0x5413
def terminal_size
rows, cols = 25, 80
buf = [0, 0, 0, 0].pack("SSSS")
if STDOUT.ioctl(TIOCGWINSZ, buf) >= 0 then
rows, cols, row_pixels, row_pixels, col_pixels = buf.unpack("SSSS")[0..1]
end
return [rows, cols]
end
print terminal_size
But when I run it, I get :
./term_size.rb:9:in `ioctl': Inappropriate ioctl for device (Errno::ENOTTY)
from ./term_size.rb:9:in `terminal_size'
from ./term_size.rb:15
This is on OS X 10.5, tried with both terminal and iterm. Any ideas?
Thanks,
Ken
···
On Sep 25, 2008, at 5:33 PM, Kenneth McDonald wrote:
I'd like to be able to print to terminal and do some reasonable formatting, without going to the trouble of learning curses. I simply need to know the dimensions of the terminal, but that info doesn't appear to be available in the ENV variables (which is what I'd expected.) Is there a way to get this info?
Thanks,
Ken
It's platform dependent, and curses has already done the hard work of
writing a uniform function call across various platforms. I'd
personally recommend going ahead and using just enough of curses to
get that function - you don't need to bother with the rest of it.
martin
···
On Thu, Sep 25, 2008 at 3:33 PM, Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net> wrote:
I'd like to be able to print to terminal and do some reasonable formatting,
without going to the trouble of learning curses. I simply need to know the
dimensions of the terminal, but that info doesn't appear to be available in
the ENV variables (which is what I'd expected.) Is there a way to get this
info?
Sure:
$ cat term_size.rb
#!/usr/bin/env ruby -wKU
require "rubygems"
require "highline/system_extensions"
p HighLine::SystemExtensions.terminal_size
$ ruby term_size.rb
[80, 24]
You can look in that file of the source if you just want to see how HighLine is doing that on various platforms.
James Edward Gray II
···
On Sep 25, 2008, at 5:33 PM, Kenneth McDonald wrote:
I'd like to be able to print to terminal and do some reasonable formatting, without going to the trouble of learning curses. I simply need to know the dimensions of the terminal, but that info doesn't appear to be available in the ENV variables (which is what I'd expected.) Is there a way to get this info?
Kenneth McDonald wrote:
I'd like to be able to print to terminal and do some reasonable
formatting, without going to the trouble of learning curses. I simply
need to know the dimensions of the terminal, but that info doesn't
appear to be available in the ENV variables (which is what I'd
expected.) Is there a way to get this info?
Thanks,
Ken
There is code in Ruport that you could try.
http://stonecode.svnrepository.com/svn/ruport/ruport/trunk/lib/ruport.rb
Search down to "terminal_size"
···
--
Posted via http://www.ruby-forum.com/\.
Kenneth McDonald wrote:
Followup: I found this code, which is supposed to do the trick:
#!/usr/bin/env ruby
TIOCGWINSZ = 0x5413
def terminal_size
rows, cols = 25, 80
buf = [0, 0, 0, 0].pack("SSSS")
if STDOUT.ioctl(TIOCGWINSZ, buf) >= 0 then
rows, cols, row_pixels, row_pixels, col_pixels = buf.unpack("SSSS")[0..1]
end
return [rows, cols]
end
print terminal_size
But when I run it, I get :
/term_size.rb:9:in `ioctl': Inappropriate ioctl for device (Errno::ENOTTY)
from ./term_size.rb:9:in `terminal_size'
from ./term_size.rb:15
This is on OS X 10.5, tried with both terminal and iterm. Any ideas?
Thanks,
Ken
I'd like to be able to print to terminal and do some reasonable formatting, without going to the trouble of learning curses. I simply need to know the dimensions of the terminal, but that info doesn't appear to be available in the ENV variables (which is what I'd expected.) Is there a way to get this info?
Thanks,
Ken
Do you have a command called infocmp available on OS X? Calling this without a name will return the terminal capabilities of the currently defined terminal. From this you can extract the lines and columns by looking for lines# and cols# respectively.
The above command gets the information from the terminfo database and there should be other implementations of it available if it is not on your system.
I got the above command from the O'Reilly 'termcap and terminfo' book.
···
On Sep 25, 2008, at 5:33 PM, Kenneth McDonald wrote:
This looks very nice. Do you know where on OS X highline and its documentation get installed, after a sudo gem install? Strangely, I can't find them easing using the OS X search features.
Thanks,
Ken
···
On Sep 25, 2008, at 7:27 PM, James Gray wrote:
On Sep 25, 2008, at 5:33 PM, Kenneth McDonald wrote:
I'd like to be able to print to terminal and do some reasonable formatting, without going to the trouble of learning curses. I simply need to know the dimensions of the terminal, but that info doesn't appear to be available in the ENV variables (which is what I'd expected.) Is there a way to get this info?
Sure:
$ cat term_size.rb
#!/usr/bin/env ruby -wKU
require "rubygems"
require "highline/system_extensions"
p HighLine::SystemExtensions.terminal_size
$ ruby term_size.rb
[80, 24]
You can look in that file of the source if you just want to see how HighLine is doing that on various platforms.
James Edward Gray II
Looks to me like Greg took that code right out of HighLine. 
James Edward Gray II
···
On Sep 26, 2008, at 4:05 AM, Brian Candler wrote:
Kenneth McDonald wrote:
I'd like to be able to print to terminal and do some reasonable
formatting, without going to the trouble of learning curses. I simply
need to know the dimensions of the terminal, but that info doesn't
appear to be available in the ENV variables (which is what I'd
expected.) Is there a way to get this info?
Thanks,
Ken
There is code in Ruport that you could try.
http://stonecode.svnrepository.com/svn/ruport/ruport/trunk/lib/ruport.rb
Search down to "terminal_size"
You can just read the source online:
http://highline.rubyforge.org/svn/trunk/highline/lib/highline/system_extensions.rb
Or just use this gem command to copy the source where you would like it:
gem unpack highline
Hope that helps.
James Edward Gray II
···
On Sep 25, 2008, at 8:11 PM, Kenneth McDonald wrote:
This looks very nice. Do you know where on OS X highline and its documentation get installed, after a sudo gem install? Strangely, I can't find them easing using the OS X search features.