Class: TTY2::Prompt::List Private

Inherits:
Object
  • Object
show all
Defined in:
lib/tty2/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 TTY2::Prompt to display interactive menu.

Direct Known Subclasses

MultiList

Constant Summary collapse

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

Allowed keys for filter, along with backspace and canc.

/\A([[:alnum:]]|[[:punct:]])\Z/.freeze
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) ⇒ List

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

Parameters:

  • Hash

    options the configuration options

  • options (Hash)

    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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tty2/prompt/list.rb', line 36

def initialize(prompt, **options)
  check_options_consistency(options)

  @prompt       = prompt
  @prefix       = options.fetch(:prefix) { @prompt.prefix }
  @enum         = options.fetch(:enum) { nil }
  @default      = Array(options[:default])
  @choices      = Choices.new
  @active_color = options.fetch(:active_color) { @prompt.active_color }
  @help_color   = options.fetch(:help_color) { @prompt.help_color }
  @cycle        = options.fetch(:cycle) { false }
  @filterable   = options.fetch(:filter) { false }
  @symbols      = @prompt.symbols.merge(options.fetch(:symbols, {}))
  @quiet        = options.fetch(:quiet) { @prompt.quiet }
  @filter       = []
  @filter_cache = {}
  @help         = options[:help]
  @show_help    = options.fetch(:show_help) { :start }
  @first_render = true
  @done         = false
  @per_page     = options[:per_page]
  @paginator    = Paginator.new
  @block_paginator = BlockPaginator.new
  @by_page      = false
  @paging_changed = false
end

Instance Method Details

#arrows_helpString

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.

Information about arrow keys

Returns:

  • (String)


156
157
158
159
160
161
162
163
164
# File 'lib/tty2/prompt/list.rb', line 156

def arrows_help
  up_down = @symbols[:arrow_up] + "/" + @symbols[:arrow_down]
  left_right = @symbols[:arrow_left] + "/" + @symbols[:arrow_right]

  arrows = [up_down]
  arrows << "/" if paginated?
  arrows << left_right if paginated?
  arrows.join
end

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

Call the list menu by passing question and choices

Parameters:

  • question (String)


239
240
241
242
243
244
245
246
247
# File 'lib/tty2/prompt/list.rb', line 239

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

#choice(*value, &block) ⇒ Object

Add a single choice



201
202
203
204
205
206
207
208
# File 'lib/tty2/prompt/list.rb', line 201

def choice(*value, &block)
  @filter_cache = {}
  if block
    @choices << (value << block)
  else
    @choices << value
  end
end

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

Add multiple choices, or return them.

Parameters:

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

    the values to add as choices; if not passed, the current choices are displayed.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/tty2/prompt/list.rb', line 217

def choices(values = (not_set = true))
  if not_set
    if !filterable? || @filter.empty?
      @choices
    else
      filter_value = @filter.join.downcase
      @filter_cache[filter_value] ||= @choices.enabled.select do |choice|
        choice.name.to_s.downcase.include?(filter_value)
      end
    end
  else
    @filter_cache = {}
    values.each { |val| @choices << val }
  end
end

#default(*default_values) ⇒ Object

Set default option selected



78
79
80
# File 'lib/tty2/prompt/list.rb', line 78

def default(*default_values)
  @default = default_values
end

#default_helpObject

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.

Default help text

Note that enumeration and filter are mutually exclusive



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/tty2/prompt/list.rb', line 171

def default_help
  str = []
  str << "(Press "
  str << "#{arrows_help} arrow"
  str << " or 1-#{choices.size} number" if enumerate?
  str << " to move"
  str << (filterable? ? "," : " and")
  str << " Enter to select"
  str << " and letters to filter" if filterable?
  str << ")"
  str.join
end

#enum(value) ⇒ Object

Set selecting active index using number pad



187
188
189
# File 'lib/tty2/prompt/list.rb', line 187

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)


252
253
254
# File 'lib/tty2/prompt/list.rb', line 252

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)


136
137
138
139
140
# File 'lib/tty2/prompt/list.rb', line 136

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

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

#keybackspaceObject

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.



367
368
369
370
371
372
# File 'lib/tty2/prompt/list.rb', line 367

def keybackspace(*)
  return unless filterable?

  @filter.pop
  @active = 1
end

#keydeleteObject

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.



360
361
362
363
364
365
# File 'lib/tty2/prompt/list.rb', line 360

def keydelete(*)
  return unless filterable?

  @filter.clear
  @active = 1
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.



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/tty2/prompt/list.rb', line 293

