[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to get terminal dimensions without using curses or ncurses?

Kenneth McDonald

9/25/2008 10:34:00 PM

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

8 Answers

Kenneth McDonald

9/25/2008 10:46:00 PM

0

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
>


Michael W. Ryder

9/25/2008 11:28:00 PM

0

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
>
>
> 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
>>
>
>
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.

Martin DeMello

9/25/2008 11:49:00 PM

0

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?

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

James Gray

9/26/2008 12:28:00 AM

0

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

Kenneth McDonald

9/26/2008 1:11:00 AM

0

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
>


James Gray

9/26/2008 1:38:00 AM

0

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.

You can just read the source online:

http://highline.rubyforge.org/svn/trunk/highline/lib/highline/system_ext...

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

Brian Candler

9/26/2008 9:05:00 AM

0

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...

Search down to "terminal_size"
--
Posted via http://www.ruby-....

James Gray

9/26/2008 3:50:00 PM

0

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...
>
> Search down to "terminal_size"

Looks to me like Greg took that code right out of HighLine. :)

James Edward Gray II