Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to get terminal dimensions without using curses or ncurses?

Reply
Thread Tools

How to get terminal dimensions without using curses or ncurses?

 
 
Kenneth McDonald
Guest
Posts: n/a
 
      09-25-2008
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

 
Reply With Quote
 
 
 
 
Kenneth McDonald
Guest
Posts: n/a
 
      09-25-2008
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
>



 
Reply With Quote
 
 
 
 
Michael W. Ryder
Guest
Posts: n/a
 
      09-25-2008
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.
 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      09-25-2008
On Thu, Sep 25, 2008 at 3:33 PM, Kenneth McDonald
<(E-Mail Removed)> 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

 
Reply With Quote
 
James Gray
Guest
Posts: n/a
 
      09-26-2008
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

 
Reply With Quote
 
Kenneth McDonald
Guest
Posts: n/a
 
      09-26-2008
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
>



 
Reply With Quote
 
James Gray
Guest
Posts: n/a
 
      09-26-2008
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/tr..._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

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      09-26-2008
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/s.../lib/ruport.rb

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

 
Reply With Quote
 
James Gray
Guest
Posts: n/a
 
      09-26-2008
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/s.../lib/ruport.rb
>
> Search down to "terminal_size"


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

James Edward Gray II

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: Replacing curses (Was: Re: Problem with curses and UTF-8) Jean-Paul Calderone Python 2 02-09-2006 08:29 AM
Curses and Terminal DEC Leif B. Kristensen Python 0 02-17-2005 05:02 PM
Curses, Terminal Settings J Turner Python 0 08-28-2003 05:13 PM



Advertisments