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 18 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 } @cycle = options.fetch(:cycle) { false } @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
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 elsif @cycle @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 elsif @cycle @page_active = 1 end end
Also aliased as: keytab
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