# Console width?

**URL:** <https://www.ruby-forum.com/t/console-width/93754>\
**Category:** Ruby\
**Created:** [April 8, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/console-width/93754 "2006-04-08T18:54:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Barrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90db22/32.png) [@Alex\_Barrett](https://www.ruby-forum.com/u/Alex_Barrett)\
**Post date:** [April 8, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/console-width/93754/1 "2006-04-08T18:54:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alex\_Barrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90db22/32.png) [@Alex\_Barrett](https://www.ruby-forum.com/u/Alex_Barrett)\
**Post date:** [April 8, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/console-width/93754/2 "2006-04-08T19:40:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alex\_Barrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90db22/32.png) [@Alex\_Barrett](https://www.ruby-forum.com/u/Alex_Barrett)\
**Post date:** [April 8, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/console-width/93754/3 "2006-04-08T20:05:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alex\_Barrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90db22/32.png) [@Alex\_Barrett](https://www.ruby-forum.com/u/Alex_Barrett)\
**Post date:** [April 8, 2006, 9:34pm UTC](https://www.ruby-forum.com/t/console-width/93754/4 "2006-04-08T21:34:09Z")

</div>

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 :

---

<div class="post-metadata">

**Author:** ![Alex\_Barrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/90db22/32.png) [@Alex\_Barrett](https://www.ruby-forum.com/u/Alex_Barrett)\
**Post date:** [April 9, 2006, 4:23am UTC](https://www.ruby-forum.com/t/console-width/93754/5 "2006-04-09T04:23:49Z")

</div>

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.
