views:

258

answers:

3

Have you ever noticed that if you run rake -T in rails the list of rake descriptions are truncated by the width of the terminal window. So there should be a way to get it in Ruby and Use it.

I'm printing some Ascii-art on the screen and I don't want it to be broken. therefore I need to find out the width of terminal at run time some how.

Any Idea how to do that?

+1  A: 

ENV['COLUMNS'] will give you the number of columns in the terminal.

Ethan Gunderson
I tried it in IRB and inspected the ENV hash but couldn't find the columns key.
Allen Bargi
This trick must be specific to some shells or operating systems. It didn't work under Bash on Debian, either.
Wayne Conrad
Interesting, works on OSX at least.
Ethan Gunderson
Only works on OSX using bash, not tcsh.
mark4o
@mark4o Good to know, thanks!
Ethan Gunderson
+3  A: 

Their is a common unix command:

tput cols

Which return the width of the terminal.

Brian Gianforcaro
Thanks, this one is working.
Allen Bargi
+1  A: 

If you want your code to work across platforms, here's what I use: http://github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb#L61-71

Also check out the system_extensions file in highline

Thanks for giving me exactly what I wanted! BTW, Gabriel, I'm using your hirb gem in my rails console and I'm just loving it. thanks man.
Allen Bargi