Module: Prompter::Methods

Included in:
Prompter, Prompter
Defined in:
lib/prompter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#echoObject

The echo instance option (true by default)



43
44
45
# File 'lib/prompter.rb', line 43

def echo
  @echo ||= true
end

#prefixObject



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| … }

Parameters:

  • prompt (String)

    Your question

  • opts (Hash) (defaults to: {})

    the standard opts to pass along (see #say

Yields:

  • (input)

    yields the block with the user input

Yield Parameters:

  • input (String)

    user input

Returns:

  • (String)

    The user 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| … }

Parameters:

  • prompt (String)

    Your question

  • opts (Hash) (defaults to: {})

    the standard opts to pass along (see #say, plus the followings)

Options Hash (opts):

  • :input_end (String)

    Used to end the input (default nil: requires ^D to end the input)

  • :force_new_line (Boolean)

    Default to true

Yields:

  • (input)

    yields the block with the user input

Yield Parameters:

  • input (String)

    user input

Returns:

  • (String)

    The user input (with the eventual :input_end line removed)



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

Parameters:

  • prompt (String)

    Your question

  • validation (RegExp, Proc)

    Validates the input

  • opts (Hash) (defaults to: {})

    the standard opts to pass along (see #say)

Yields:

  • (input)

    yields the block with the user-chosen input

Yield Parameters:

  • input (String)

    The user input

Returns:

  • (String)

    The user-chosen input



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

Parameters:

  • prompt (String)

    Your question

  • list (Array)

    The list of choices

  • opts (Hash) (defaults to: {})

    the standard opts to pass along (see #say)

Yields:

  • yields the block with the user-chosen input

Yield Parameters:

  • index (Integer)

    The index number

Returns:

  • (Integer)

    The index number from the list referring to the user-chosen input



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

Parameters:

  • prompt (String)

    Your question

  • validation (RegExp, Proc)

    Validates the input

  • opts (Hash) (defaults to: {})

    the standard opts to pass along (see #say, plus the followings)

Options Hash (opts):

  • :split (String, RegExp)

    Used to split the input (see String#split)

Yields:

  • (input)

    yields the block with the user-chosen array of input

Yield Parameters:

  • input (Array)

    The user array of input

Returns:

  • (Array)

    The user-chosen array of input



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

Parameters:

  • prompt (String)

    Your question

  • list (Array)

    The list of choices

  • opts (Hash) (defaults to: {})

    the standard opts to pass along (see #say, plus the followings)

Options Hash (opts):

  • :split (String, RegExp)

    Used to split the input (see String#split)

Yields:

  • (input)

    yields the block with the user-chosen array of input

Yield Parameters:

  • indexes (Array)

    The user-chosen array of indexes

Returns:

  • (Array)

    The user-chosen array of indexes



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

Parameters:

  • prompt (String)

    Your question

  • opts (Hash) (defaults to: {})

    the standard opts to pass along (see #say)

Yields:

  • yields the block when the answer is no

Returns:

  • (Boolean)

    true for ‘n’ and false for ‘y’



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

Parameters:

  • message (String) (defaults to: "")

    Your message

  • opts (Hash) (defaults to: {})

    the standard opts to pass along

Options Hash (opts):

  • :default (String)

    The default choice when the user hits <enter> instead of any content

  • :prefix (String)

    The prefix string

  • :echo (Boolean)

    Adds a line showing the input (=> input)

  • :hint (String)

    A string to show the available choices

  • :style (Symbol)

    The Dye style name to use for this prompt

  • :force_new_line (Boolean)

    Forces the addition of a new line (otherwise determined by the end of the prompt string)



63
64
65
66
67
68
69
70
71
72
# File 'lib/prompter.rb', line 63

def say(message="", opts={})
  message = message.to_s
  opts = { :force_new_line => (message !~ /( |\t)$/),
           :style          => :say_style,
           :prefix         => prefix }.merge opts
  message = (opts[:prefix]||'') + message
  message = dye(message, *opts[:style]) unless opts[:style].nil?
  $stdout.send((opts[:force_new_line] ? :puts : :print), message)
  $stdout.flush
end

#say_log(message = "", opts = {}) ⇒ Object

Shows a colored message. It uses :say passing the :say_log_style :style option

See Also:



87
88
89
90
# File 'lib/prompter.rb', line 87

def say_log(message="", opts={})
  opts = { :style => :say_log_style }.merge opts
  say message, opts
end

#say_notice(message = "", opts = {}) ⇒ Object

Shows a colored message. It uses :say passing the :say_notice_style :style option

See Also:



96
97
98
99
# File 'lib/prompter.rb', line 96

def say_notice(message="", opts={})
  opts = { :style => :say_notice_style }.merge opts
  say message, opts
end

#say_ok(message = "", opts = {}) ⇒ Object

Shows a colored message. It uses :say passing the :say_ok_style :style option

See Also:



78
79
80
81
# File 'lib/prompter.rb', line 78

def say_ok(message="", opts={})
  opts = { :style => :say_ok_style }.merge opts
  say message, 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

See Also:



117
118
119
120
# File 'lib/prompter.rb', line 117

def say_title(message="", opts={})
  opts = { :style => :say_title_style }.merge opts
  say "\n #{message} \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.

See Also:



106
107
108
109
110
# File 'lib/prompter.rb', line 106

def say_warning(message="", opts={})
  opts = { :style => :say_warning_style }.merge opts
  message = "\a" + message unless opts[:mute]
  say message, opts
end

#yes?(prompt, opts = {}) { ... } ⇒ Boolean

Asks a yes/no question and yields the block only when the answer is yes

Parameters:

  • prompt (String)

    Your question

  • opts (Hash) (defaults to: {})

    the standard opts to pass along (see #say)

Yields:

  • yields the block when the answer is ‘y’

Returns:

  • (Boolean)

    true for ‘y’ and false for ‘n’



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

Parameters:

  • opts (Hash) (defaults to: {})

    the standard opts to pass along (see #say)

Yields:

  • yields the block always with a Boolean

Yield Parameters:

  • (Boolean)

Returns:

  • (Boolean)

    true for ‘y’ and false for ‘n’



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