Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

high like/agree default option? #275

Open
satmandu opened this issue Aug 18, 2024 · 6 comments
Open

high like/agree default option? #275

satmandu opened this issue Aug 18, 2024 · 6 comments

Comments

@satmandu
Copy link

Is there a way to set a default value when using agree such that enter would choose that default?

(If that's not currently possible... This is a feature request.)

@abinoam
Copy link
Collaborator

abinoam commented Aug 19, 2024

It completely makes sense for me to have such feature documented.

The current implementation of #agree is at this link https://github.com/JEG2/highline/blob/3de8af1ea83016ede666f33917233fb7a6854fb7/lib/highline.rb#L192C1-L202C6

I believe, #ask is so flexible that we can achieve this by simply messing with its arguments.

I think I can do it this week. 👍

@abinoam
Copy link
Collaborator

abinoam commented Aug 21, 2024

Meanwhile, you can try this.

I have changed the validate regexp to match an empty string (the result of an enter).

require "highline"

def agree_with_default(yes_or_no_question, character = nil, default:)
  answer_type = ->(yn) { yn.downcase[0] == "y" || (yn.empty? && default.downcase[0] == "y") }

  HighLine.ask(yes_or_no_question, answer_type) do |q|
    q.validate                 = /\A(?:y(?:es)?|no?|)\Z/i
    q.responses[:not_valid]    = 'Please enter "yes" or "no".'
    q.responses[:ask_on_error] = :question
    q.character                = character
    q.completion               = %w[yes no]

    yield q if block_given?
  end
end

# Using it
agree_with_default("Install lib xyz (yes/NO)?", true, default: "n")

@satmandu
Copy link
Author

Have you considered using timeout with highline at all?

Something simple like this might work?

require 'timeout'
TIMEOUT_SECONDS = 10
begin
  Timeout::timeout(TIMEOUT_SECONDS) do
    <highline stuff with default>
  end
rescue Timeout::Error
  <do default>
end

@abinoam
Copy link
Collaborator

abinoam commented Sep 19, 2024

It would be a really nice addition. I use things like that in shell scripts like
It would work like read -t <timeout> -p <prompt> <name> of Bash.
It's nice for "unattended" installs or something like that. But then you would have the chance to give a window of time for the user to change something, otherwise, it would follow the defaults.

As soon as I get some free time I can try doing that.
Thanks for the suggestion.

@satmandu
Copy link
Author

I added an implementation here:

chromebrew/chromebrew#10498

@abinoam
Copy link
Collaborator

abinoam commented Sep 19, 2024

Nice. Perhaps we could do that from inside the "answer gathering" workflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants