Skip to content

Commit

Permalink
Fixed raw_no_echo_mode so that it uses stty -icanon rather than cbreak
Browse files Browse the repository at this point in the history
as cbreak does not appear to be the posixly correct argument. It fails
on Solaris if cbreak is used.
  • Loading branch information
jacott committed Mar 23, 2010
1 parent 6fccbda commit 25ca825
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Below is a complete listing of changes for each revision of HighLine.

== 1.5.4

* Fixed raw_no_echo_mode so that it uses stty -icanon rather than cbreak
as cbreak does not appear to be the posixly correct argument. It fails
on Solaris if cbreak is used.

== 1.5.3

* Fixed an issue that kept Menu from showing the correct choices for
Expand Down
44 changes: 22 additions & 22 deletions lib/highline/system_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ module SystemExtensions

#
# Windows savvy getc().
#
#
#
#
def get_character( input = STDIN )
@stdin_handle ||= GetStdHandle(STD_INPUT_HANDLE)

Expand All @@ -38,7 +38,7 @@ def get_character( input = STDIN )
input.getbyte
ensure
SetConsoleEcho(@stdin_handle, true)
end
end
end

# A Windows savvy method to fetch the console columns, and rows.
Expand All @@ -61,7 +61,7 @@ def SetConsoleEcho( console_handle, on )
mode &= ~ENABLE_ECHO_INPUT
end

ok = SetConsoleMode(console_handle, mode)
ok = SetConsoleMode(console_handle, mode)
end

# win32 console APIs
Expand All @@ -77,12 +77,12 @@ def SetConsoleEcho( console_handle, on )
ENABLE_WINDOW_INPUT = 0x0008
ENABLE_MOUSE_INPUT = 0x0010
ENABLE_INSERT_MODE = 0x0020
ENABLE_QUICK_EDIT_MODE = 0x0040
ENABLE_QUICK_EDIT_MODE = 0x0040

@@apiGetStdHandle = nil
@@apiGetConsoleMode = nil
@@apiSetConsoleMode = nil
@@apiGetConsoleScreenBufferInfo = nil
@@apiGetConsoleScreenBufferInfo = nil

def GetStdHandle( handle_type )
@@apiGetStdHandle ||= Win32API.new( "kernel32", "GetStdHandle",
Expand All @@ -105,18 +105,18 @@ def SetConsoleMode( console_handle, mode )
['L', 'L'], 'I' )

@@apiSetConsoleMode.call(console_handle, mode) != 0
end
end

def GetConsoleScreenBufferInfo( console_handle )
@@apiGetConsoleScreenBufferInfo ||=
Win32API.new( "kernel32", "GetConsoleScreenBufferInfo",
['L', 'P'], 'L' )

format = 'SSSSSssssSS'
buf = ([0] * format.size).pack(format)
buf = ([0] * format.size).pack(format)
@@apiGetConsoleScreenBufferInfo.call(console_handle, buf)
buf.unpack(format)
end
end

rescue LoadError # If we're not on Windows try...
begin
Expand All @@ -126,9 +126,9 @@ def GetConsoleScreenBufferInfo( console_handle )

#
# Unix savvy getc(). (First choice.)
#
#
# *WARNING*: This method requires the "termios" library!
#
#
def get_character( input = STDIN )
old_settings = Termios.getattr(input)

Expand All @@ -144,10 +144,10 @@ def get_character( input = STDIN )
end
end
rescue LoadError # If our first choice fails, try using ffi-ncurses.
begin
begin
require 'ffi-ncurses' # The ffi gem is builtin to JRUBY and because stty does not
# work correctly in JRuby manually installing the ffi-ncurses
# gem is the only way to get highline to operate correctly in
# 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.
Expand All @@ -156,7 +156,7 @@ def get_character( input = STDIN )

#
# ncurses savvy getc(). (JRuby choice.)
#
#
def get_character( input = STDIN )
FFI::NCurses.initscr
FFI::NCurses.cbreak
Expand All @@ -171,14 +171,14 @@ def get_character( input = STDIN )
rescue LoadError # If the ffi-ncurses choice fails, try using stty
if JRUBY
STDERR.puts "\n*** Using highline effectively in JRuby requires manually installing the ffi-ncurses gem.\n*** jruby -S gem install ffi-ncurses"
end
end
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

Expand All @@ -191,19 +191,19 @@ def get_character( input = STDIN )

#
# 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"
system "stty raw -echo -icanon isig"
end

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

0 comments on commit 25ca825

Please sign in to comment.