How to get terminal dimensions without using curses or ncurses?

Discussion in 'Ruby' started by Kenneth McDonald, Sep 25, 2008.

  1. 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
     
    Kenneth McDonald, Sep 25, 2008
    #1
    1. Advertising

  2. 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
    >
     
    Kenneth McDonald, Sep 25, 2008
    #2
    1. Advertising

  3. 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.
     
    Michael W. Ryder, Sep 26, 2008
    #3
  4. On Thu, Sep 25, 2008 at 3: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?


    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
     
    Martin DeMello, Sep 26, 2008
    #4
  5. Kenneth McDonald

    James Gray Guest

    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, Sep 26, 2008
    #5
  6. 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
    >
     
    Kenneth McDonald, Sep 26, 2008
    #6
  7. Kenneth McDonald

    James Gray Guest

    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

    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
     
    James Gray, Sep 26, 2008
    #7
  8. 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/.
     
    Brian Candler, Sep 26, 2008
    #8
  9. Kenneth McDonald

    James Gray Guest

    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"


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

    James Edward Gray II
     
    James Gray, Sep 26, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Burkert
    Replies:
    1
    Views:
    438
    Jarek Zgoda
    Jan 18, 2004
  2. dachteruit
    Replies:
    6
    Views:
    381
    dachteruit
    Nov 12, 2010
  3. William Morgan
    Replies:
    0
    Views:
    526
    William Morgan
    Feb 12, 2005
  4. Kenneth McDonald

    Question on curses and ncurses

    Kenneth McDonald, Aug 22, 2008, in forum: Ruby
    Replies:
    1
    Views:
    113
    Harish Chandra
    Sep 12, 2008
  5. Kenneth McDonald
    Replies:
    1
    Views:
    157
    Nit Khair
    Sep 27, 2008
Loading...

Share This Page