piotrmurach / tty-prompt

A beautiful and powerful interactive command line prompt
https://ttytoolkit.org
MIT License
1.47k stars 136 forks source link

%{value} not present when using validate with a proc #159

Closed bbugh closed 3 years ago

bbugh commented 3 years ago

Hi! 👋 Thanks again for this exceptional set of gems.

Describe the problem

When using validate ->(input)... and customizing the message, the %{value} token is not present. I put in a pry at the error location, and the only token available is valid, which is the proc itself.

Steps to reproduce the problem

key(:filename).ask("File name", required: true) do |q|
    q.modify :remove
    q.validate ->(input) { !File.exists?("app/somewhere/#{input}.html") }, "Filename %{value} already exists!"
end

Actual behaviour

It throws an error

KeyError: key{value} not found
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/question.rb:87:in `%'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/question.rb:87:in `message_for'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/question/checks.rb:58:in `call'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/result.rb:17:in `with'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/evaluator.rb:20:in `each'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/evaluator.rb:20:in `reduce'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/evaluator.rb:20:in `call'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/question.rb:159:in `process_input'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/question.rb:114:in `render'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/question.rb:104:in `block in call'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-reader-0.7.0/lib/tty/reader.rb:107:in `subscribe'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/question.rb:103:in `call'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt.rb:172:in `invoke_question'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt.rb:193:in `ask'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/answers_collector.rb:73:in `public_send'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/answers_collector.rb:73:in `method_missing'
/Users/bbugh/projects/project/lib/tasks/sample/create.rake:12:in `block (3 levels) in <main>'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/answers_collector.rb:21:in `instance_eval'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt/answers_collector.rb:21:in `call'
/Users/bbugh/.rvm/gems/ruby-2.6.6/gems/tty-prompt-0.21.0/lib/tty/prompt.rb:532:in `collect'
/Users/bbugh/projects/project/lib/tasks/sample/create.rake:9:in `block (2 levels) in <main>'
Tasks: TOP => sample:create

Expected behaviour

It would result in a value, like the other messages.

Describe your environment

piotrmurach commented 3 years ago

Hi Brian 👋

Thanks for using tty-prompt.

Before I try to answer your query, the current version is 0.23.0, so please upgrade as a lot has changed.

When using validate ->(input)... and customizing the message, the %{value} token is not present.

I'm curious to know how did you find the %{value}? I must admit this is the first time I hear about such an ability in the validate method and I cannot find it in the readme or the source code. Is this a feature request?

bbugh commented 3 years ago

Thanks for the quick reply!

Before I try to answer your query, the current version is 0.23.0, so please upgrade as a lot has changed.

Ack! Bundler strikes again. I just added the unversioned gem to my Gemfile and that's the version bundler pulled. Not the first time that has burned me - I've looked everywhere to see if bundler had some config hidden somewhere to choose cache over remote. Thanks for the heads up.

I'm curious to know how did you find the %{value}? I must admit this is the first time I hear about such an ability in the validate method and I cannot find it in the readme or the source code. Is this a feature request?

It wasn't intended as a feature request, but I suppose it is now! It looks like I had a misunderstanding:

  1. In the README, I saw that the %{value} token worked in convert and manually setting messages for range? and convert?.
  2. When I put a binding.pry in at the tokenization code, the validate command had a token valid, but not value
  3. I was able to get an output by trying both q.validate ->(input) { false }, "%{valid}" and q.messages[:valid?] = "%{valid}" which output the proc's inspect:
    File name: something
    >> #<Proc:0x00007fe8956990f8@/Users/bbugh/projects/project/lib/tasks/sample/create.rake:30 (lambda)>

In Rails, validation messages also use %{value}, and I think this subconciously influenced my thinking.

Altogether, the result was that I made the incorrect assumption that the value token was expected to be available in all situations for setting messages (which would be really helpful in our use case).

piotrmurach commented 3 years ago

My reply was way too hasty - sorry! I appreciate your patience with this issue. I only quickly looked at the validate method and didn't realise that %{value} is indeed used in convert and in methods. I 💯 agree that this should be supported. Do you have time to check against the master branch?

bbugh commented 3 years ago

I checked on master, it works great! Thank you so much.

File name: test_file
>> Filename 'test_file' already exists!