class TTY::Prompt::EnumList

A class reponsible for rendering enumerated list menu. Used by {Prompt} to display static choice menu.

@api private

Constants

PAGE_HELP

Public Class Methods

new(prompt, options = {}) click to toggle source

Create instance of EnumList menu.

@api public

# File lib/tty/prompt/enum_list.rb, line 19
def initialize(prompt, options = {})
  @prompt       = prompt
  @prefix       = options.fetch(:prefix) { @prompt.prefix }
  @enum         = options.fetch(:enum) { ')' }
  @default      = options.fetch(:default) { 1 }
  @active_color = options.fetch(:active_color) { @prompt.active_color }
  @help_color   = options.fetch(:help_color)   { @prompt.help_color }
  @error_color  = options.fetch(:error_color)  { @prompt.error_color }
  @input        = nil
  @done         = false
  @first_render = true
  @failure      = false
  @active       = @default
  @choices      = Choices.new
  @per_page     = options[:per_page]
  @page_help    = options[:page_help] || PAGE_HELP
  @paginator    = EnumPaginator.new
  @page_active  = @default

  @prompt.subscribe(self)
end

Public Instance Methods

call(question, possibilities, &block) click to toggle source

Call the list menu by passing question and choices

@param [String] question

@param @api public

# File lib/tty/prompt/enum_list.rb, line 109
def call(question, possibilities, &block)
  choices(possibilities)
  @question = question
  block[self] if block
  setup_defaults
  render
end
choice(*value, &block) click to toggle source

Add a single choice

@api public

# File lib/tty/prompt/enum_list.rb, line 85
def choice(*value, &block)
  if block
    @choices << (value << block)
  else
    @choices << value
  end
end
choices(values) click to toggle source

Add multiple choices

@param [Array] values

the values to add as choices

@api public

# File lib/tty/prompt/enum_list.rb, line 99
def choices(values)
  values.each { |val| choice(*val) }
end
default(default) click to toggle source

Set default option selected

@api public

# File lib/tty/prompt/enum_list.rb, line 44
def default(default)
  @default = default
end
enum(value) click to toggle source

Set selecting active index using number pad

@api public

# File lib/tty/prompt/enum_list.rb, line 78
def enum(value)
  @enum = value
end
keyenter(*)
Alias for: keyreturn
keyleft(*) click to toggle source
# File lib/tty/prompt/enum_list.rb, line 148
def keyleft(*)
  if (@page_active - page_size) >= 0
    @page_active -= page_size
  else
    @page_active = @choices.size - 1
  end
end
keypress(event) click to toggle source
# File lib/tty/prompt/enum_list.rb, line 117
def keypress(event)
  if [:backspace, :delete].include?(event.key.name)
    return if @input.empty?
    @input.chop!
    mark_choice_as_active
  elsif event.value =~ /^\d+$/
    @input += event.value
    mark_choice_as_active
  end
end
keyreturn(*) click to toggle source
# File lib/tty/prompt/enum_list.rb, line 128
def keyreturn(*)
  @failure = false
  if (@input.to_i > 0 && @input.to_i <= @choices.size) || @input.empty?
    @done = true
  else
    @input = ''
    @failure = true
  end
end
Also aliased as: keyenter
keyright(*) click to toggle source
# File lib/tty/prompt/enum_list.rb, line 139
def keyright(*)
  if (@page_active + page_size) <= @choices.size
    @page_active += page_size
  else
    @page_active = 1
  end
end
Also aliased as: keytab
keytab(*)
Alias for: keyright
page_help(text) click to toggle source

@param [String] text

the help text to display per page

@api pbulic

# File lib/tty/prompt/enum_list.rb, line 71
def page_help(text)
  @page_help = text
end
page_size() click to toggle source
# File lib/tty/prompt/enum_list.rb, line 55
def page_size
  (@per_page || Paginator::DEFAULT_PAGE_SIZE)
end
paginated?() click to toggle source

Check if list is paginated

@return [Boolean]

@api private

# File lib/tty/prompt/enum_list.rb, line 64
def paginated?
  @choices.size > page_size
end
per_page(value) click to toggle source

Set number of items per page

@api public

# File lib/tty/prompt/enum_list.rb, line 51
def per_page(value)
  @per_page = value
