Class: TTY::Prompt::Expander Private

Inherits:
Object
  • Object
show all
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
}.freeze
DELETE_KEYS =

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.

Names for delete keys

%i[backspace delete].freeze

Instance Method Summary collapse

Constructor Details

#initialize(prompt, options = {}) ⇒ Expander

Create instance of Expander



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tty/prompt/expander.rb', line 24

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

Instance Method Details

#call(message, possibilities, &block) ⇒ Object

Execute this prompt



142
143
144
145
146
147
148
149
150
151
# File 'lib/tty/prompt/expander.rb', line 142

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

#choice(value, &block) ⇒ Object

Add a single choice



121
122
123
124
125
126
127
# File 'lib/tty/prompt/expander.rb', line 121

def choice(value, &block)
  if block
    @choices << value.update(value: block)
  else
    @choices << value
  end
end

#choices(values) ⇒ Object

Add multiple choices

Parameters:

  • values (Array[Object])

    the values to add as choices



135
136
137
# File 'lib/tty/prompt/expander.rb', line 135

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.

Returns:

  • (Boolean)


44
45
46
# File 'lib/tty/prompt/expander.rb', line 44

def collapsed?
  @status == :collapsed
end

#default(value = (not_set = true)) ⇒ Object

Set default value.



105
106
107
108
109
# File 'lib/tty/prompt/expander.rb', line 105

def default(value = (not_set = true))
  return @default if not_set

  @default = value
end

#expandObject

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.



48
49
50
# File 'lib/tty/prompt/expander.rb', line 48

def expand
  @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.

Returns:

  • (Boolean)


40
41
42
# File 'lib/tty/prompt/expander.rb', line 40

def expanded?
  @status == :expanded
end

#keyenter(_) ⇒ Object Also known as: keyreturn

Respond to submit event



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tty/prompt/expander.rb', line 55

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

#keypress(event) ⇒ Object

Respond to key press event



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tty/prompt/expander.rb', line 80

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

#quiet(value) ⇒ Object

Set quiet mode.



114
115
116
# File 'lib/tty/prompt/expander.rb', line 114

def quiet(value)
  @quiet = value
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

Returns:



98
99
100
# File 'lib/tty/prompt/expander.rb', line 98

def select_choice(key)
  @choices.find_by(:key, key)
end