[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Need Solaris Help

James Gray

8/7/2007 6:20:00 PM

I'm told the following bit of code doesn't work on Solaris:

# A Unix savvy method to fetch the console columns, and rows.
def terminal_size
`stty size`.split.map { |x| x.to_i }.reverse
end

HighLine makes use of this method as does Capistrano by extension, so
we need find a portable solution.

Can anyone provide an equivalent technique that works on Solaris?

Thanks.

James Edward Gray II

18 Answers

Gregory Brown

8/7/2007 6:25:00 PM

0

On 8/7/07, James Edward Gray II <james@grayproductions.net> wrote:
> I'm told the following bit of code doesn't work on Solaris:
>
> # A Unix savvy method to fetch the console columns, and rows.
> def terminal_size
> `stty size`.split.map { |x| x.to_i }.reverse
> end
>
> HighLine makes use of this method as does Capistrano by extension, so
> we need find a portable solution.

Ruport vendors this chunk of code, so sorry for the 'me too', but me too!

dougal.s

8/7/2007 6:45:00 PM

0

On 7 Aug 2007, at 19:20, James Edward Gray II wrote:

> I'm told the following bit of code doesn't work on Solaris:
> `stty size`.split.map { |x| x.to_i }.reverse
>
> James Edward Gray II
>

If it helps, the output from stty looks like the following:

-bash-3.00# stty size
unknown mode: size
-bash-3.00# stty
speed 38400 baud; -parity
rows = 24; columns = 80; ypixels = 0; xpixels = 0;
eol = -^?; eol2 = -^?; swtch = <undef>; flush = -^?; lnext = -^?;
brkint -inpck -istrip icrnl imaxbel onlcr tab3
echo echoe echok echoctl echoke iexten

Had a quick look at the man pages, there doesn't seem to be an alias
for size.

The man pages are also on the Sun site here: http://docs.su...
docs/doc/816-5165/6mbb0m9tb?a=view

Hope this is of some help.

Douglas F Shearer
http://douglasfs...

Alex Young

8/7/2007 6:54:00 PM

0

Douglas F Shearer wrote:
> On 7 Aug 2007, at 19:20, James Edward Gray II wrote:
>
>> I'm told the following bit of code doesn't work on Solaris:
>> `stty size`.split.map { |x| x.to_i }.reverse
>>
>> James Edward Gray II
>>
>
> If it helps, the output from stty looks like the following:
>
> -bash-3.00# stty size
> unknown mode: size
> -bash-3.00# stty
> speed 38400 baud; -parity
> rows = 24; columns = 80; ypixels = 0; xpixels = 0;
> eol = -^?; eol2 = -^?; swtch = <undef>; flush = -^?; lnext = -^?;
> brkint -inpck -istrip icrnl imaxbel onlcr tab3
> echo echoe echok echoctl echoke iexten
>
On FreeBSD:

[alex@panama ~/noodling/rubinius]$ stty
speed 38400 baud;
lflags: echoe echoke echoctl pendin
oflags: -oxtabs
cflags: cs8 -parenb

No rows or columns. How about stty -a?

[alex@panama ~/noodling/rubinius]$ stty -a
speed 38400 baud; 62 rows; 157 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
-echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
-extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -ignbrk
brkint -inpck -ignpar -parmrk
oflags: opost onlcr -ocrnl -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
-dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
eol2 = <undef>; erase = ^?; erase2 = ^H; intr = ^C; kill = ^U;
lnext = ^V; min = 1; quit = ^\; reprint = ^R; start = ^Q;
status = ^T; stop = ^S; susp = ^Z; time = 0; werase = ^W;


--
Alex

Tim Pease

8/7/2007 6:56:00 PM

0

On 8/7/07, James Edward Gray II <james@grayproductions.net> wrote:
>
> Can anyone provide an equivalent technique that works on Solaris?
>

$ cat t.rb
def terminal_size
m = %r/rows\s(?:=\s)?(\d+);.*columns\s(?:=\s)?(\d+);/.match(`stty -a`)
[Integer(m[2]), Integer(m[1])]
end

puts terminal_size.inspect

$ uname -a
SunOS hedwig 5.9 Generic_112233-12 sun4u sparc SUNW,Sun-Blade-1500

$ ruby t.rb
[80, 36]


And on the Linux box ...

$ uname -a
Linux pong 2.6.21-1.3228.fc7 #1 SMP Tue Jun 12 15:37:31 EDT 2007 i686
i686 i386 GNU/Linux

$ ruby t.rb
[80, 24]


Blessings,
TwP

Tim Pease

8/7/2007 6:59:00 PM

0

On 8/7/07, Alex Young <alex@blackkettle.org> wrote:
>
> [alex@panama ~/noodling/rubinius]$ stty -a
> speed 38400 baud; 62 rows; 157 columns;

Leave it to BSD to do everything backwards! ;-)

