class Terminal::Size

Constants

IOCTL_INPUT_BUF
VERSION

Public Class Methods

resize(direction, magnitude) click to toggle source

These are experimental

# File lib/terminal-size.rb, line 10
def resize direction, magnitude
  tmux 'resize-pane', "-#{direction}", magnitude
end
size() click to toggle source
# File lib/terminal-size.rb, line 4
def size
  size_via_low_level_ioctl or size_via_stty or nil
end
size!() click to toggle source
# File lib/terminal-size.rb, line 7
def size!; size or _height_width_hash_from 25, 80 end
size_via_low_level_ioctl() click to toggle source
# File lib/terminal-size.rb, line 19
def size_via_low_level_ioctl
  # Thanks to runpaint for the general approach to this
  return unless $stdin.respond_to? :ioctl
  code = tiocgwinsz_value_for RUBY_PLATFORM
  return unless code
  buf = IOCTL_INPUT_BUF.dup
  return unless $stdout.ioctl(code, buf).zero?
  return if IOCTL_INPUT_BUF == buf
  got = buf.unpack('S4')[0..1]
  _height_width_hash_from *got
rescue
  nil
end
size_via_stty() click to toggle source
# File lib/terminal-size.rb, line 42
def size_via_stty
  ints = `stty size`.scan(/\d+/).map &:to_i
  _height_width_hash_from *ints
rescue
  nil
end
tiocgwinsz_value_for(platform) click to toggle source
# File lib/terminal-size.rb, line 33
def tiocgwinsz_value_for platform
  # This is as reported by <sys/ioctl.h>
  # Hard-coding because it seems like overkll to acutally involve C for this.
  {
    /linux/ => 0x5413,
    /darwin/ => 0x40087468, # thanks to brandon@brandon.io for the lookup!
  }.find{|k,v| platform[k]}
end
tmux(*cmd) click to toggle source
# File lib/terminal-size.rb, line 14
def tmux *cmd
  system 'tmux', *(cmd.map &:to_s)
end

Private Class Methods

_height_width_hash_from(*dimensions) click to toggle source
# File lib/terminal-size.rb, line 50
def _height_width_hash_from *dimensions
  { :height => dimensions[0], :width => dimensions[1] }
end