Class: TTY::Prompt::Paginator

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

Direct Known Subclasses

EnumPaginator

Constant Summary collapse

DEFAULT_PAGE_SIZE =
6

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Paginator

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.

Create a Paginator



11
12
13
14
15
# File 'lib/tty/prompt/paginator.rb', line 11

def initialize(options = {})
  @last_index  = Array(options[:default]).flatten.first || 0
  @per_page    = options[:per_page]
  @lower_index = Array(options[:default]).flatten.first
end

Instance Method Details

#max_indexInteger

Maximum index for current pagination

Returns:

  • (Integer)

Raises:

  • (ArgumentError)


22
23
24
25
# File 'lib/tty/prompt/paginator.rb', line 22

def max_index
  raise ArgumentError, 'no max index' unless @per_page
  @lower_index + @per_page - 1
end

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

Paginate collection given an active index

Parameters:

  • list (Array[Choice])

    a collection of choice items

  • active (Integer)

    current choice active index

  • per_page (Integer) (defaults to: nil)

    number of choice items per page

Returns:

  • (Enumerable)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tty/prompt/paginator.rb', line 39

def paginate(list, active, per_page = nil, &block)
  current_index = active - 1
  default_size = (list.size <= DEFAULT_PAGE_SIZE ? list.size : DEFAULT_PAGE_SIZE)
  @per_page = @per_page || per_page || default_size
  @lower_index ||= current_index
  @upper_index ||= max_index

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

  if current_index > @last_index # going up
    if current_index > @upper_index && current_index < list.size - 1
      @lower_index += 1
    end
  elsif current_index < @last_index # going down
    if current_index < @lower_index && current_index > 0
      @lower_index -= 1
    end
  end

  # Cycle list
  if current_index.zero?
    @lower_index = 0
  elsif current_index == list.size - 1
    @lower_index = list.size - 1 - (@per_page - 1)
  end

  @upper_index = @lower_index + (@per_page - 1)
  @last_index = current_index

  sliced_list = list[@lower_index..@upper_index]
  indices = (@lower_index..@upper_index)

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

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