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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
# frozen_string_literal: true
require_relative "choices"
module TTY
class Prompt
# A class responsible for rendering expanding options
# Used by {Prompt} to display key options question.
#
# @api private
class Expander
HELP_CHOICE = {
key: "h",
name: "print help",
value: :help
}.freeze
# Names for delete keys
DELETE_KEYS = %i[backspace delete].freeze
# Create instance of Expander
#
# @api public
def initialize(prompt, options = {})
@prompt = prompt
@prefix = options.fetch(:prefix) { @prompt.prefix }
@default = options.fetch(:default, 1)
@auto_hint = options.fetch(:auto_hint, false)
@active_color = options.fetch(:active_color) { @prompt.active_color }
@help_color = options.fetch(:help_color) { @prompt.help_color }
@quiet = options.fetch(:quiet) { @prompt.quiet }
@choices = Choices.new
@selected = nil
@done = false
@status = :collapsed
@hint = nil
@default_key = false
end
def expanded?
@status == :expanded
end
def collapsed?
@status == :collapsed
end
def expand
@status = :expanded
end
# Respond to submit event
#
# @api public
def keyenter(_)
if @input.nil? || @input.empty?
@input = @choices[@default - 1].key
@default_key = true
end
selected = select_choice(@input)
if selected && selected.key.to_s == "h"
expand
@selected = nil
@input = ""
elsif selected
@done = true
@selected = selected
@hint = nil
else
@input = ""
end
end
alias keyreturn keyenter
# Respond to key press event
#
# @api public
def keypress(event)
if DELETE_KEYS.include?(event.key.name)
@input.chop! unless @input.empty?
elsif event.value =~ /^[^\e\n\r]/
@input += event.value
end
@selected = select_choice(@input)
if @selected && !@default_key && collapsed?
@hint = @selected.name
end
end
# Select choice by given key
#
# @return [Choice]
#
# @api private
def select_choice(key)
@choices.find_by(:key, key)
end
# Set default value.
#
# @api public
def default(value = (not_set = true))
return @default if not_set
@default = value
end
# Set quiet mode.
#
# @api public
def quiet(value)
@quiet = value
end
# Add a single choice
#
# @api public
def choice(value, &block)
if block
@choices << value.update(value: block)
else
@choices << value
end
end
# Add multiple choices
#
# @param [Array[Object]] values
# the values to add as choices
#
# @api public
def choices(values)
values.each { |val| choice(val) }
end
# Execute this prompt
#
# @api public
def call(message, possibilities, &block)
choices(possibilities)
@message = message
block.call(self) if block
setup_defaults
choice(HELP_CHOICE)
@prompt.subscribe(self) do
render
end
end
private
# Create possible keys with current choice highlighted
#
# @return [String]
#
# @api private
def possible_keys
keys = @choices.pluck(:key)
default_key = keys[@default - 1]
if @selected
index = keys.index(@selected.key)
keys[index] = @prompt.decorate(keys[index], @active_color)
elsif @input.to_s.empty? && default_key
keys[@default - 1] = @prompt.decorate(default_key, @active_color)
end
keys.join(",")
end
# @api private
def render
@input = ""
until @done
question = render_question
@prompt.print(question)
read_input
@prompt.print(refresh(question.lines.count))
end
@prompt.print(render_question) unless @quiet
answer
end
# @api private
def answer
@selected.value
end
# Render message with options
#
# @return [String]
#
# @api private
def render_header
header = ["#{@prefix}#{@message} "]
if @done
selected_item = @selected.name.to_s
header << @prompt.decorate(selected_item, @active_color)
elsif collapsed?
header << %[(enter "h" for help) ]
header << "[#{possible_keys}] "
header << @input
end
header.join
end
# Show hint for selected option key
#
# return [String]
#
# @api private
def render_hint
"\n" + @prompt.decorate(">> ", @active_color) +
@hint +
@prompt.cursor.prev_line +
@prompt.cursor.forward(@prompt.strip(render_header).size)
end
# Render question with menu
#
# @return [String]
#
# @api private
def render_question
load_auto_hint if @auto_hint
header = render_header
header << render_hint if @hint
header << "\n" if @done
if !@done && expanded?
header << render_menu
header << render_footer
end
header
end
def load_auto_hint
if @hint.nil? && collapsed?
if @selected
@hint = @selected.name
else
if @input.empty?
@hint = @choices[@default - 1].name
else
@hint = "invalid option"
end
end
end
end
def render_footer
" Choice [#{@choices[@default - 1].key}]: #{@input}"
end
def read_input
@prompt.read_keypress
end
# Refresh the current input
#
# @param [Integer] lines
#
# @return [String]
#
# @api private
def refresh(lines)
if (@hint && (!@selected || @done)) || (@auto_hint && collapsed?)
@hint = nil
@prompt.clear_lines(lines, :down) +
@prompt.cursor.prev_line
elsif expanded?
@prompt.clear_lines(lines)
else
@prompt.clear_line
end
end
# Render help menu
#
# @api private
def render_menu
output = ["\n"]
@choices.each do |choice|
chosen = %(#{choice.key} - #{choice.name})
if @selected && @selected.key == choice.key
chosen = @prompt.decorate(chosen, @active_color)
end
output << " " + chosen + "\n"
end
output.join
end
def setup_defaults
validate_choices
end
def validate_choices
errors = []
keys = []
@choices.each do |choice|
if choice.key.nil?
errors << "Choice #{choice.name} is missing a :key attribute"
next
end
if choice.key.length != 1
errors << "Choice key `#{choice.key}` is more than one character long."
end
if choice.key.to_s == "h"
errors << "Choice key `#{choice.key}` is reserved for help menu."
end
if keys.include?(choice.key)
errors << "Choice key `#{choice.key}` is a duplicate."
end
keys << choice.key if choice.key
end
errors.each { |err| raise ConfigurationError, err }
end
end # Expander
end # Prompt
end # TTY
|