Module: Prompter::Methods
Instance Attribute Summary collapse
-
#echo ⇒ Object
The echo instance option (true by default).
- #prefix ⇒ Object
Instance Method Summary collapse
-
#ask(prompt, opts = {}) {|input| ... } ⇒ String
Asks for an input.
-
#ask_multiline(prompt, opts = {}) {|input| ... } ⇒ String
Asks for a multiline input.
-
#choose(prompt, validation, opts = {}) {|input| ... } ⇒ String
Chooses among different choices.
-
#choose_index(prompt, list, opts = {}) {|index| ... } ⇒ Integer
Chooses among different choices in an indexed list.
-
#choose_many(prompt, validation, opts = {}) {|input| ... } ⇒ Array
Chooses many among different choices.
-
#choose_many_index(prompt, list, opts = {}) {|input| ... } ⇒ Array
Chooses many among different choices in an indexed list.
-
#no?(prompt, opts = {}) { ... } ⇒ Boolean
Asks a yes/no question and yields the block only when the answer is no.
-
#say(message = "", opts = {}) ⇒ Object
Shows a message.
-
#say_notice(message = "", opts = {}) ⇒ Object
Shows a colored message.
-
#say_warning(message = "", opts = {}) ⇒ Object
Shows a red colored message.
-
#yes?(prompt, opts = {}) { ... } ⇒ Boolean
Asks a yes/no question and yields the block only when the answer is yes.
-
#yes_no?(prompt, opts = {}) {|| ... } ⇒ Boolean
Asks a yes/no question and yields the block with the resulting Boolean.
Instance Attribute Details
#echo ⇒ Object
The echo instance option (true by default)
40 41 42 |
# File 'lib/prompter.rb', line 40 def echo @echo ||= true end |
#prefix ⇒ Object
44 45 46 |
# File 'lib/prompter.rb', line 44 def prefix @prefix ||= '' end |
Instance Method Details
#ask(prompt, opts = {}) {|input| ... } ⇒ String
Asks for an input
for block {|input| … }
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/prompter.rb', line 102 def ask(prompt, opts={}) opts = { :style => :ask_style, :hint => '', :default => '' }.merge opts force_new_line = !!opts.delete(:force_new_line) unless opts[:hint].empty? prompt = prompt + ' ' unless opts[:force_new_line] say prompt, opts say_hint(opts[:hint], opts.merge({:force_new_line => !!force_new_line})) unless opts[:hint].empty? input = $stdin.gets || '' # multilines ended at start generate a nil input.strip! input = opts[:default].to_s if input.empty? say_echo(input, opts) unless opts[:echo] == false || echo == false block_given? ? yield(input) : input end |
#ask_multiline(prompt, opts = {}) {|input| ... } ⇒ String
Asks for a multiline input
for block {|input| … }
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/prompter.rb', line 130 def ask_multiline(prompt, opts={}) opts = { :input_end => nil, :force_new_line => true, :hint => %([end the input by typing "#{opts[:input_end] || '^D'}" in a new line]) }.merge opts old_separator = $/ $/ = opts.delete(:input_end) input = ask(prompt, opts).sub(/\n#{$/}$/, '') $/ = old_separator block_given? ? yield(input) : input end |
#choose(prompt, validation, opts = {}) {|input| ... } ⇒ String
Chooses among different choices
152 153 154 155 156 157 158 159 160 |
# File 'lib/prompter.rb', line 152 def choose(prompt, validation, opts={}, &block) choice = ask prompt, opts if valid_choice?(validation, choice) block_given? ? yield(choice) : choice else say_warning 'Unknown choice!' choose(prompt, validation, opts, &block) end end |
#choose_index(prompt, list, opts = {}) {|index| ... } ⇒ Integer
Chooses among different choices in an indexed list
239 240 241 242 243 244 |
# File 'lib/prompter.rb', line 239 def choose_index(prompt, list, opts={}) opts = list_choices(prompt, list, opts) choice = choose ">", lambda{|v| (1..list.size).map(&:to_s).include?(v) }, opts index = choice.to_i-1 block_given? ? yield(index) : index end |
#choose_many(prompt, validation, opts = {}) {|input| ... } ⇒ Array
Chooses many among different choices
174 175 176 177 178 179 180 181 182 |
# File 'lib/prompter.rb', line 174 def choose_many(prompt, validation, opts={}, &block) choices = ask(prompt, opts).split(opts[:split]) if choices.all? {|c| valid_choice?(validation, c)} block_given? ? yield(choices) : choices else say_warning 'One or more choices are unknown!' choose_many(prompt, validation, opts, &block) end end |
#choose_many_index(prompt, list, opts = {}) {|input| ... } ⇒ Array
Chooses many among different choices in an indexed list
258 259 260 261 262 263 |
# File 'lib/prompter.rb', line 258 def choose_many_index(prompt, list, opts={}) opts = list_choices(prompt, list, opts, true) choices = choose_many ">", lambda{|v| (1..list.size).map(&:to_s).include?(v) }, opts indexes = choices.map {|i| i.to_i-1 } block_given? ? yield(indexes) : indexes end |
#no?(prompt, opts = {}) { ... } ⇒ Boolean
Asks a yes/no question and yields the block only when the answer is no
223 224 225 226 |
# File 'lib/prompter.rb', line 223 def no?(prompt, opts={}) result = yes_no? prompt, opts (block_given? && !result) ? yield : !result end |
#say(message = "", opts = {}) ⇒ Object
Shows a message
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/prompter.rb', line 60 def say(="", opts={}) = .to_s opts = { :force_new_line => ( !~ /( |\t)$/), :style => :say_style, :prefix => prefix }.merge opts = (opts[:prefix]||'') + = dye(, *opts[:style]) unless opts[:style].nil? $stdout.send((opts[:force_new_line] ? :puts : :print), ) $stdout.flush end |
#say_notice(message = "", opts = {}) ⇒ Object
Shows a colored message. It uses :say passing the :say_notice_style :style option
75 76 77 78 |
# File 'lib/prompter.rb', line 75 def say_notice(="", opts={}) opts = { :style => :say_notice_style }.merge opts say , opts end |
#say_warning(message = "", opts = {}) ⇒ Object
Shows a red colored message. It uses :say passing the :say_warning_style :style option, besides it adds an audible bell character to the message. Pass :mute => true to mute.
85 86 87 88 89 |
# File 'lib/prompter.rb', line 85 def say_warning(="", opts={}) opts = { :style => :say_warning_style }.merge opts = "\a" + unless opts[:mute] say , opts end |
#yes?(prompt, opts = {}) { ... } ⇒ Boolean
Asks a yes/no question and yields the block only when the answer is yes
209 210 211 212 |
# File 'lib/prompter.rb', line 209 def yes?(prompt, opts={}) result = yes_no? prompt, opts (block_given? && result) ? yield : result end |
#yes_no?(prompt, opts = {}) {|| ... } ⇒ Boolean
Asks a yes/no question and yields the block with the resulting Boolean
193 194 195 196 197 198 |
# File 'lib/prompter.rb', line 193 def yes_no?(prompt, opts={}) opts = { :hint => '[y|n]' }.merge opts choice = choose(prompt, /^y|n$/i, opts) result = choice.match(/^y$/i) ? true : false block_given? ? yield(result) : result end |