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)"
INTEGER_MATCHER =

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.

Checks type of default parameter to be integer

/\A[-+]?\d+\Z/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(prompt, **options) ⇒ EnumList

Create instance of EnumList menu.



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

def initialize(prompt, **options)
  @prompt       = prompt
  @prefix       = options.fetch(:prefix) { @prompt.prefix }
  @enum         = options.fetch(:enum) { ")" }
  @default      = options.fetch(:default, nil)
  @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)


147
148
149
150
151
152
153
154
155
# File 'lib/tty/prompt/enum_list.rb', line 147

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



119
120
121
122
123
124
125
# File 'lib/tty/prompt/enum_list.rb', line 119

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



133
134
135
136
137
138
139
# File 'lib/tty/prompt/enum_list.rb', line 133

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



62
63
64
# File 'lib/tty/prompt/enum_list.rb', line 62

def default(default)
  @default = default
end

#default?Boolean

Check if default value is set

Returns:

  • (Boolean)


71
72
73
# File 'lib/tty/prompt/enum_list.rb', line 71

def default?
  !@default.to_s.empty?
end

#enum(value) ⇒ Object

Set selecting active index using number pad



105
106
107
# File 'lib/tty/prompt/enum_list.rb', line 105

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.



193
194
195
196
197
198
199
# File 'lib/tty/prompt/enum_list.rb', line 193

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.



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/tty/prompt/enum_list.rb', line 157

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.



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/tty/prompt/enum_list.rb', line 169

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.



184
185
186
187
188
189
190
# File 'lib/tty/prompt/enum_list.rb', line 184

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



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

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.



82
83
84
# File 'lib/tty/prompt/enum_list.rb', line 82

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)


91
92
93
# File 'lib/tty/prompt/enum_list.rb', line 91

def paginated?
  @choices.size > page_size
end

#per_page(value) ⇒ Object

Set number of items per page



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

def per_page(value)
  @per_page = value
end

#quiet(value) ⇒ Object

Set quiet mode



112
113
114
# File 'lib/tty/prompt/enum_list.rb', line 112

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



53
54
55
56
57
# File 'lib/tty/prompt/enum_list.rb', line 53

def symbols(new_symbols = (not_set = true))
  return @symbols if not_set

  @symbols.merge!(new_symbols)
end