Class: TTY::Prompt::List Private

Inherits:
Object
  • Object
show all
Includes:
Symbols
Defined in:
lib/tty/prompt/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 responsible for rendering select list menu Used by TTY::Prompt to display interactive menu.

Direct Known Subclasses

MultiList

Constant Summary collapse

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.

'(Use arrow%s keys, press Enter to select)'
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.

'(Move up or down to reveal more choices)'

Constants included from Symbols

Symbols::KEYS, Symbols::WIN_KEYS

Instance Method Summary collapse

Methods included from Symbols

symbols, windows?

Constructor Details

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

Create instance of TTY::Prompt::List menu.

Parameters:

  • Hash

    options the configuration options

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :default (Symbol)

    the default active choice, defaults to 1

  • :color (Symbol)

    the color for the selected item, defualts to :green

  • :marker (Symbol)

    the marker for the selected item

  • :enum (String)

    the delimiter for the item index



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tty/prompt/list.rb', line 34

def initialize(prompt, options = {})
  @prompt       = prompt
  @prefix       = options.fetch(:prefix) { @prompt.prefix }
  @enum         = options.fetch(:enum) { nil }
  @default      = Array[options.fetch(:default) { 1 }]
  @active       = @default.first
  @choices      = Choices.new
  @active_color = options.fetch(:active_color) { @prompt.active_color }
  @help_color   = options.fetch(:help_color) { @prompt.help_color }
  @marker       = options.fetch(:marker) { symbols[:pointer] }
  @help         = options[:help]
  @first_render = true
  @done         = false
  @per_page     = options[:per_page]
  @page_help    = options[:page_help] || PAGE_HELP
  @paginator    = Paginator.new

  @prompt.subscribe(self)
end

Instance Method Details

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

Call the list menu by passing question and choices

Parameters:

  • question (String)


150
151
152
153
154
155
156
# File 'lib/tty/prompt/list.rb', line 150

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

#choice(*value, &block) ⇒ Object

Add a single choice



126
127
128
129
130
131
132
# File 'lib/tty/prompt/list.rb', line 126

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



140
141
142
# File 'lib/tty/prompt/list.rb', line 140

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

#default(*default_values) ⇒ Object

Set default option selected



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

def default(*default_values)
  @default = default_values
end

#default_helpObject

Default help text



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

def default_help
  self.class::HELP % [enumerate? ? " or number (1-#{@choices.size})" : '']
end

#enum(value) ⇒ Object

Set selecting active index using number pad



119
120
121
# File 'lib/tty/prompt/list.rb', line 119

def enum(value)
  @enum = value
end

#enumerate?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 enumerated

Returns:

  • (Boolean)


161
162
163
# File 'lib/tty/prompt/list.rb', line 161

def enumerate?
  !@enum.nil?
end

#help(value = (not_set = true)) ⇒ String

Provide help information

Parameters:

  • value (String) (defaults to: (not_set = true))

    the new help text

Returns:

  • (String)


103
104
105
106
107
# File 'lib/tty/prompt/list.rb', line 103

def help(value = (not_set = true))
  return @help if !@help.nil? && not_set

  @help = (@help.nil? && !not_set) ? value : default_help
end

#keydownObject 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
# File 'lib/tty/prompt/list.rb', line 184

def keydown(*)
  @active = (@active == @choices.length) ? 1 : @active + 1
end

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



165
166
167
168
169
170
# File 'lib/tty/prompt/list.rb', line 165

def keynum(event)
  return unless enumerate?
  value = event.value.to_i
  return unless (1..@choices.count).cover?(value)
  @active = value
end

#keyreturnObject

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.



176
177
178
# File 'lib/tty/prompt/list.rb', line 176

def keyreturn(*)
  @done = true
end

#keyspaceObject

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.



172
173
174
# File 'lib/tty/prompt/list.rb', line 172

def keyspace(*)
  @done = true
end

#keyupObject

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.



180
181
182
# File 'lib/tty/prompt/list.rb', line 180

def keyup(*)
  @active = (@active == 1) ? @choices.length : @active - 1
end

#marker(value) ⇒ Object

Set marker



57
58
59
# File 'lib/tty/prompt/list.rb', line 57

def marker(value)
  @marker = value
end

#page_help(text) ⇒ Object

Parameters:

  • text (String)

    the help text to display per page



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

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.



75
76
77
# File 'lib/tty/prompt/list.rb', line 75

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)


84
85
86
# File 'lib/tty/prompt/list.rb', line 84

def paginated?
  @choices.size > page_size
end

#per_page(value) ⇒ Object

Set number of items per page



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

def per_page(value)
  @per_page = value
end