Fix for #22 that uses JLine when running in JRuby #27

Merged
merged 5 commits into from Nov 29, 2011

Projects

None yet

2 participants

@tomdz
tomdz commented Nov 29, 2011

Turns out JLine is built into JRuby (it uses it for its readline support), so this should work out of the box with a reasonably modern jruby.

I also fixed a bug in the handling of @question.limit in that it would echo one less character than was entered.

Here is a test script that I used to try out the various combinations:

#!/usr/bin/env ruby
require 'rubygems'
require 'highline/import'

puts "CHARACTER_MODE = #{HighLine::SystemExtensions::CHARACTER_MODE}"

def ask_and_tell(params)
  params = { :readline => false, :character => nil, :echo => true, :overwrite => false, :limit => nil }.merge(params)
  response = ask(params[:prompt]) do |q|
    q.readline = params[:readline]
    q.character = params[:character]
    q.echo = params[:echo]
    q.overwrite = params[:overwrite]
    q.limit = params[:limit]
  end
  puts "=> " + response
end

ask_and_tell(:prompt => "Same line, defaults > ")
ask_and_tell(:prompt => "Same line, readline > ", :readline => true)
ask_and_tell(:prompt => "Same line, :getc > ", :character => :getc)
ask_and_tell(:prompt => "Same line, character = true > ", :character => true)
ask_and_tell(:prompt => "Same line, no echo > ", :echo => false)
ask_and_tell(:prompt => "Same line, echo masked > ", :echo => '*')
ask_and_tell(:prompt => "Same line, limit > ", :limit => 5)
ask_and_tell(:prompt => "Same line, limit and no echo > ", :limit => 5, :echo => false)
ask_and_tell(:prompt => "Same line, limit and masked echo > ", :limit => 5, :echo => '*')
ask_and_tell(:prompt => "Same line, overwrite > ", :overwrite => true)
ask_and_tell(:prompt => "Same line, overwrite and no echo > ", :overwrite => true, :echo => false)
ask_and_tell(:prompt => "Same line, overwrite and masked echo > ", :overwrite => true, :echo => '*')
ask_and_tell(:prompt => "Same line, overwrite and limit > ", :overwrite => true, :limit => 5)
ask_and_tell(:prompt => "Same line, overwrite and limit and no echo > ", :overwrite => true, :limit => 5, :echo => false)
ask_and_tell(:prompt => "Same line, overwrite and limit and masked echo > ", :overwrite => true, :limit => 5, :echo => '*')

ask_and_tell(:prompt => "New line, defaults\n")
ask_and_tell(:prompt => "New line, readline\n", :readline => true)
ask_and_tell(:prompt => "New line, :getc\n", :character => :getc)
ask_and_tell(:prompt => "New line, character = true\n", :character => true)
ask_and_tell(:prompt => "New line, no echo\n", :echo => false)
ask_and_tell(:prompt => "New line, echo masked\n", :echo => '*')
ask_and_tell(:prompt => "New line, limit\n", :limit => 5)
ask_and_tell(:prompt => "New line, limit and no echo\n", :limit => 5, :echo => false)
ask_and_tell(:prompt => "New line, limit and masked echo\n", :limit => 5, :echo => '*')
ask_and_tell(:prompt => "New line, overwrite\n", :overwrite => true)
ask_and_tell(:prompt => "New line, overwrite and no echo\n", :overwrite => true, :echo => false)
ask_and_tell(:prompt => "New line, overwrite and masked echo\n", :overwrite => true, :echo => '*')
ask_and_tell(:prompt => "New line, overwrite and limit\n", :overwrite => true, :limit => 5)
ask_and_tell(:prompt => "New line, overwrite and limit and no echo\n", :overwrite => true, :limit => 5, :echo => false)
ask_and_tell(:prompt => "New line, overwrite and limit and masked echo\n", :overwrite => true, :limit => 5, :echo => '*')
@JEG2 JEG2 merged commit fc255ba into JEG2:master Nov 29, 2011
@JEG2
Owner
JEG2 commented Nov 29, 2011

Thanks.

@tomdz
tomdz commented Dec 14, 2011

Any chance you could release a new highline version containing this ?

cheers,
Tom

@JEG2
Owner
JEG2 commented Dec 18, 2011

I have pushed 1.6.9 with this fix.

@tomdz
tomdz commented Dec 18, 2011

Thanks, somehow I didn't notice this :)

@rsutphin rsutphin added a commit to rsutphin/highline that referenced this pull request Feb 23, 2013
@rsutphin rsutphin Use the configured streams on JRuby with JLine.
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 #31, which was introduced by PR #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 #27 was merged, so it seems likely the failures were caused
by #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.
b0d81a8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment