From: Daniel Berger Date: 2007-08-08T05:04:51+09:00 Subject: Re: Need Solaris Help On Aug 7, 1:39 pm, James Edward Gray II wrote: > On Aug 7, 2007, at 2:17 PM, Coey Minear wrote: > > > > > ronald braswell writes: > >> On 8/7/07, Gregory Brown wrote: > > >>> On 8/7/07, James Edward Gray II 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, I think you're better off using this snippet that Greg Hurrell showed you earlier this year instead of shelling out at all. I've modified it to use the correct value of TIOCGWINSZ for Solaris: TIOCGWINSZ = 21608 buf = [0, 0, 0, 0].pack('SSSS') if $stdin.ioctl(TIOCGWINSZ, buf) >= 0 rows, cols, xpixels, ypixels = buf.unpack("SSSS") p rows, cols, xpixels, ypixels else puts "Unable to get window size" end [24, 80, 730, 370] Regards, Dan PS - First time I think I've ever used IO#ioctl for anything. :)