Class: Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/inquirer/utils/paginator.rb

Instance Method Summary collapse

Constructor Details

#initializePaginator

Returns a new instance of Paginator.



5
6
7
8
9
# File 'lib/inquirer/utils/paginator.rb', line 5

def initialize
  @pointer    = 0
  @last_index = 0
  @page_size  = 7
end

Instance Method Details

#paginate(content, current_position) ⇒ Object



11
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
# File 'lib/inquirer/utils/paginator.rb', line 11

def paginate(content, current_position)

  lines_list = content.lines

  # Make sure there's enough lines to paginate
  return content if lines_list.count < (@page_size + 2)

  promt_question = lines_list.shift
  lines_count    = lines_list.count
  infinite       = lines_list * 3

  # Move the pos only when the user go down and limit it to 3
  if @pointer < 3 && @last_index < current_position && current_position - @last_index < 9
    @pointer = [3, @pointer + current_position - @last_index].min
  end
  @last_index = current_position

  top_index = [0, current_position + lines_count - @pointer].max
  section   = infinite.slice(top_index, @page_size).join

  [
    promt_question,
    section,
    Inquirer::Style.pagiator_text + IOChar.newline
  ].join(IOChar.newline)
end