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
}

Instance Method Summary collapse

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, 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

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(message, possibilities, &block)
  choices(possibilities)
  @message = 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

Parameters:

  • values (Array[Object])

    the values to add as 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.

Returns:

  • (Boolean)


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

#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.



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

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)


37
38
39
# File 'lib/tty/prompt/expander.rb', line 37

def expanded?
  @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'
    expand
    @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

Returns:



93
94
95
# File 'lib/tty/prompt/expander.rb', line 93

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