TwP

Alex Young

8/7/2007 7:15:00 PM

0

Tim Pease wrote:
> On 8/7/07, James Edward Gray II <james@grayproductions.net> wrote:
>> Can anyone provide an equivalent technique that works on Solaris?
>>
>
> $ cat t.rb
> def terminal_size
> m = %r/rows\s(?:=\s)?(\d+);.*columns\s(?:=\s)?(\d+);/.match(`stty -a`)
> [Integer(m[2]), Integer(m[1])]
> end
>
> puts terminal_size.inspect
>
> $ uname -a
> SunOS hedwig 5.9 Generic_112233-12 sun4u sparc SUNW,Sun-Blade-1500
>
> $ ruby t.rb
> [80, 36]
>
>
> And on the Linux box ...
>
> $ uname -a
> Linux pong 2.6.21-1.3228.fc7 #1 SMP Tue Jun 12 15:37:31 EDT 2007 i686
> i686 i386 GNU/Linux
>
> $ ruby t.rb
> [80, 24]
>
How's this:

[alex@panama ~/noodling/ruby]$ ruby t.rb
[157, 62]
[alex@panama ~/noodling/ruby]$ cat t.rb
def terminal_size
%r{([^;]*rows[^;]*;)([^;]*columns[^;]*;)}.match(`stty
-a`)[1..-1].map{|i| i.gsub(/\D/, '').to_i}.reverse
end

puts terminal_size.inspect


'Scuse the line break, but it works on FreeBSD, Linux and Cygwin+OCI...

--
Alex

Coey Minear

8/7/2007 7:17:00 PM

0

ronald braswell writes:
> On 8/7/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:
> >
> > On 8/7/07, James Edward Gray II <james@grayproductions.net> wrote:
> > > I'm told the following bit of code doesn't work on Solaris:
> > >
> > > # A Unix savvy method to fetch the console columns, and rows.
> > > def terminal_size
> > > `stty size`.split.map { |x| x.to_i }.reverse
> > > end
> > >
> > > HighLine makes use of this method as does Capistrano by extension, so
> > > we need find a portable solution.
> >
> > Ruport vendors this chunk of code, so sorry for the 'me too', but me too!
> >
> >
> This is not pretty but it works on my Solaris 10 x64 box. But you may have
> been looking
> for something more elegant.
>
> if `stty` =~ /.*\brows = (\d+).*\bcolumns = (\d+)/
> rows = $1
> columns = $2
> end
>
> Ron

Here's my stab at a cross-platform implementation:

def terminal_size
if /solaris/ =~ RUBY_PLATFORM
output = `stty`
[output.match('columns = (\d+)')[1].to_i,
output.match('rows = (\d+)')[1].to_i]
else
`stty size`.split.map { |x| x.to_i }.reverse
end
end

I did a quick test on Solaris (9 and 10), FreeBSD and Linux, and got
the same results in every case. (No guarantees on AIX, HP-UX, etc.)

Coey


James Gray

8/7/2007 7:30:00 PM

