Class: TTY::Prompt::Paginator
- Inherits:
-
Object
- Object
- TTY::Prompt::Paginator
- Defined in:
- lib/tty/prompt/paginator.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_PAGE_SIZE =
6
Instance Attribute Summary collapse
-
#current_index ⇒ Object
readonly
The 0-based index of the active item on this page.
-
#end_index ⇒ Object
readonly
The 0-based index of the last item on this page.
-
#last_index ⇒ Object
readonly
The 0-based index of the previously active item on this page.
-
#start_index ⇒ Object
The 0-based index of the first item on this page.
Instance Method Summary collapse
-
#check_page_size! ⇒ Object
private
Check if page size is valid.
-
#initialize(**options) ⇒ Paginator
constructor
private
Create a Paginator.
-
#paginate(list, active, per_page = nil, &block) ⇒ Enumerable
Paginate collection given an active index.
-
#reset! ⇒ Object
private
Reset current page indexes.
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
23 24 25 26 27 |
# File 'lib/tty/prompt/paginator.rb', line 23 def initialize(**) @last_index = Array([:default]).flatten.first || 0 @per_page = [:per_page] @start_index = Array([:default]).flatten.first end |
Instance Attribute Details
#current_index ⇒ Object (readonly)
The 0-based index of the active item on this page
15 16 17 |
# File 'lib/tty/prompt/paginator.rb', line 15 def current_index @current_index end |
#end_index ⇒ Object (readonly)
The 0-based index of the last item on this page
12 13 14 |
# File 'lib/tty/prompt/paginator.rb', line 12 def end_index @end_index end |
#last_index ⇒ Object (readonly)
The 0-based index of the previously active item on this page
18 19 20 |
# File 'lib/tty/prompt/paginator.rb', line 18 def last_index @last_index end |
#start_index ⇒ Object
The 0-based index of the first item on this page
9 10 11 |
# File 'lib/tty/prompt/paginator.rb', line 9 def start_index @start_index end |
Instance Method Details
#check_page_size! ⇒ 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.
Check if page size is valid
42 43 44 |
# File 'lib/tty/prompt/paginator.rb', line 42 def check_page_size! raise InvalidArgument, "per_page must be > 0" if @per_page < 1 end |
#paginate(list, active, per_page = nil, &block) ⇒ Enumerable
Paginate collection given an active index
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/tty/prompt/paginator.rb', line 59 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 check_page_size! @start_index ||= (current_index / @per_page) * @per_page @end_index ||= @start_index + @per_page - 1 # 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 step = (current_index - @last_index).abs if current_index > @last_index # going up if current_index >= @end_index && current_index < list.size - 1 last_page = list.size - @per_page @start_index = [@start_index + step, last_page].min end elsif current_index < @last_index # going down if current_index <= @start_index && current_index > 0 @start_index = [@start_index - step, 0].max end end # Cycle list if current_index.zero? @start_index = 0 elsif current_index == list.size - 1 @start_index = list.size - 1 - (@per_page - 1) end @end_index = @start_index + (@per_page - 1) @last_index = current_index 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 |
#reset! ⇒ 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.
Reset current page indexes
32 33 34 35 |
# File 'lib/tty/prompt/paginator.rb', line 32 def reset! @start_index = nil @end_index = nil end |