Console width?

Is there a variable I can access somewhere to tell me the width of the
command line?
It defaults to 80, which I’ve put in a global; but it would be better if
it did it by itself. Even better if it updated itself as well.

require ‘curses’

screen = Curses.init_screen
width = screen.maxx
height = screen.maxy


Not quite what you want though because Curses.init_screen will setup
the terminal for curses (blank the screen, etc.)

On the other hand, if you’re interested in the width there’s a good
chance you want to use curses anyways.

On Apr 8, 2006, at 6:54 PM, Alex Barrett wrote:

Is there a variable I can access somewhere to tell me the width of the
command line?
It defaults to 80, which I’ve put in a global; but it would be
better if
it did it by itself. Even better if it updated itself as well.

There is the environment variable COLUMNS. It probably won’t exist on
windows shells:

ENV[“COLUMNS”] -> “115”

  • Daniel

I don’t know what your application is. In all cases when I needed to
modify the last line of output, I would send a line feed (\r) and
rewrite completely. It seems to always be simpler than doing tricky
calculation on how many character to go back.

Something like:

$stdout.sync = true
10.times { |i| print("\ri=%2d!!!" % i); sleep(1); }
puts

For anything more complicated, curses should be it, although I have no
experience in this area.

Guillaume.

Le 8 avr. 06, à 12:54, Alex Barrett a écrit :

DÅ?a Sobota 8. Apríl 2006 21:33 Guillaume M. napísal:

I don’t know what your application is. In all cases when I needed to
modify the last line of output, I would send a line feed (\r) and
rewrite completely. It seems to always be simpler than doing tricky
calculation on how many character to go back.

There’s way more scenarios than doing screen rewrites when you need to
know
console width. Text layout comes to mind - wrapping words at the end of
a
line or writing a multi-column table, As a matter of fact, ignoring
console
width sounds like a pretty bad thing to do when outputting any text at
all
that might span over the usual lowest-common denominator of 80 columns,
and
it’s not completely good practice to rely on this value either, even if
it’s
unlikely to ever cause problems for most programs.

David V.