0

On Aug 7, 2007, at 1:54 PM, Alex Young wrote:

> Douglas F Shearer wrote:
>> On 7 Aug 2007, at 19:20, James Edward Gray II wrote:
>>> I'm told the following bit of code doesn't work on Solaris:
>>> `stty size`.split.map { |x| x.to_i }.reverse
>>>
>>> James Edward Gray II
>>>
>> If it helps, the output from stty looks like the following:
>> -bash-3.00# stty size
>> unknown mode: size
>> -bash-3.00# stty
>> speed 38400 baud; -parity
>> rows = 24; columns = 80; ypixels = 0; xpixels = 0;
>> eol = -^?; eol2 = -^?; swtch = <undef>; flush = -^?; lnext = -^?;
>> brkint -inpck -istrip icrnl imaxbel onlcr tab3
>> echo echoe echok echoctl echoke iexten
> On FreeBSD:

I assume the current code (using `stty size`) works on FreeBSD. Does
it not?

James Edward Gray II

Alex Young

8/7/2007 7:39:00 PM

0

James Edward Gray II wrote:
> On Aug 7, 2007, at 1:54 PM, Alex Young wrote:
>
>> Douglas F Shearer wrote:
>>> On 7 Aug 2007, at 19:20, James Edward Gray II wrote:
>>>> I'm told the following bit of code doesn't work on Solaris:
>>>> `stty size`.split.map { |x| x.to_i }.reverse
>>>>
>>>> James Edward Gray II
>>>>
>>> If it helps, the output from stty looks like the following:
>>> -bash-3.00# stty size
>>> unknown mode: size
>>> -bash-3.00# stty
>>> speed 38400 baud; -parity
>>> rows = 24; columns = 80; ypixels = 0; xpixels = 0;
>>> eol = -^?; eol2 = -^?; swtch = <undef>; flush = -^?; lnext = -^?;
>>> brkint -inpck -istrip icrnl imaxbel onlcr tab3
>>> echo echoe echok echoctl echoke iexten
>> On FreeBSD:
>
> I assume the current code (using `stty size`) works on FreeBSD. Does it
> not?
>
It does. I was aiming at something that would work everywhere.

--
Alex

James Gray

8/7/2007 7:39:00 PM

0

On Aug 7, 2007, at 2:17 PM, Coey Minear wrote:

> ronald braswell writes:
>> On 8/7/07, Gregory Brown <gregory.t.brown@gmail.com> wrote:
>>>
>>> On 8/7/07, James Edward Gray II <james@grayproductions.net> wrote:
>>>> I'm told the following bit of code doesn't work on Solaris:
>>>>
>>>> # A Unix savvy method to fetch the console columns, and
>>>> rows.
>>>> def terminal_size
>>>> `stty size`.split.map { |x| x.to_i }.reverse
>>>> end
>>>>
>>>> HighLine makes use of this method as does Capistrano by
>>>> extension, so
>>>> we need find a portable solution.
>>>
>>> Ruport vendors this chunk of code, so sorry for the 'me too', but
>>> me too!
>>>
>>>
>> This is not pretty but it works on my Solaris 10 x64 box. But
>> you may have
>> been looking
>> for something more elegant.
>>
>> if `stty` =~ /.*\brows = (\d+).*\bcolumns = (\d+)/
>> rows = $1
>> columns = $2
>> end
>>
>> Ron
>
> Here's my stab at a cross-platform implementation:
>
> def terminal_size
> if /solaris/ =~ RUBY_PLATFORM
> output = `stty`
> [output.match('columns = (\d+)')[1].to_i,
> output.match('rows = (\d+)')[1].to_i]
> else
> `stty size`.split.map { |x| x.to_i }.reverse
> end
> end
>
> I did a quick test on Solaris (9 and 10), FreeBSD and Linux, and got
> the same results in every case. (No guarantees on AIX, HP-UX, etc.)

Thanks all. I appreciate the help.

James Edward Gray II