Author |
Message |
DtY
|
Posted: Sat Jun 06, 2009 11:20 pm Post subject: Getting terminal width |
|
|
In irb, I'm able to get the width of the terminal using ENV['COLUMNS'], however, ENV doesn't seem to have that key when running a script. Is there any other way to get the size without installing some extra libraries? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rdrake
|
Posted: Sun Jun 07, 2009 12:33 am Post subject: RE:Getting terminal width |
|
|
Don't think so. Check out Ruby Ncurses, methinks it can do what you want.
You can also use the back ticks in order to execute something on the command line, then grab that in your script.
Ruby: | `echo ENV['COLUMNS']` |
|
|
|
|
|
|
DtY
|
Posted: Sun Jun 07, 2009 1:02 am Post subject: RE:Getting terminal width |
|
|
You sure that that line's right?
irb(main):002:0> `echo ENV['COLUMNS']`
=> "ENV[COLUMNS]\n"
And ncurses is more than I want to set up, my program is just showing a progress bar, so I'd rather use a fixed with than use ncurses. |
|
|
|
|
|
rdrake
|
Posted: Sun Jun 07, 2009 10:40 am Post subject: RE:Getting terminal width |
|
|
Ruby: | irb(main):004:0> cols = `echo $COLUMNS`.chomp.to_i
=> 80
irb(main):005:0> cols
=> 80 |
|
|
|
|
|
|
DtY
|
Posted: Sun Jun 07, 2009 12:50 pm Post subject: RE:Getting terminal width |
|
|
Same thing, works in irb, but not ruby:
irb(main):001:0> `echo $COLUMNS`
=> "80\n"
[jeff@localhost ~]$ ruby
p `echo $COLUMNS`[CTRL+D]
"\n" |
|
|
|
|
|
rdrake
|
Posted: Sun Jun 07, 2009 1:17 pm Post subject: RE:Getting terminal width |
|
|
This gem comes from the HighLiner project:
code: | -jailshell-3.2$ cat test.rb
def terminal_size
`stty size`.split.map { |x| x.to_i }.reverse
end
puts terminal_size()[0]
puts terminal_size()[1]
-jailshell-3.2$ ruby test.rb
80
24 |
Of course instead of just stealing the method you can also have users install the gem which is just called "highliner." It also includes a Win32 version of the terminal_size() method. |
|
|
|
|
|
DtY
|
Posted: Sun Jun 07, 2009 1:25 pm Post subject: RE:Getting terminal width |
|
|
That one works, thanks you |
|
|
|
|
|
|