JEG2 / highline

A higher level command-line oriented interface.
Other
1.29k stars 137 forks source link

Incosistent behavior when ask with a validator and readline=true #221

Closed ailisp closed 7 years ago

ailisp commented 7 years ago

Hi, we're using highline in a console for a cloud management platform: https://github.com/manageiq/manageiq-appliance_console. Recently we add all Highline.ask with readline=true option to add arrow key navigation. It works fine for no validator ask, but for cases with a validator, like Highline.agree or Highline.choose, it's behavior is inconsistent with the same method call of readline=false option. Though we use a previous version of highline, but I just test with current master branch and inconsistency still exist. For a minimum example, try some invalid input to these two agree:

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

def agree_readline(prompt)
  HighLine.new.agree(prompt) do |q|
    q.readline=true
  end
end

def agree(prompt)
  HighLine.new.agree(prompt)
end

agree_readline("agree readline=true? ")
agree("agree readline=false? ")

Similar problems for choose or manually call ask given validator. I did some hacks in our project to make them lo behave the same way as readline=false. But I think it's better to fix here.

abinoam commented 7 years ago

Hi @ailisp,

Thanks for reporting the issue. What exactly is happening on your setup? What behaviour is inconsistent?

ailisp commented 7 years ago

Hi @abinoam Thanks for quick response. In a clone of current master branch. save the content I provide in previous comment as test.rb:

[boyao@new-host highline]$ bundler exec ./test.rb
agree readline=true?aaa
Please enter "yes" or "no".
agree readline=true?
agree readline=true?
Please enter "yes" or "no".
agree readline=true?
agree readline=true?yes
agree readline=false?
aaa
Please enter "yes" or "no".
agree readline=false?

Please enter "yes" or "no".
agree readline=false?
yes

I input aaa, nothing and yes for both case (readline=true or false), and you can see with readline=true there's no extra newline after question, but a redundant question. For choose, if we have readline=true then giving an invalid input, the question and menu will both display again, but for readline=false only question will display again.

abinoam commented 7 years ago

I think I understood.

I've had a quick look at the code now and couldn't figure out a quick fix. I'll try to take a look at it next week and try to fix it or at least provide a way to work around it.

👍

abinoam commented 7 years ago

I see that the use of readline = true at agree is pretty much unimplemented. It doesn't even autocomplete for yes or no.

ailisp commented 7 years ago

Yes. just noticed the auto complete part is not implemented. And here's of some hacks I did for make choose and agree looks the same as readline=false, it works but with some ugly hacks so I don't make a PR here direcly. I think you're more familiar with GNU Readline, but hope this can save you some time.

abinoam commented 7 years ago

@ailisp Could you please have a look at PR #222 ?

ailisp commented 7 years ago

@abinoam Just tested with ask and choose, readline=true works consistently as expected and ompletion also works. Thanks for the fix!

abinoam commented 7 years ago

Thank you very much @ailisp! I'll merge it and release it. 👍

abinoam commented 7 years ago

Released https://rubygems.org/gems/highline/versions/2.0.0.pre.develop.14