|
| 1 | +type style = Dots | Numerals |
| 2 | +type t |
| 3 | + |
| 4 | +val set_total_pages : t -> int -> t * int |
| 5 | +(** [set_total_pages paginator total] calculates the total number of pages |
| 6 | +from a given number of items. Its use is optional since this pager can be |
| 7 | +used for other things beyond navigating sets. Note that it both returns the |
| 8 | +number of total pages and alters the model. |
| 9 | +
|
| 10 | + {@ocaml[ |
| 11 | + let paginator = Paginator.make () in |
| 12 | + let total_pages, paginator = Paginator.set_total_pages paginator 3 |
| 13 | + ]} |
| 14 | + *) |
| 15 | + |
| 16 | +val get_slice_bounds : t -> int -> int * int |
| 17 | +(** [get_slice_bounds paginator item_count] returns the start and end bounds of slice you're |
| 18 | + rendering for the current page based on total number of items |
| 19 | +
|
| 20 | + {@ocaml[ |
| 21 | + let bunch_of_stuff = List.to_seq [...] in |
| 22 | + let start, end_pos = Paginator.get_slice_bounds (Seq.length bunch_of_stuff) in |
| 23 | + let slice_to_render = Seq.take (end_pos - start) @@ Seq.drop start bunch_of_stuff |
| 24 | + ]} |
| 25 | + *) |
| 26 | + |
| 27 | +val items_on_page : t -> int -> int |
| 28 | +(** [items_on_page paginator items_count] returns the number of items on the |
| 29 | + current page given the total number of items passed as an argument. |
| 30 | +
|
| 31 | + {@ocaml[ |
| 32 | + let paginator = Paginator.make () in |
| 33 | + let item_count = Paginator.items_on_page paginator 10 |
| 34 | + ]} |
| 35 | + *) |
| 36 | + |
| 37 | +val on_last_page : t -> bool |
| 38 | +(** [on_last_page paginator] returns whether or not we're on the last page. |
| 39 | +
|
| 40 | + {@ocaml[ |
| 41 | + let paginator = Paginator.make () in |
| 42 | + Paginator.on_last_page paginator |
| 43 | + ]} |
| 44 | + *) |
| 45 | + |
| 46 | +val on_first_page : t -> bool |
| 47 | +(** [on_first_page paginator] returns whether or not we're on the first page. |
| 48 | +
|
| 49 | + {@ocaml[ |
| 50 | + let paginator = Paginator.make () in |
| 51 | + Paginator.on_first_page paginator |
| 52 | + ]} |
| 53 | + *) |
| 54 | + |
| 55 | +val prev_page : t -> t |
| 56 | +(** [prev_page paginator] navigates one page backward with a lower bound of 0. |
| 57 | +
|
| 58 | + {@ocaml[ |
| 59 | + let paginator = Paginator.make () in |
| 60 | + let paginator = Paginator.prev_page paginator |
| 61 | + ]} |
| 62 | + *) |
| 63 | + |
| 64 | +val next_page : t -> t |
| 65 | +(** [next_page paginator] navigates one page forward with an upper bound of total_pages - 1. |
| 66 | +
|
| 67 | + {@ocaml[ |
| 68 | + let paginator = Paginator.make () in |
| 69 | + let paginator = Paginator.next_page paginator |
| 70 | + ]} |
| 71 | + *) |
| 72 | + |
| 73 | +val make : |
| 74 | + ?style:style -> |
| 75 | + ?page:int -> |
| 76 | + ?per_page:int -> |
| 77 | + ?total_pages:int -> |
| 78 | + ?active_dot:string -> |
| 79 | + ?inactive_dot:string -> |
| 80 | + ?numerals_format:(int -> int -> string, unit, string) format -> |
| 81 | + ?text_style:Spices.style -> |
| 82 | + unit -> |
| 83 | + t |
| 84 | +(** [make ()] creates a new {Paginator.t} |
| 85 | +
|
| 86 | + A different start page can be specified using `page`. |
| 87 | +
|
| 88 | + For helper functions like `get_slice_bounds` to work correctly, the number of items to be rendered on each page can be passed to `per_page`. |
| 89 | +
|
| 90 | + By default, Paginator uses Numerals format, displaying page 1 of 3 as "1/3". |
| 91 | + The format for numerals can be changed using `numerals_format` to any format that takes two integers, e.g. "%d of %d" rendering "1 of 3" instead. |
| 92 | + Alternatively, `style` can be specified as `Paginator.Dots` to display pages using characters instead with the `active_dot` "•" and inactive_dot "○" defaults, which can be specified/overriden. |
| 93 | +
|
| 94 | + By default, Spices.default is used, which can be overriden using `text_style`. |
| 95 | +
|
| 96 | + {@ocaml[ |
| 97 | + let paginator = Paginator.make () |
| 98 | + ]} |
| 99 | + *) |
| 100 | + |
| 101 | +val update : t -> Minttea.Event.t -> t |
| 102 | +(** [update paginator event] is the Tea update function which binds keystrokes to pagination. |
| 103 | +
|
| 104 | + {@ocaml[ |
| 105 | + let paginator = Paginator.update paginator event |
| 106 | + ]} |
| 107 | + *) |
| 108 | + |
| 109 | +val view : t -> string |
| 110 | +(** [view paginator] renders the pagination to a string. |
| 111 | +
|
| 112 | + {@ocaml[ |
| 113 | + let paginator = Paginator.make () in |
| 114 | + let pagination_string = Paginator.view paginator |
| 115 | + ]} |
| 116 | + *) |
0 commit comments