Skip to content

Commit

Permalink
Update JRuby code to work with JRuby-1.7, which uses jline-2.7
Browse files Browse the repository at this point in the history
This is an initial update. A bit of refactoring is needed. A lot of code can
move into system_extensions.rb so we can have highline.rb clean of any special
casing.
  • Loading branch information
mnzaki committed Jul 17, 2012
1 parent 334844a commit 721bc1e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
44 changes: 17 additions & 27 deletions lib/highline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,12 @@ def initialize( input = $stdin, output = $stdout,
require 'java'
java_import 'java.io.OutputStreamWriter'
java_import 'java.nio.channels.Channels'
java_import 'jline.ConsoleReader'
java_import 'jline.Terminal'

@java_input = Channels.newInputStream($stdin.to_channel)
@java_output = OutputStreamWriter.new(Channels.newOutputStream($stdout.to_channel))
@java_terminal = Terminal.getTerminal
@java_console = ConsoleReader.new(@java_input, @java_output)
@java_console.setUseHistory(false)
@java_console.setBellEnabled(true)
@java_console.setUsePagination(false)
java_import 'jline.console.ConsoleReader'

@java_console = ConsoleReader.new($stdin.to_inputstream, $stdout.to_outputstream)
@java_console.set_history_enabled(false)
@java_console.set_bell_enabled(true)
@java_console.set_pagination_enabled(false)
end

self.wrap_at = wrap_at
Expand Down Expand Up @@ -255,7 +251,9 @@ def ask( question, answer_type = String, &details ) # :yields: question
# readline() needs to handle it's own output, but readline only supports
# full line reading. Therefore if @question.echo is anything but true,
# the prompt will not be issued. And we have to account for that now.
say(@question) unless (@question.readline and @question.echo == true)
# Also, JRuby-1.7's ConsoleReader.readLine() needs to be passed the prompt
# to handle line editing properly.
say(@question) unless ((JRUBY or @question.readline) and @question.echo == true)
begin
@answer = @question.answer_or_default(get_response)
unless @question.valid_answer?(@answer)
Expand Down Expand Up @@ -796,13 +794,7 @@ def get_line( )
answer
else
if JRUBY
enable_echo_afterwards = @java_terminal.isEchoEnabled
@java_terminal.disableEcho
begin
raw_answer = @java_console.readLine(nil, nil)
ensure
@java_terminal.enableEcho if enable_echo_afterwards
end
raw_answer = @java_console.readLine(@question.question, nil)
else
raise EOFError, "The input stream is exhausted." if @@track_eof and
@input.eof?
Expand All @@ -814,9 +806,7 @@ def get_line( )
end

def get_single_character(is_stty)
if JRUBY
@java_console.readVirtualKey
elsif is_stty
if JRUBY or is_stty
@input.getbyte
else
get_character(@input)
Expand All @@ -842,8 +832,8 @@ def get_response( )
get_line
else
if JRUBY
enable_echo_afterwards = @java_terminal.isEchoEnabled
@java_terminal.disableEcho
echoChar = @java_console.getEchoCharacter
@java_console.setEchoCharacter 0
elsif stty
raw_no_echo_mode
end
Expand Down Expand Up @@ -886,7 +876,7 @@ def get_response( )
end
ensure
if JRUBY
@java_terminal.enableEcho if enable_echo_afterwards
@java_console.setEchoCharacter echoChar
elsif stty
restore_mode
end
Expand All @@ -902,8 +892,8 @@ def get_response( )
end
else
if JRUBY
enable_echo_afterwards = @java_terminal.isEchoEnabled
@java_terminal.disableEcho
echoChar = @java_console.getEchoCharacter
@java_console.setEchoCharacter 0
end
begin
if @question.character == :getc
Expand All @@ -926,7 +916,7 @@ def get_response( )
end
ensure
if JRUBY
@java_terminal.enableEcho if enable_echo_afterwards
@java_console.setEchoCharacter echoChar
end
end
@question.change_case(response)
Expand Down
3 changes: 1 addition & 2 deletions lib/highline/system_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,8 @@ def terminal_size
elsif JRUBY
# JRuby running on Unix can fetch the number of columns and rows from the builtin Jline library
require 'java'
java_import 'jline.Terminal'
def terminal_size
java_terminal = @java_terminal || Terminal.getTerminal
java_terminal = @java_console.getTerminal
[ java_terminal.getTerminalWidth, java_terminal.getTerminalHeight ]
end
else
Expand Down

0 comments on commit 721bc1e

Please sign in to comment.