Giter Site home page Giter Site logo

high like/agree default option? about highline HOT 6 OPEN

satmandu avatar satmandu commented on September 27, 2024 1
high like/agree default option?

from highline.

Comments (6)

abinoam avatar abinoam commented on September 27, 2024 1

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. 👍

from highline.

abinoam avatar abinoam commented on September 27, 2024 1

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")

from highline.

satmandu avatar satmandu commented on September 27, 2024 1

I added an implementation here:

chromebrew/chromebrew#10498

from highline.

abinoam avatar abinoam commented on September 27, 2024 1

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

from highline.

satmandu avatar satmandu commented on September 27, 2024

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

from highline.

abinoam avatar abinoam commented on September 27, 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.

from highline.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.