end

Private Instance Methods

answer() click to toggle source

Find value for the choice selected

@return [nil, Object]

@api private

# File lib/tty/prompt/enum_list.rb, line 218
def answer
  @choices[@active - 1].value
end
error_message() click to toggle source

Error message when incorrect index chosen

@api private

# File lib/tty/prompt/enum_list.rb, line 252
def error_message
  error = 'Please enter a valid number'
  "\n" + @prompt.decorate('>>', @error_color) + ' ' + error
end
mark_choice_as_active() click to toggle source

Find active choice or set to default

@return [nil]

@api private

# File lib/tty/prompt/enum_list.rb, line 163
def mark_choice_as_active
  if (@input.to_i > 0) && !@choices[@input.to_i - 1].nil?
    @active = @input.to_i
  else
    @active = @default
  end
  @page_active = @active
end
page_help_message() click to toggle source

Pagination help message

@return [String]

@api private

# File lib/tty/prompt/enum_list.rb, line 297
def page_help_message
  return '' unless paginated?
  "\n" + @prompt.decorate(@page_help, @help_color)
end
refresh(lines) click to toggle source

Determine area of the screen to clear

@param [Integer] lines

the lines to clear

@return [String]

@api private

# File lib/tty/prompt/enum_list.rb, line 230
def refresh(lines)
  @prompt.clear_lines(lines) +
    @prompt.cursor.clear_screen_down
end
render() click to toggle source

Render a selection list.

By default the result is printed out.

@return [Object] value

return the selected value

@api private

# File lib/tty/prompt/enum_list.rb, line 197
def render
  @input = ''
  until @done
    question = render_question
    @prompt.print(question)
    @prompt.print(render_error) if @failure
    if paginated? && !@done
      @prompt.print(render_page_help)
    end
    @prompt.read_keypress
    @prompt.print(refresh(question.lines.count))
  end
  @prompt.print(render_question)
  answer
end
render_error() click to toggle source

Render error message and return cursor to position of input

@return [String]

@api private

# File lib/tty/prompt/enum_list.rb, line 262
def render_error
  error = error_message.dup
  if !paginated?
    error << @prompt.cursor.prev_line
    error << @prompt.cursor.forward(render_footer.size)
  end
  error
end
render_header() click to toggle source

Render chosen option

@return [String]

@api private

# File lib/tty/prompt/enum_list.rb, line 276
def render_header
  return '' unless @done
  return '' unless @active
  selected_item = @choices[@active - 1].name.to_s
  @prompt.decorate(selected_item, @active_color)
end
render_menu() click to toggle source

Render menu with indexed choices to select from

@return [String]

@api private

# File lib/tty/prompt/enum_list.rb, line 321
def render_menu
  output = ''
  @paginator.paginate(@choices, @page_active, @per_page) do |choice, index|
    num = (index + 1).to_s + @enum + ' '
    selected = ' ' * 2 + num + choice.name
    output << if index + 1 == @active
                @prompt.decorate(selected.to_s, @active_color)
              else
                selected
              end
    output << "\n"
  end
  output
end
render_page_help() click to toggle source

Render page help

@return [String]

@api private

# File lib/tty/prompt/enum_list.rb, line 307
def render_page_help
  help = page_help_message.dup
  if @failure
    help << @prompt.cursor.prev_line
  end
  help << @prompt.cursor.prev_line
  help << @prompt.cursor.forward(render_footer.size)
end
render_question() click to toggle source

Render question with the menu options

@return [String]

@api private

# File lib/tty/prompt/enum_list.rb, line 240
def render_question
  header = "#{@prefix}#{@question} #{render_header}\n"
  unless @done
    header << render_menu
    header << render_footer
  end
  header
end
setup_defaults() click to toggle source

Setup default option and active selection

@api private

# File lib/tty/prompt/enum_list.rb, line 184
def setup_defaults
  validate_defaults
  mark_choice_as_active
end
validate_defaults() click to toggle source

Validate default indexes to be within range

@api private

# File lib/tty/prompt/enum_list.rb, line 175
def validate_defaults
  return if @default >= 1 && @default <= @choices.size
  raise PromptConfigurationError,
        "default index `#{d}` out of range (1 - #{@choices.size})"
end