Module HighLine::SystemExtensions
In: lib/highline/system_extensions.rb

Methods

Constants

JRUBY = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
CHARACTER_MODE = "Win32API"
CHARACTER_MODE = "termios"
CHARACTER_MODE = "ncurses"   work correctly in JRuby manually installing the ffi-ncurses gem is the only way to get highline to operate correctly in JRuby. The ncurses library is only present on unix platforms so this is not a solution for using highline in JRuby on windows.
CHARACTER_MODE = "stty"

Public Instance methods

Windows savvy getc().

WARNING: This method ignores input and reads one character from STDIN!

[Source]

# File lib/highline/system_extensions.rb, line 37
      def get_character( input = STDIN )
        Win32API.new("crtdll", "_getch", [ ], "L").Call
      end

ncurses savvy getc(). (JRuby choice.)

[Source]

# File lib/highline/system_extensions.rb, line 100
          def get_character( input = STDIN )
            FFI::NCurses.initscr
            FFI::NCurses.cbreak
            begin
              FFI::NCurses.curs_set 0
              input.getbyte
            ensure
              FFI::NCurses.endwin
            end
          end

Unix savvy getc(). (First choice.)

WARNING: This method requires the "termios" library!

[Source]

# File lib/highline/system_extensions.rb, line 72
        def get_character( input = STDIN )
          old_settings = Termios.getattr(input)

          new_settings                     =  old_settings.dup
          new_settings.c_lflag             &= ~(Termios::ECHO | Termios::ICANON)
          new_settings.c_cc[Termios::VMIN] =  1

          begin
            Termios.setattr(input, Termios::TCSANOW, new_settings)
            input.getbyte
          ensure
            Termios.setattr(input, Termios::TCSANOW, old_settings)
          end
        end

Unix savvy getc(). (Second choice.)

WARNING: This method requires the external "stty" program!

[Source]

# File lib/highline/system_extensions.rb, line 126
          def get_character( input = STDIN )
            raw_no_echo_mode

            begin
              input.getbyte
            ensure
              restore_mode
            end
          end

Switched the input mode to raw and disables echo.

WARNING: This method requires the external "stty" program!

[Source]

# File lib/highline/system_extensions.rb, line 141
          def raw_no_echo_mode
            @state = `stty -g`
            system "stty raw -echo -icanon isig"
          end

Restores a previously saved input mode.

WARNING: This method requires the external "stty" program!

[Source]

# File lib/highline/system_extensions.rb, line 151
          def restore_mode
            system "stty #{@state}"
          end

A Windows savvy method to fetch the console columns, and rows.

[Source]

# File lib/highline/system_extensions.rb, line 42
      def terminal_size
        m_GetStdHandle               = Win32API.new( 'kernel32',
                                                     'GetStdHandle',
                                                     ['L'],
                                                     'L' )
        m_GetConsoleScreenBufferInfo = Win32API.new(
          'kernel32', 'GetConsoleScreenBufferInfo', ['L', 'P'], 'L'
        )

        format        = 'SSSSSssssSS'
        buf           = ([0] * format.size).pack(format)
        stdout_handle = m_GetStdHandle.call(0xFFFFFFF5)
        
        m_GetConsoleScreenBufferInfo.call(stdout_handle, buf)
        bufx, bufy, curx, cury, wattr,
        left, top, right, bottom, maxx, maxy = buf.unpack(format)
        return right - left + 1, bottom - top + 1
      end

A ncurses savvy method to fetch the console columns, and rows.

[Source]

# File lib/highline/system_extensions.rb, line 160
        def terminal_size
          size = [80, 40]
          FFI::NCurses.initscr
          begin
            size = FFI::NCurses.getmaxyx(stdscr).reverse
          ensure
            FFI::NCurses.endwin
          end
          size
        end

A Unix savvy method using stty that to fetch the console columns, and rows. … stty does not work in JRuby

[Source]

# File lib/highline/system_extensions.rb, line 173
        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

[Validate]