Class: TTY::Prompt::EnumList Private

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/prompt/enum_list.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 reponsible for rendering enumerated list menu. Used by TTY::Prompt to display static choice menu.

Constant Summary collapse

PAGE_HELP =

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.

"(Press tab/right or left to reveal more choices)"

Instance Method Summary collapse

Constructor Details

#initialize(prompt, **options) ⇒ EnumList

Create instance of EnumList menu.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tty/prompt/enum_list.rb', line 21

def initialize(prompt, **options)
  @prompt       = prompt
  @prefix       = options.fetch(:prefix) { @prompt.prefix }
  @enum         = options.fetch(:enum) { ")" }
  @default      = options.fetch(:default) { -1 }
  @active_color = options.fetch(:active_color) { @prompt.active_color }
  @help_color   = options.fetch(:help_color)   { @prompt.help_color }
  @error_color  = options.fetch(:error_color)  { @prompt.error_color }
  @cycle        = options.fetch(:cycle) { false }
  @quiet        = options.fetch(:quiet) { @prompt.quiet }
  @symbols      = @prompt.symbols.merge(options.fetch(:symbols, {}))
  @input        = nil
  @done         = false
  @first_render = true
  @failure      = false
  @active       = @default
  @choices      = Choices.new
  @per_page     = options[:per_page]
  @page_help    = options[:page_help] || PAGE_HELP
  @paginator    = BlockPaginator.new
  @page_active  = @default
end

Instance Method Details

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

Call the list menu by passing question and choices

Parameters:

  • question (String)

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

def call(question, possibilities, &block)
  choices(possibilities)
  @question = question
  block[self] if block
  setup_defaults
  @prompt.subscribe(self) do
    render
  end
end

#choice(*value, &block) ⇒ Object

Add a single choice


115
116
117
118
119
120
121
# File 'lib/tty/prompt/enum_list.rb', line 115

def choice(*value, &block)
  if block
    @choices << (value << block)
  else
    @choices << value
  end
end

#choices(values = (not_set = true)) ⇒ Object

Add multiple choices

Parameters:

  • values (Array[Object]) (defaults to: (not_set = true))

    the values to add as choices


129
130
131
132
133
134
135
# File 'lib/tty/prompt/enum_list.rb', line 129

def choices(values = (not_set = true))
  if not_set
    @choices
  else
    values.each { |val| @choices << val }
  end
end

#default(default) ⇒ Object

Set default option selected


58
59
60
# File 'lib/tty/prompt/enum_list.rb', line 58

def default(default)
  @default = default
end

#default?Boolean

Check if default value is set

Returns:

  • (Boolean)

67
68
69
# File 'lib/tty/prompt/enum_list.rb', line 67

def default?
  @default > 0
end

#enum(value) ⇒ Object

Set selecting active index using number pad


101
102
103
# File 'lib/tty/prompt/enum_list.rb', line 101

def enum(value)
  @enum = value
end

#keyleftObject

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.


188
189
190
191
192
193
194
# File 'lib/tty/prompt/enum_list.rb', line 188

def keyleft(*)
  if (@page_active - page_size) >= 0
    @page_active -= page_size
  elsif @cycle
    @page_active = @choices.size - 1
  end
end

#keypress(event) ⇒ 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.


153
154
155
156
157
158
159
160
161
162
# File 'lib/tty/prompt/enum_list.rb', line 153

def keypress(event)
  if %i[backspace delete].include?(event.key.name)
    return if @input.empty?
    @input.chop!
    mark_choice_as_active
  elsif event.value =~ /^\d+$/
    @input += event.value
    mark_choice_as_active
  end
end

#keyreturnObject Also known as: keyenter

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.


164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/tty/prompt/enum_list.rb', line 164

def keyreturn(*)
  @failure = false
  num = @input.to_i
  choice_disabled = choices[num - 1] && choices[num - 1].disabled?
  choice_in_range = num > 0 && num <= @choices.size

  if choice_in_range && !choice_disabled || @input.empty?
    @done = true
  else
    @input = ""
    @failure = true
  end
end

#keyrightObject Also known as: keytab

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.


179
180
181
182
183
184
185
# File 'lib/tty/prompt/enum_list.rb', line 179

def keyright(*)
  if (@page_active + page_size) <= @choices.size
    @page_active += page_size
  elsif @cycle
    @page_active = 1
  end
end

#page_help(text) ⇒ Object

Parameters:

  • text (String)

    the help text to display per page


94
95
96
# File 'lib/tty/prompt/enum_list.rb', line 94

def page_help(text)
  @page_help = text
end

#page_sizeObject

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.


78
79
80
# File 'lib/tty/prompt/enum_list.rb', line 78

def page_size
  (@per_page || Paginator::DEFAULT_PAGE_SIZE)
end

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

Check if list is paginated

Returns:

  • (Boolean)

87
88
89
# File 'lib/tty/prompt/enum_list.rb', line 87

def paginated?
  @choices.size > page_size
end

#per_page(value) ⇒ Object

Set number of items per page


74
75
76
# File 'lib/tty/prompt/enum_list.rb', line 74

def per_page(value)
  @per_page = value
end

#quiet(value) ⇒ Object

Set quiet mode


108
109
110
# File 'lib/tty/prompt/enum_list.rb', line 108

def quiet(value)
  @quiet = value
end

#symbols(new_symbols = (not_set = true)) ⇒ Object

Change symbols used by this prompt

Parameters:

  • new_symbols (Hash) (defaults to: (not_set = true))

    the new symbols to use


50
51
52
53
# File 'lib/tty/prompt/enum_list.rb', line 50

def symbols(new_symbols = (not_set = true))
  return @symbols if not_set
  @symbols.merge!(new_symbols)
end