Comments (6)
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.
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.
I added an implementation here:
from highline.
Nice. Perhaps we could do that from inside the "answer gathering" workflow.
from highline.
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.
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)
- undefined method 'default_instance' HOT 3
- Default Highline Example on GithubGives Error HOT 5
- HighLine Terminal::IOConsole#restore_mode does not work with StringIO HOT 3
- System extensions depend on now-internalized JRuby dependencies HOT 14
- consider exposing Readline.completion_proc via HighLine::Question#selection HOT 3
- default value has to be a string HOT 14
- Ctrl-C improperly handled when q.echo = false HOT 6
- Issues with echo: false and not on TTY HOT 7
- Can't get terminal_size HOT 11
- echo=false or echo='*' requires extra character after enter on Windows HOT 6
- Possible to accept multiple selections with `.in` and `.default`? HOT 5
- test failing when ruby is built against libedit instead of readline HOT 3
- Have to set convert type in order for `.in` to work with array
- 2.1.0 was released, but tag is missing in repository HOT 2
- Make Highline Ruby 3.3 and reline compatible HOT 1
- Update tests for Reline (as the substitute of Readline)
- Add `abbrev` to gemspec HOT 2
- `ask()` causes `Readline`-related error on Ruby 3.3 HOT 5
- `::add_to_color_scheme` improvement HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from highline.