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)'.freeze

Instance Method Summary collapse

Constructor Details

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

Create instance of EnumList menu.



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

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 }
  @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    = EnumPaginator.new
  @page_active  = @default

  @prompt.subscribe(self)
end

Instance Method Details

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

Call the list menu by passing question and choices

Parameters:

  • question (String)


109
110
111
112
113
114
115
# File 'lib/tty/prompt/enum_list.rb', line 109

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

#choice(*value, &block) ⇒ Object

Add a single choice



85
86
87
88
89
90
91
# File 'lib/tty/prompt/enum_list.rb', line 85

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

#choices(values) ⇒ Object

Add multiple choices

Parameters:

  • values (Array[Object])

    the values to add as choices



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

def choices(values)
  values.each { |val| choice(*val) }
end

#default(default) ⇒ Object

Set default option selected



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

def default(default)
  @default = default
end

#enum(value) ⇒ Object

Set selecting active index using number pad



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

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.



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

def keyleft(*)
  if (@page_active - page_size) >= 0
    @page_active -= page_size
  else
    @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.



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

def keypress(event)
  if [: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.



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

def keyreturn(*)
  @failure = false
  if (@input.to_i > 0 && @input.to_i <= @choices.size) || @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.



139
140
141
142
143
144
145
# File 'lib/tty/prompt/enum_list.rb', line 139

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

#page_help(text) ⇒ Object

Parameters:

  • text (String)

    the help text to display per page



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

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.



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

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)


64
65
66
# File 'lib/tty/prompt/enum_list.rb', line 64

def paginated?
  @choices.size > page_size
end

#per_page(value) ⇒ Object

Set number of items per page



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

def per_page(value)
  @per_page = value
end