Sha256: 97a67680d5932dfab5145a5f874f18f9fed3366e988416c100e05c30103eb752

Contents?: true

Size: 1.77 KB

Versions: 6

Compression:

Stored size: 1.77 KB

Contents

# attempt to call stty; if failure, raise error
`stty 2> /dev/null`
if $?.exitstatus != 0
  raise "stty command returned nonzero exit status"
end

warn "io/console on JRuby shells out to stty for most operations"

# Non-Windows assumes stty command is available
class IO
  if RbConfig::CONFIG['host_os'].downcase =~ /linux/ && File.exists?("/proc/#{Process.pid}/fd")
    def stty(*args)
      `stty #{args.join(' ')} < /proc/#{Process.pid}/fd/#{fileno}`
    end
  else
    def stty(*args)
      `stty #{args.join(' ')}`
    end
  end

  def raw(*)
    saved = stty('-g')
    stty('raw')
    yield self
  ensure
    stty(saved)
  end

  def raw!(*)
    stty('raw')
  end

  def cooked(*)
    saved = stty('-g')
    stty('-raw')
    yield self
  ensure
    stty(saved)
  end

  def cooked!(*)
    stty('-raw')
  end

  def echo=(echo)
    stty(echo ? 'echo' : '-echo')
  end

  def echo?
    (stty('-a') =~ / -echo /) ? false : true
  end

  def noecho
    saved = stty('-g')
    stty('-echo')
    yield self
  ensure
    stty(saved)
  end

  # Not all systems return same format of stty -a output
  IEEE_STD_1003_2 = '(?<rows>\d+) rows; (?<columns>\d+) columns'
  UBUNTU = 'rows (?<rows>\d+); columns (?<columns>\d+)'

  def winsize
    match = stty('-a').match(/#{IEEE_STD_1003_2}|#{UBUNTU}/)
    [match[:rows].to_i, match[:columns].to_i]
  end

  def winsize=(size)
    size = size.to_ary unless size.kind_of?(Array)
    sizelen = size.size

    if sizelen != 2 && sizelen != 4
      raise ArgumentError.new("wrong number of arguments (given #{sizelen}, expected 2 or 4)")
    end

    row, col, xpixel, ypixel = size

    if sizelen == 4
      warn "stty io/console does not support pixel winsize"
    end

    stty("rows #{row} cols #{col}")
  end

  def iflush
  end

  def oflush
  end

  def ioflush
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
io-console-0.7.1-java lib/ffi/io/console/stty_console.rb
io-console-0.7.0-java lib/ffi/io/console/stty_console.rb
io-console-0.6.0-java lib/io/console/ffi/stty_console.rb
io-console-0.5.11-java lib/io/console/ffi/stty_console.rb
io-console-0.5.9-java lib/io/console/ffi/stty_console.rb
io-console-0.5.8-java lib/io/console/ffi/stty_console.rb