Forum: Ruby Console width?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Alex Barrett (Guest)
on 2006-04-08 20:54
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.
Chris A. (Guest)
on 2006-04-08 21:40
(Received via mailing list)
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.
Daniel H. (Guest)
on 2006-04-08 22:05
(Received via mailing list)
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
Guillaume M. (Guest)
on 2006-04-08 23:34
(Received via mailing list)
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 :
David V. (Guest)
on 2006-04-09 06:23
(Received via mailing list)
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.
This topic is locked and can not be replied to.