Class: TTY2::Prompt::MultiList Private

Inherits:
List
  • Object
show all
Defined in:
lib/tty2/prompt/multi_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 multi select list menu. Used by TTY2::Prompt to display interactive choice menu.

Constant Summary

Constants inherited from List

List::FILTER_KEYS_MATCHER, List::INTEGER_MATCHER

Instance Method Summary collapse

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 TTY2::Prompt::MultiList menu.

Parameters:

  • :prompt (Prompt)
  • options (Hash)


19
20
21
22
23
24
25
26
# File 'lib/tty2/prompt/multi_list.rb', line 19

def initialize(prompt, **options)
  super
  @selected = SelectedChoices.new
  @help = options[:help]
  @echo = options.fetch(:echo, true)
  @min  = options[:min]
  @max  = options[:max]
end

Instance Method Details

#keyctrl_aObject

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/tty2/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_rObject

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/tty2/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

#keyenterObject 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/tty2/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

#keyspaceObject

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/tty2/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/tty2/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/tty2/prompt/multi_list.rb', line 31

def min(value)
  @min = value
end