Class: TTY::Prompt::List Private
- Inherits:
-
Object
- Object
- TTY::Prompt::List
- Includes:
- Symbols
- Defined in:
- lib/tty/prompt/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 responsible for rendering select list menu Used by TTY::Prompt to display interactive menu.
Direct Known Subclasses
Constant Summary collapse
- 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.
'(Use arrow%s keys, press Enter to select)'
- 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.
'(Move up or down to reveal more choices)'
Constants included from Symbols
Symbols::KEYS, Symbols::WIN_KEYS
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_values) ⇒ Object
Set default option selected.
-
#default_help ⇒ Object
Default help text.
-
#enum(value) ⇒ Object
Set selecting active index using number pad.
-
#enumerate? ⇒ Boolean
private
Check if list is enumerated.
-
#help(value = (not_set = true)) ⇒ String
Provide help information.
-
#initialize(prompt, options = {}) ⇒ List
constructor
Create instance of TTY::Prompt::List menu.
- #keydown ⇒ Object (also: #keytab) private
- #keynum(event) ⇒ Object private
- #keyreturn ⇒ Object private
- #keyspace ⇒ Object private
- #keyup ⇒ Object private
-
#marker(value) ⇒ Object
Set marker.
- #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.
Methods included from Symbols
Constructor Details
#initialize(prompt, options = {}) ⇒ List
Create instance of TTY::Prompt::List menu.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/tty/prompt/list.rb', line 34 def initialize(prompt, = {}) @prompt = prompt @prefix = .fetch(:prefix) { @prompt.prefix } @enum = .fetch(:enum) { nil } @default = Array[.fetch(:default) { 1 }] @active = @default.first @choices = Choices.new @active_color = .fetch(:active_color) { @prompt.active_color } @help_color = .fetch(:help_color) { @prompt.help_color } @marker = .fetch(:marker) { symbols[:pointer] } @help = [:help] @first_render = true @done = false @per_page = [:per_page] @page_help = [:page_help] || PAGE_HELP @paginator = Paginator.new @prompt.subscribe(self) end |
Instance Method Details
#call(question, possibilities, &block) ⇒ Object
Call the list menu by passing question and choices
150 151 152 153 154 155 156 |
# File 'lib/tty/prompt/list.rb', line 150 def call(question, possibilities, &block) choices(possibilities) @question = question block.call(self) if block setup_defaults render end |
#choice(*value, &block) ⇒ Object
Add a single choice
126 127 128 129 130 131 132 |
# File 'lib/tty/prompt/list.rb', line 126 def choice(*value, &block) if block @choices << (value << block) else @choices << value end end |
#choices(values) ⇒ Object
Add multiple choices
140 141 142 |
# File 'lib/tty/prompt/list.rb', line 140 def choices(values) Array(values).each { |val| choice(*val) } end |
#default(*default_values) ⇒ Object
Set default option selected
64 65 66 |
# File 'lib/tty/prompt/list.rb', line 64 def default(*default_values) @default = default_values end |
#default_help ⇒ Object
Default help text
112 113 114 |
# File 'lib/tty/prompt/list.rb', line 112 def default_help self.class::HELP % [enumerate? ? " or number (1-#{@choices.size})" : ''] end |
#enum(value) ⇒ Object
Set selecting active index using number pad
119 120 121 |
# File 'lib/tty/prompt/list.rb', line 119 def enum(value) @enum = value end |
#enumerate? ⇒ 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 enumerated
161 162 163 |
# File 'lib/tty/prompt/list.rb', line 161 def enumerate? !@enum.nil? end |
#help(value = (not_set = true)) ⇒ String
Provide help information
103 104 105 106 107 |
# File 'lib/tty/prompt/list.rb', line 103 def help(value = (not_set = true)) return @help if !@help.nil? && not_set @help = (@help.nil? && !not_set) ? value : default_help end |
#keydown ⇒ 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.
184 185 186 |
# File 'lib/tty/prompt/list.rb', line 184 def keydown(*) @active = (@active == @choices.length) ? 1 : @active + 1 end |
#keynum(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.
165 166 167 168 169 170 |
# File 'lib/tty/prompt/list.rb', line 165 def keynum(event) return unless enumerate? value = event.value.to_i return unless (1..@choices.count).cover?(value) @active = value end |
#keyreturn ⇒ 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.
176 177 178 |
# File 'lib/tty/prompt/list.rb', line 176 def keyreturn(*) @done = true end |
#keyspace ⇒ 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.
172 173 174 |
# File 'lib/tty/prompt/list.rb', line 172 def keyspace(*) @done = true end |
#keyup ⇒ 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.
180 181 182 |
# File 'lib/tty/prompt/list.rb', line 180 def keyup(*) @active = (@active == 1) ? @choices.length : @active - 1 end |
#marker(value) ⇒ Object
Set marker
57 58 59 |
# File 'lib/tty/prompt/list.rb', line 57 def marker(value) @marker = value end |
#page_help(text) ⇒ Object
91 92 93 |
# File 'lib/tty/prompt/list.rb', line 91 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.
75 76 77 |
# File 'lib/tty/prompt/list.rb', line 75 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
84 85 86 |
# File 'lib/tty/prompt/list.rb', line 84 def paginated? @choices.size > page_size end |
#per_page(value) ⇒ Object
Set number of items per page
71 72 73 |
# File 'lib/tty/prompt/list.rb', line 71 def per_page(value) @per_page = value end |