Class: TTY::Prompt::MultiList Private
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 multi select list menu. Used by TTY::Prompt to display interactive choice menu.
Constant Summary
Constants inherited from List
List::FILTER_KEYS_MATCHER, List::INTEGER_MATCHER
Instance Method Summary collapse
-
#initialize(prompt, **options) ⇒ MultiList
constructor
Create instance of TTY::Prompt::MultiList menu.
-
#keyctrl_a ⇒ Object
private
Selects all choices when Ctrl+A is pressed.
-
#keyctrl_r ⇒ Object
private
Revert currently selected choices when Ctrl+I is pressed.
-
#keyenter ⇒ Object
(also: #keyreturn)
private
Callback fired when enter/return key is pressed.
-
#keyspace ⇒ Object
private
Callback fired when space key is pressed.
-
#max(value) ⇒ Object
Set a maximum number of choices.
-
#min(value) ⇒ Object
Set a minimum number of choices.
Methods inherited from List
#arrows_help, #call, #choice, #choices, #default, #enum, #enumerate?, #help, #keybackspace, #keydelete, #keydown, #keyleft, #keynum, #keypress, #keyright, #keyup, #page_size, #paginated?, #paginator, #per_page, #quiet, #search_choice_in, #show_help, #symbols, #sync_paginators
Constructor Details
#initialize(prompt, **options) ⇒ MultiList
Create instance of TTY::Prompt::MultiList menu.
19 20 21 22 23 24 25 26 |
# File 'lib/tty/prompt/multi_list.rb', line 19 def initialize(prompt, **) super @selected = SelectedChoices.new @help = [:help] @echo = .fetch(:echo, true) @min = [:min] @max = [:max] end |
Instance Method Details
#keyctrl_a ⇒ 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.
Selects all choices when Ctrl+A is pressed
71 72 73 74 75 |
# File 'lib/tty/prompt/multi_list.rb', line 71 def keyctrl_a(*) return if @max && @max < choices.size @selected = SelectedChoices.new(choices.enabled, choices.enabled_indexes) end |
#keyctrl_r ⇒ 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.
Revert currently selected choices when Ctrl+I is pressed
80 81 82 83 84 85 86 87 88 |
# File 'lib/tty/prompt/multi_list.rb', line 80 def keyctrl_r(*) return if @max && @max < choices.size indexes = choices.each_with_index.reduce([]) do |acc, (choice, idx)| acc << idx if !choice.disabled? && !@selected.include?(choice) acc end @selected = SelectedChoices.new(choices.enabled - @selected.to_a, indexes) end |
#keyenter ⇒ Object Also known as: keyreturn
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.
Callback fired when enter/return key is pressed
45 46 47 48 49 50 51 |
# File 'lib/tty/prompt/multi_list.rb', line 45 def keyenter(*) valid = true valid = @min <= @selected.size if @min valid = @selected.size <= @max if @max super if valid 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.
Callback fired when space key is pressed
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tty/prompt/multi_list.rb', line 57 def keyspace(*) active_choice = choices[@active - 1] if @selected.include?(active_choice) @selected.delete_at(@active - 1) else return if @max && @selected.size >= @max @selected.insert(@active - 1, active_choice) end end |
#max(value) ⇒ Object
Set a maximum number of choices
38 39 40 |
# File 'lib/tty/prompt/multi_list.rb', line 38 def max(value) @max = value end |
#min(value) ⇒ Object
Set a minimum number of choices
31 32 33 |
# File 'lib/tty/prompt/multi_list.rb', line 31 def min(value) @min = value end |