Method: #terminal_width

Defined in:
lib/cli/tools.rb

#terminal_width(default_width = 81) ⇒ Object

Try to determine the terminal with. If it is not possible to to so, it returns the default_width. default_width Defaults to 81



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cli/tools.rb', line 4

def terminal_width(default_width = 81)
  tiocgwinsz = 0x5413
  data = [0, 0, 0, 0].pack("SSSS")
  if @out.ioctl(tiocgwinsz, data) >= 0
    rows, cols, xpixels, ypixels = data.unpack("SSSS")
    raise unless cols > 0
    cols
  else
    raise
  end
rescue
  begin
    IO.popen('stty -a 2>&1') do |pipe|
      column_line = pipe.detect { |line| /(\d+) columns/ =~ line }
      raise unless column_line
      $1.to_i
    end
  rescue
    default_width
  end
end