Class: TTY::Prompt::Expander Private
- Inherits:
-
Object
- Object
- TTY::Prompt::Expander
- Defined in:
- lib/tty/prompt/expander.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 expanding options Used by TTY::Prompt to display key options question.
Constant Summary collapse
- HELP_CHOICE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
{ key: 'h', name: 'print help', value: :help }
Instance Method Summary collapse
-
#call(message, possibilities, &block) ⇒ Object
Execute this prompt.
-
#choice(value, &block) ⇒ Object
Add a single choice.
-
#choices(values) ⇒ Object
Add multiple choices.
- #collapsed? ⇒ Boolean private
-
#default(value = (not_set = true)) ⇒ Object
Set default value.
- #expand ⇒ Object private
- #expanded? ⇒ Boolean private
-
#initialize(prompt, options = {}) ⇒ Expander
constructor
Create instance of Expander.
-
#keyenter(_) ⇒ Object
(also: #keyreturn)
Respond to submit event.
-
#keypress(event) ⇒ Object
Respond to key press event.
-
#select_choice(key) ⇒ Choice
private
Select choice by given key.
Constructor Details
#initialize(prompt, options = {}) ⇒ Expander
Create instance of Expander
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tty/prompt/expander.rb', line 21 def initialize(prompt, = {}) @prompt = prompt @prefix = .fetch(:prefix) { @prompt.prefix } @default = .fetch(:default) { 1 } @active_color = .fetch(:active_color) { @prompt.active_color } @help_color = .fetch(:help_color) { @prompt.help_color } @choices = Choices.new @selected = nil @done = false @status = :collapsed @hint = nil @default_key = false @prompt.subscribe(self) end |
Instance Method Details
#call(message, possibilities, &block) ⇒ Object
Execute this prompt
129 130 131 132 133 134 135 136 |
# File 'lib/tty/prompt/expander.rb', line 129 def call(, possibilities, &block) choices(possibilities) @message = block.call(self) if block setup_defaults choice(HELP_CHOICE) render end |
#choice(value, &block) ⇒ Object
Add a single choice
108 109 110 111 112 113 114 |
# File 'lib/tty/prompt/expander.rb', line 108 def choice(value, &block) if block @choices << value.update(value: block) else @choices << value end end |
#choices(values) ⇒ Object
Add multiple choices
122 123 124 |
# File 'lib/tty/prompt/expander.rb', line 122 def choices(values) values.each { |val| choice(val) } end |
#collapsed? ⇒ Boolean
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.
41 42 43 |
# File 'lib/tty/prompt/expander.rb', line 41 def collapsed? @status == :collapsed end |
#default(value = (not_set = true)) ⇒ Object
Set default value.
100 101 102 103 |
# File 'lib/tty/prompt/expander.rb', line 100 def default(value = (not_set = true)) return @default if not_set @default = value end |
#expand ⇒ 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.
45 46 47 |
# File 'lib/tty/prompt/expander.rb', line 45 def @status = :expanded end |
#expanded? ⇒ Boolean
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.
37 38 39 |
# File 'lib/tty/prompt/expander.rb', line 37 def @status == :expanded end |
#keyenter(_) ⇒ Object Also known as: keyreturn
Respond to submit event
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/tty/prompt/expander.rb', line 52 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' @selected = nil @input = '' elsif selected @done = true @selected = selected else @input = '' end end |
#keypress(event) ⇒ Object
Respond to key press event
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tty/prompt/expander.rb', line 76 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) ⇒ Choice
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.
Select choice by given key
93 94 95 |
# File 'lib/tty/prompt/expander.rb', line 93 def select_choice(key) @choices.find_by(:key, key) end |