class TTY::Prompt::Expander
A class responsible for rendering expanding options Used by {Prompt} to display key options question.
@api private
Constants
- HELP_CHOICE
Public Class Methods
new(prompt, options = {})
click to toggle source
Create instance of Expander
@api public
# File lib/tty/prompt/expander.rb, line 20 def initialize(prompt, options = {}) @prompt = prompt @prefix = options.fetch(:prefix) { @prompt.prefix } @default = options.fetch(:default) { 1 } @active_color = options.fetch(:active_color) { @prompt.active_color } @help_color = options.fetch(:help_color) { @prompt.help_color } @choices = Choices.new @selected = nil @done = false @status = :collapsed @hint = nil @default_key = false @prompt.subscribe(self) end
Public Instance Methods
call(message, possibilities, &block)
click to toggle source
Execute this prompt
@api public
# File lib/tty/prompt/expander.rb, line 128 def call(message, possibilities, &block) choices(possibilities) @message = message block.call(self) if block setup_defaults choice(HELP_CHOICE) render end
choice(value, &block)
click to toggle source
Add a single choice
@api public
# File lib/tty/prompt/expander.rb, line 107 def choice(value, &block) if block @choices << value.update(value: block) else @choices << value end end
choices(values)
click to toggle source
Add multiple choices
@param [Array] values
the values to add as choices
@api public
# File lib/tty/prompt/expander.rb, line 121 def choices(values) values.each { |val| choice(val) } end
collapsed?()
click to toggle source
# File lib/tty/prompt/expander.rb, line 40 def collapsed? @status == :collapsed end
default(value = (not_set = true))
click to toggle source
Set default value.
@api public
# File lib/tty/prompt/expander.rb, line 99 def default(value = (not_set = true)) return @default if not_set @default = value end
expand()
click to toggle source
# File lib/tty/prompt/expander.rb, line 44 def expand @status = :expanded end
expanded?()
click to toggle source
# File lib/tty/prompt/expander.rb, line 36 def expanded? @status == :expanded end
keyenter(_)
click to toggle source
Respond to submit event
@api public
# File lib/tty/prompt/expander.rb, line 51 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 else @input = '' end end
Also aliased as: keyreturn
keypress(event)
click to toggle source
Respond to key press event
@api public
# File lib/tty/prompt/expander.rb, line 75 def keypress(event) if [:backspace, :delete].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(key)
click to toggle source
Select choice by given key
@return [Choice]
@api private
# File lib/tty/prompt/expander.rb, line 92 def select_choice(key) @choices.find_by(:key, key) end