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_log(message = "", opts = {}) ⇒ Object
Shows a colored message.
-
#say_notice(message = "", opts = {}) ⇒ Object
Shows a colored message.
-
#say_ok(message = "", opts = {}) ⇒ Object
Shows a colored message.
-
#say_title(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)
43 44 45 |
# File 'lib/prompter.rb', line 43 def echo @echo ||= true end |
#prefix ⇒ Object
47 48 49 |
# File 'lib/prompter.rb', line 47 def prefix @prefix ||= '' end |
Instance Method Details
#ask(prompt, opts = {}) {|input| ... } ⇒ String
Asks for an input
for block {|input| … }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/prompter.rb', line 133 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| … }
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/prompter.rb', line 161 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
183 184 185 186 187 188 189 190 191 |
# File 'lib/prompter.rb', line 183 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
270 271 272 273 274 275 |
# File 'lib/prompter.rb', line 270 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
205 206 207 208 209 210 211 212 213 |
# File 'lib/prompter.rb', line 205 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
289 290 291 292 293 294 |
# File 'lib/prompter.rb', line 289 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
254 255 256 257 |
# File 'lib/prompter.rb', line 254 def no?(prompt, opts={}) result = yes_no? prompt, opts (block_given? && !result) ? yield : !result end |
#say(message = "", opts = {}) ⇒ Object
Shows a message
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/prompter.rb', line 63 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_log(message = "", opts = {}) ⇒ Object
Shows a colored message. It uses :say passing the :say_log_style :style option
87 88 89 90 |
# File 'lib/prompter.rb', line 87 def say_log(="", opts={}) opts = { :style => :say_log_style }.merge opts say , opts end |
#say_notice(message = "", opts = {}) ⇒ Object
Shows a colored message. It uses :say passing the :say_notice_style :style option
96 97 98 99 |
# File 'lib/prompter.rb', line 96 def say_notice(="", opts={}) opts = { :style => :say_notice_style }.merge opts say , opts end |
#say_ok(message = "", opts = {}) ⇒ Object
Shows a colored message. It uses :say passing the :say_ok_style :style option
78 79 80 81 |
# File 'lib/prompter.rb', line 78 def say_ok(="", opts={}) opts = { :style => :say_ok_style }.merge opts say , opts end |
#say_title(message = "", opts = {}) ⇒ Object
Shows a colored message. It adds a new line and a space before and after the message and uses :say passing the :say_title_style :style option
117 118 119 120 |
# File 'lib/prompter.rb', line 117 def say_title(="", opts={}) opts = { :style => :say_title_style }.merge opts say "\n #{} \n", 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.
106 107 108 109 110 |
# File 'lib/prompter.rb', line 106 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
240 241 242 243 |
# File 'lib/prompter.rb', line 240 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
224 225 226 227 228 229 |
# File 'lib/prompter.rb', line 224 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 |