Sha256: 307a221143dbf610ae9578da6513a0806de7ed20b4bc6329e2baa6cfb8e13116

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

require 'remedy/console_resize'

module Remedy
  module Console
    require 'io/console'

    TIOCGWINSZ = 0x40087468

    module_function

    def input
      @input ||= $stdin
    end

    def output
      @output ||= $stdout
    end

    def raw
      raw!
      result = yield
    ensure
      cooked!
      return result
    end

    def raw!
      input.echo = false
      input.raw!
    end

    def cooked!
      input.echo = true
      input.cooked!
    end

    def columns
      size.last
    end
    alias_method :width, :columns

    def rows
      size.first
    end
    alias_method :height, :rows

    def size
      str = [0, 0, 0, 0].pack('SSSS')
      if input.ioctl(TIOCGWINSZ, str) >= 0 then
        str.unpack('SSSS').first 2
      else
        raise UnknownConsoleSize, "Unable to get console size"
      end
    end

    def interactive?
      input.isatty
    end

    def set_console_resized_hook!
      Console::Resize.set_console_resized_hook! do
        yield
      end
    end

    class UnknownConsoleSize < IOError; end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
remedy-0.1.2 lib/remedy/console.rb
remedy-0.1.1 lib/remedy/console.rb
remedy-0.1.0 lib/remedy/console.rb