1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
# frozen_string_literal: true
require_relative "list"
require_relative "selected_choices"
module TTY
class Prompt
# A class responsible for rendering multi select list menu.
# Used by {Prompt} to display interactive choice menu.
#
# @api private
class MultiList < List
# Create instance of TTY::Prompt::MultiList menu.
#
# @param [Prompt] :prompt
# @param [Hash] options
#
# @api public
def initialize(prompt, **options)
super
@selected = SelectedChoices.new
@help = options[:help]
@echo = options.fetch(:echo, true)
@min = options[:min]
@max = options[:max]
end
# Set a minimum number of choices
#
# @api public
def min(value)
@min = value
end
# Set a maximum number of choices
#
# @api public
def max(value)
@max = value
end
# Callback fired when enter/return key is pressed
#
# @api private
def keyenter(*)
valid = true
valid = @min <= @selected.size if @min
valid = @selected.size <= @max if @max
super if valid
end
alias keyreturn keyenter
# Callback fired when space key is pressed
#
# @api private
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
# Selects all choices when Ctrl+A is pressed
#
# @api private
def keyctrl_a(*)
return if @max && @max < choices.size
@selected = SelectedChoices.new(choices.enabled, choices.enabled_indexes)
end
# Revert currently selected choices when Ctrl+I is pressed
#
# @api private
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
private
# Setup default options and active selection
#
# @api private
def setup_defaults
validate_defaults
# At this stage, @choices matches all the visible choices.
default_indexes = @default.map do |d|
if d.to_s =~ INTEGER_MATCHER
d - 1
else
choices.index(choices.find_by(:name, d.to_s))
end
end
@selected = SelectedChoices.new(@choices.values_at(*default_indexes),
default_indexes)
if @default.empty?
# no default, pick the first non-disabled choice
@active = choices.index { |choice| !choice.disabled? } + 1
elsif @default.last.to_s =~ INTEGER_MATCHER
@active = @default.last
elsif default_choice = choices.find_by(:name, @default.last.to_s)
@active = choices.index(default_choice) + 1
end
end
# Generate selected items names
#
# @return [String]
#
# @api private
def selected_names
@selected.map(&:name).join(", ")
end
# Header part showing the minimum/maximum number of choices
#
# @return [String]
#
# @api private
def minmax_help
help = []
help << "min. #{@min}" if @min
help << "max. #{@max}" if @max
"(%s) " % [help.join(", ")]
end
# Build a default help text
#
# @return [String]
#
# @api private
def default_help
str = []
str << "(Press "
str << "#{arrows_help} arrow"
str << " or 1-#{choices.size} number" if enumerate?
str << " to move, Space"
str << "/Ctrl+A|R" if @max.nil?
str << " to select"
str << " (all|rev)" if @max.nil?
str << (filterable? ? "," : " and")
str << " Enter to finish"
str << " and letters to filter" if filterable?
str << ")"
str.join
end
# Render initial help text and then currently selected choices
#
# @api private
def render_header
instructions = @prompt.decorate(help, @help_color)
minmax_suffix = @min || @max ? minmax_help : ""
print_selected = @selected.size.nonzero? && @echo
if @done && @echo
@prompt.decorate(selected_names, @active_color)
elsif (@first_render && (help_start? || help_always?)) ||
(help_always? && !@filter.any? && !@done)
minmax_suffix +
(print_selected ? "#{selected_names} " : "") +
instructions
elsif filterable? && @filter.any?
minmax_suffix +
(print_selected ? "#{selected_names} " : "") +
@prompt.decorate(filter_help, @help_color)
else
minmax_suffix + (print_selected ? selected_names : "")
end
end
# All values for the choices selected
#
# @return [Array[nil,Object]]
#
# @api private
def answer
@selected.map(&:value)
end
# Render menu with choices to select from
#
# @return [String]
#
# @api private
def render_menu
output = []
sync_paginators if @paging_changed
paginator.paginate(choices, @active, @per_page) do |choice, index|
num = enumerate? ? (index + 1).to_s + @enum + " " : ""
indicator = (index + 1 == @active) ? @symbols[:marker] : " "
indicator += " "
message = if @selected.include?(choice) && !choice.disabled?
selected = @prompt.decorate(@symbols[:radio_on], @active_color)
"#{selected} #{num}#{choice.name}"
elsif choice.disabled?
@prompt.decorate(@symbols[:cross], :red) +
" #{num}#{choice.name} #{choice.disabled}"
else
"#{@symbols[:radio_off]} #{num}#{choice.name}"
end
end_index = paginated? ? paginator.end_index : choices.size - 1
newline = (index == end_index) ? "" : "\n"
output << indicator + message + newline
end
output.join
end
end # MultiList
end # Prompt
end # TTY
|