# How to get terminal dimensions without using curses or ncurses?

**URL:** <https://www.ruby-forum.com/t/how-to-get-terminal-dimensions-without-using-curses-or-ncurses/149540>\
**Category:** Ruby\
**Created:** [September 26, 2008, 12:34am UTC](https://www.ruby-forum.com/t/how-to-get-terminal-dimensions-without-using-curses-or-ncurses/149540 "2008-09-26T00:34:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 26, 2008, 12:34am UTC](https://www.ruby-forum.com/t/how-to-get-terminal-dimensions-without-using-curses-or-ncurses/149540/1 "2008-09-26T00:34:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 26, 2008, 12:47am UTC](https://www.ruby-forum.com/t/how-to-get-terminal-dimensions-without-using-curses-or-ncurses/149540/2 "2008-09-26T00:47:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 26, 2008, 1:32am UTC](https://www.ruby-forum.com/t/how-to-get-terminal-dimensions-without-using-curses-or-ncurses/149540/3 "2008-09-26T01:32:42Z")

</div>

Kenneth McDonald wrote:

> ```
> rows, cols, row_pixels, row_pixels, col_pixels =  
> 
> ```
> 
> /term\_size.rb:9:in `ioctl’: Inappropriate ioctl for device  
> On Sep 25, 2008, at 5:33 PM, Kenneth McDonald wrote:

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.

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 26, 2008, 2:28am UTC](https://www.ruby-forum.com/t/how-to-get-terminal-dimensions-without-using-curses-or-ncurses/149540/4 "2008-09-26T02:28:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 26, 2008, 1:49am UTC](https://www.ruby-forum.com/t/how-to-get-terminal-dimensions-without-using-curses-or-ncurses/149540/5 "2008-09-26T01:49:39Z")

</div>

On Thu, Sep 25, 2008 at 3:33 PM, Kenneth McDonald  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 26, 2008, 3:12am UTC](https://www.ruby-forum.com/t/how-to-get-terminal-dimensions-without-using-curses-or-ncurses/149540/6 "2008-09-26T03:12:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 26, 2008, 3:39am UTC](https://www.ruby-forum.com/t/how-to-get-terminal-dimensions-without-using-curses-or-ncurses/149540/7 "2008-09-26T03:39:03Z")

</div>

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\_extensions.rb](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 G. II

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 26, 2008, 11:05am UTC](https://www.ruby-forum.com/t/how-to-get-terminal-dimensions-without-using-curses-or-ncurses/149540/8 "2008-09-26T11:05:30Z")

</div>

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](http://stonecode.svnrepository.com/svn/ruport/ruport/trunk/lib/ruport.rb)

Search down to “terminal\_size”

---

<div class="post-metadata">

**Author:** ![Kenneth\_McDonald](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@Kenneth\_McDonald](https://www.ruby-forum.com/u/Kenneth_McDonald)\
**Post date:** [September 26, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/how-to-get-terminal-dimensions-without-using-curses-or-ncurses/149540/9 "2008-09-26T17:51:23Z")

</div>

On Sep 26, 2008, at 4:05 AM, Brian C. wrote:

> There is code in Ruport that you could try.
> 
> [http://stonecode.svnrepository.com/svn/ruport/ruport/trunk/lib/ruport.rb](http://stonecode.svnrepository.com/svn/ruport/ruport/trunk/lib/ruport.rb)
> 
> Search down to “terminal\_size”

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

James Edward G. II
