Skip to content

Commit

Permalink
Use the configured streams on JRuby with JLine.
Browse files Browse the repository at this point in the history
Before this commit, the input and output parameters to HighLine.new
were ignored for #ask (both for specifying the input stream and for
displaying the prompt).

This commit fixes issue JEG2#31, which was introduced by PR JEG2#27. Since
then, the HighLine tests have been unable to run on JRuby. With this
change, the tests run on JRuby 1.7.3 but many of them fail. No tests
fail on JRuby 1.7.3 using the version of HighLine from immediately
before JEG2#27 was merged, so it seems likely the failures were caused
by JEG2#27 also.

This commit stops using `SystemExtensions#initialize` to set up JLine.
It would have been possible to pass the input and output parameters to
the `super` call, but that seemed to me like an abuse of inheritance.
Instead, following the pattern of the rest of SystemExtensions, I
added a method called `initialize_system_extensions` which is defined
on a per-platform basis (currently for JRuby only) and invoked by
`HighLine#initialize` when present.
  • Loading branch information
rsutphin committed Feb 23, 2013
1 parent 4b42ae9 commit b0d81a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/highline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ def self.const_missing(name)
#
def initialize( input = $stdin, output = $stdout,
wrap_at = nil, page_at = nil, indent_size=3, indent_level=0 )
super()
@input = input
@output = output

Expand All @@ -193,6 +192,8 @@ def initialize( input = $stdin, output = $stdout,
@gather = nil
@answers = nil
@key = nil

initialize_system_extensions if respond_to?(:initialize_system_extensions)
end

include HighLine::SystemExtensions
Expand Down
10 changes: 5 additions & 5 deletions lib/highline/system_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class HighLine
module SystemExtensions
JRUBY = defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'

def initialize
if JRUBY
if JRUBY
def initialize_system_extensions
require 'java'
if JRUBY_VERSION =~ /^1.7/
java_import 'jline.console.ConsoleReader'

@java_console = ConsoleReader.new($stdin.to_inputstream, $stdout.to_outputstream)
@java_console = ConsoleReader.new(@input.to_inputstream, @output.to_outputstream)
@java_console.set_history_enabled(false)
@java_console.set_bell_enabled(true)
@java_console.set_pagination_enabled(false)
Expand All @@ -28,8 +28,8 @@ def initialize
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_input = Channels.newInputStream(@input.to_channel)
@java_output = OutputStreamWriter.new(Channels.newOutputStream(@output.to_channel))
@java_terminal = Terminal.getTerminal
@java_console = ConsoleReader.new(@java_input, @java_output)
@java_console.setUseHistory(false)
Expand Down

0 comments on commit b0d81a8

Please sign in to comment.