lib/cli/mastermind/user_interface.rb
# Display an interactive list of options for the user to select.
# If less than 2 options would be displayed, the default value is automatically
# returned unless +multiple:+ is +true+.
#
# @param question [String] The question to ask the user
# @param options [Array<#to_s>,Hash<#to_s>] the options to display
# @param default [String] The default value for this question.  Assumed to exist
#   within the given options.
# @param opts [Hash] additional options passed into +CLI::UI::Prompt.ask+.
# @option opts [Boolean] :multiple (false) Whether multiple selections should be made.
# @option opts [Boolean] :filter_ui (true) Enable option filtering
# @option opts [Boolean] :select_ui (true) Enable long-form option selection
#
# @see https://github.com/Shopify/cli-ui#interactive-prompts
Display an interactive list of options for the user to select.
If less than 2 options would be displayed, the default value is automatically
returned unless +multiple:+ is +true+.
Display an interactive list of options for the user to select . If less than 2 options would be displayed the default value is automatically returned unless + multiple : + is + true + .
def select(question, options:, default: options.first, **opts)
    default_value = nil
    options = case options
              when Array
                default_text = default
                o = options - [default]
                o.zip(o).to_h
              when Hash
                if default.is_a? Array
                  default_text, default = default
                else
                  default_text = options.invert[default]
                end
                default_text = default_text
                options.dup.tap { |o| o.delete(default_text) }
              end
    default_text = default_text.to_s
    options.transform_keys!(&:to_s)
    if opts[:multiple]
      keys = [default_text] + options.keys
      options[default_text] = default
      response = Array(CLI::UI::Prompt.ask(question, options: keys, **opts))
      return response.map { |resp| options[resp] }
    end
    return default unless options.count > 0
    CLI::UI::Prompt.ask(question, **opts) do |handler|
      handler.option(default_text) { default }
      options.each do |(text, value)|
        handler.option(text) { value }
      end
    end
  end
def select ( question , options : , default : options . first , ** opts ) default_value = nil options = case options when Array default_text = default o = options - [ default ] o . zip ( o ) . to_h when Hash if default . is_a? Array default_text , default = default else default_text = options . invert [ default ] end default_text = default_text options . dup . tap { | o | o . delete ( default_text ) } end default_text = default_text . to_s options . transform_keys! ( & :to_s ) if opts [ :multiple ] keys = [ default_text ] + options . keys options [ default_text ] = default response = Array ( CLI :: UI :: Prompt . ask ( question , options : keys , ** opts ) ) return response . map { | resp | options [ resp ] } end return default unless options . count > 0 CLI :: UI :: Prompt . ask ( question , ** opts ) do | handler | handler . option ( default_text ) { default } options . each do | ( text , value ) | handler . option ( text ) { value } end end end
Display an interactive list of options for the user to select.
Display an interactive list of options for the user to select .
# Handle the "default" default.  Otherwise, we expect the default # is the default value # dup so that we don't change whatever was passed in # Ensure all keys are strings for CLI::UI # work around a bug in CLI::UI with multi-select and block invocation # Put default at the beginning so it shows in the expected order # Put default back into the options so it can be pulled out
question
The question to ask the user
The question to ask the user
options
the options to display
the options to display
default
The default value for this question. Assumed to exist within the given options.
The default value for this question . Assumed to exist within the given options .
first
<not_specific>
opts
:select_ui (true) Enable long-form option selection
: select_ui ( true ) Enable long - form option selection
see
None