def keydown(*)
  searchable  = ((@active + 1)..choices.length)
  next_active = search_choice_in(searchable)

  if next_active
    @active = next_active
  elsif @cycle
    searchable = (1..choices.length)
    next_active = search_choice_in(searchable)

    @active = next_active if next_active
  end
  @paging_changed = @by_page
  @by_page = false
end

#keyenterObject Also known as: keyreturn, keyspace

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.



266
267
268
# File 'lib/tty2/prompt/list.rb', line 266

def keyenter(*)
  @done = true unless choices.empty?
end

#keyleftObject Also known as: keypage_up

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.



338
339
340
341
342
343
344
345
346
347
348
# File 'lib/tty2/prompt/list.rb', line 338

def keyleft(*)
  if (@active - page_size) > 0
    searchable = ((@active - page_size)..choices.size)
    @active = search_choice_in(searchable)
  elsif @cycle
    searchable = choices.size.downto(1).to_a
    @active = search_choice_in(searchable)
  end
  @paging_changed = !@by_page
  @by_page = true
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.



256
257
258
259
260
261
262
263
264
# File 'lib/tty2/prompt/list.rb', line 256

def keynum(event)
  return unless enumerate?

  value = event.value.to_i
  return unless (1..choices.count).cover?(value)
  return if choices[value - 1].disabled?

  @active = value
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.



351
352
353
354
355
356
357
358
# File 'lib/tty2/prompt/list.rb', line 351

def keypress(event)
  return unless filterable?

  if event.value =~ FILTER_KEYS_MATCHER
    @filter << event.value
    @active = 1
  end
end

#keyrightObject Also known as: keypage_down

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.

Moves all choices page by page keeping the current selected item at the same level on each page.

When the choice on a page is outside of next page range then adjust it to the last item, otherwise leave unchanged.



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/tty2/prompt/list.rb', line 315

def keyright(*)
  choices_size = choices.size
  if (@active + page_size) <= choices_size
    searchable = ((@active + page_size)..choices_size)
    @active = search_choice_in(searchable)
  elsif @active <= choices_size # last page shorter
    current   = @active % page_size
    remaining = choices_size % page_size

    if current.zero? || (remaining > 0 && current > remaining)
      searchable = choices_size.downto(0).to_a
      @active = search_choice_in(searchable)
    elsif @cycle
      searchable = ((current.zero? ? page_size : current)..choices_size)
      @active = search_choice_in(searchable)
    end
  end

  @paging_changed = !@by_page
  @by_page = 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.



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/tty2/prompt/list.rb', line 276

def keyup(*)
  searchable  = (@active - 1).downto(1).to_a
  prev_active = search_choice_in(searchable)

  if prev_active
    @active = prev_active
  elsif @cycle
    searchable  = choices.length.downto(1).to_a
    prev_active = search_choice_in(searchable)

    @active = prev_active if prev_active
  end

  @paging_changed = @by_page
  @by_page = false
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.



115
116
117
# File 'lib/tty2/prompt/list.rb', line 115

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)


124
125
126
# File 'lib/tty2/prompt/list.rb', line 124

def paginated?
  choices.size > page_size
end

#paginatorPaginator

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 paginator based on the current navigation key

Returns:



87
88
89
# File 'lib/tty2/prompt/list.rb', line 87

def paginator
  @by_page ? @block_paginator : @paginator
end

#per_page(value) ⇒ Object

Set number of items per page



111
112
113
# File 'lib/tty2/prompt/list.rb', line 111

def per_page(value)
  @per_page = value
end

#quiet(value) ⇒ Object

Set whether selected answers are echoed



194
195
196
# File 'lib/tty2/prompt/list.rb', line 194

def quiet(value)
  @quiet = value
end

#search_choice_in(searchable) ⇒ 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.



272
273
274
# File 'lib/tty2/prompt/list.rb', line 272

def search_choice_in(searchable)
  searchable.find { |i| !choices[i - 1].disabled? }
end

#show_help(value = (not_set = true)) ⇒ Object

Change when help is displayed



145
146
147
148
149
# File 'lib/tty2/prompt/list.rb', line 145

def show_help(value = (not_set = true))
  return @show_ehlp if not_set

  @show_help = 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



69
70
71
72
73
# File 'lib/tty2/prompt/list.rb', line 69

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

  @symbols.merge!(new_symbols)
end

#sync_paginatorsObject

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.

Synchronize paginators start positions



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/tty2/prompt/list.rb', line 94

def sync_paginators
  if @by_page
    if @paginator.start_index
      @block_paginator.reset!
      @block_paginator.start_index = @paginator.start_index
    end
  else
    if @block_paginator.start_index
      @paginator.reset!
      @paginator.start_index = @block_paginator.start_index
    end
  end
end