How to get terminal dimensions without using curses or ncurses?

K

Kenneth McDonald

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
 
K

Kenneth McDonald

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
 
M

Michael W. Ryder

Kenneth said:
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.
 
M

Martin DeMello

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
 
J

James Gray

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
 
K

Kenneth McDonald

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

Brian Candler

Kenneth said:
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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top