Class: TTY::Prompt::BlockPaginator

Inherits:
Paginator
  • Object
show all
Defined in:
lib/tty/prompt/block_paginator.rb

Constant Summary

Constants inherited from Paginator

Paginator::DEFAULT_PAGE_SIZE

Instance Attribute Summary

Attributes inherited from Paginator

#current_index, #end_index, #last_index, #start_index

Instance Method Summary collapse

Methods inherited from Paginator

#check_page_size!, #initialize, #reset!

Constructor Details

This class inherits a constructor from TTY::Prompt::Paginator

Instance Method Details

#paginate(list, active, per_page = nil, &block) ⇒ Object

Paginate list of choices based on current active choice. Move entire pages.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tty/prompt/block_paginator.rb', line 12

def paginate(list, active, per_page = nil, &block)
  default_size = (list.size <= DEFAULT_PAGE_SIZE ? list.size : DEFAULT_PAGE_SIZE)
  @per_page = @per_page || per_page || default_size

  check_page_size!

  # Don't paginate short lists
  if list.size <= @per_page
    @start_index = 0
    @end_index = list.size - 1
    if block
      return list.each_with_index(&block)
    else
      return list.each_with_index.to_enum
    end
  end

  unless active.nil? # User may input index out of range
    @last_index = active
  end
  page  = (@last_index / @per_page.to_f).ceil
  pages = (list.size / @per_page.to_f).ceil
  if page == 0
    @start_index = 0
    @end_index = @start_index + @per_page - 1
  elsif page > 0 && page < pages
    @start_index = (page - 1) * @per_page
    @end_index = @start_index + @per_page - 1
  elsif page == pages
    @start_index = (page - 1) * @per_page
    @end_index = list.size - 1
  else
    @end_index = list.size - 1
    @start_index = @end_index - @per_page + 1
  end

  sliced_list = list[@start_index..@end_index]
  page_range = (@start_index..@end_index)

  return sliced_list.zip(page_range).to_enum unless block_given?

  sliced_list.each_with_index do |item, index|
    block[item, @start_index + index]
  end
end