Class: TTY::Prompt::EnumList Private
- Inherits:
-
Object
- Object
- TTY::Prompt::EnumList
- Defined in:
- lib/tty/prompt/enum_list.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A class reponsible for rendering enumerated list menu. Used by TTY::Prompt to display static choice menu.
Constant Summary collapse
- PAGE_HELP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'(Press tab/right or left to reveal more choices)'.freeze
Instance Method Summary collapse
-
#call(question, possibilities, &block) ⇒ Object
Call the list menu by passing question and choices.
-
#choice(*value, &block) ⇒ Object
Add a single choice.
-
#choices(values) ⇒ Object
Add multiple choices.
-
#default(default) ⇒ Object
Set default option selected.
-
#enum(value) ⇒ Object
Set selecting active index using number pad.
-
#initialize(prompt, options = {}) ⇒ EnumList
constructor
Create instance of EnumList menu.
- #keyleft ⇒ Object private
- #keypress(event) ⇒ Object private
- #keyreturn ⇒ Object (also: #keyenter) private
- #keyright ⇒ Object (also: #keytab) private
- #page_help(text) ⇒ Object
- #page_size ⇒ Object private
-
#paginated? ⇒ Boolean
private
Check if list is paginated.
-
#per_page(value) ⇒ Object
Set number of items per page.
Constructor Details
#initialize(prompt, options = {}) ⇒ EnumList
Create instance of EnumList menu.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tty/prompt/enum_list.rb', line 19 def initialize(prompt, = {}) @prompt = prompt @prefix = .fetch(:prefix) { @prompt.prefix } @enum = .fetch(:enum) { ')' } @default = .fetch(:default) { 1 } @active_color = .fetch(:active_color) { @prompt.active_color } @help_color = .fetch(:help_color) { @prompt.help_color } @error_color = .fetch(:error_color) { @prompt.error_color } @input = nil @done = false @first_render = true @failure = false @active = @default @choices = Choices.new @per_page = [:per_page] @page_help = [:page_help] || PAGE_HELP @paginator = EnumPaginator.new @page_active = @default @prompt.subscribe(self) end |
Instance Method Details
#call(question, possibilities, &block) ⇒ Object
Call the list menu by passing question and choices
109 110 111 112 113 114 115 |
# 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) ⇒ Object
Add a single choice
85 86 87 88 89 90 91 |
# 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) ⇒ Object
Add multiple choices
99 100 101 |
# File 'lib/tty/prompt/enum_list.rb', line 99 def choices(values) values.each { |val| choice(*val) } end |
#default(default) ⇒ Object
Set default option selected
44 45 46 |
# File 'lib/tty/prompt/enum_list.rb', line 44 def default(default) @default = default end |
#enum(value) ⇒ Object
Set selecting active index using number pad
78 79 80 |
# File 'lib/tty/prompt/enum_list.rb', line 78 def enum(value) @enum = value end |
#keyleft ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
148 149 150 151 152 153 154 |
# 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) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
117 118 119 120 121 122 123 124 125 126 |
# 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 ⇒ Object Also known as: keyenter
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
128 129 130 131 132 133 134 135 136 |
# 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 |
#keyright ⇒ Object Also known as: keytab
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
139 140 141 142 143 144 145 |
# 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 |
#page_help(text) ⇒ Object
71 72 73 |
# File 'lib/tty/prompt/enum_list.rb', line 71 def page_help(text) @page_help = text end |
#page_size ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
55 56 57 |
# File 'lib/tty/prompt/enum_list.rb', line 55 def page_size (@per_page || Paginator::DEFAULT_PAGE_SIZE) end |
#paginated? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if list is paginated
64 65 66 |
# File 'lib/tty/prompt/enum_list.rb', line 64 def paginated? @choices.size > page_size end |
#per_page(value) ⇒ Object
Set number of items per page
51 52 53 |
# File 'lib/tty/prompt/enum_list.rb', line 51 def per_page(value) @per_page = value end |