Sha256: ad110ef007faac477e066a58d9c9018564cc1f1859ae084e9789991a7de91937

Contents?: true

Size: 1.12 KB

Versions: 12

Compression:

Stored size: 1.12 KB

Contents

module ANSI

  module Terminal

    module_function

    #CHARACTER_MODE = "stty"    # For Debugging purposes only.

    #
    # Unix savvy getc().  (Second choice.)
    #
    # *WARNING*:  This method requires the external "stty" program!
    #
    def get_character( input = STDIN )
      raw_no_echo_mode

      begin
        input.getc
      ensure
        restore_mode
      end
    end

    #
    # Switched the input mode to raw and disables echo.
    #
    # *WARNING*:  This method requires the external "stty" program!
    #
    def raw_no_echo_mode
      @state = `stty -g`
      system "stty raw -echo cbreak isig"
    end

    #
    # Restores a previously saved input mode.
    #
    # *WARNING*:  This method requires the external "stty" program!
    #
    def restore_mode
      system "stty #{@state}"
    end

    # A Unix savvy method to fetch the console columns, and rows.
    def terminal_size
      if /solaris/ =~ RUBY_PLATFORM and
          `stty` =~ /\brows = (\d+).*\bcolumns = (\d+)/
        [$2, $1].map { |c| x.to_i }
      else
        `stty size`.split.map { |x| x.to_i }.reverse
      end
    end

  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ansi-1.4.1 lib/ansi/terminal/stty.rb
ansi-1.4.0 lib/ansi/terminal/stty.rb
ansi-1.3.0 lib/ansi/terminal/stty.rb
ansi-1.2.5 lib/ansi/terminal/stty.rb
ansi-1.2.4 lib/ansi/terminal/stty.rb
ansi-1.2.3 lib/ansi/terminal/stty.rb
ansi-1.2.2 lib/ansi/terminal/stty.rb
ansi-1.2.1 lib/ansi/terminal/stty.rb
ansi-1.2.0 lib/ansi/terminal/stty.rb
ansi-1.1.0 lib/ansi/terminal/stty.rb
ansi-1.0.1 lib/ansi/terminal/stty.rb
ansi-1.0.0 lib/ansi/terminal/stty.rb