Paginator

Build Status

This crate is used for generating pagination bar on webpages or other UIs.

Examples

Creating a Pagination Bar Which Has 5 Pages and Is on Page 1

```rust extern crate paginator;

use paginator::{Paginator, PageItem};

use core::fmt::Write;

let paginator = Paginator::builder(5).currentpage(1).buildpaginator().unwrap();

let mut html = String::new();

for pageitem in paginator.paginate() { match pageitem { PageItem::Prev(page) => { html.writefmt(formatargs!("

  • ", page = page)).unwrap(); } PageItem::Page(page) => { html.writefmt(formatargs!("
  • {page}
  • ", page = page)).unwrap(); } PageItem::CurrentPage(page) => { html.writefmt(formatargs!("
  • {page}
  • ", page = page)).unwrap(); } PageItem::Ignore => { html.pushstr("
  • ...
  • "); } PageItem::Next(page) => { html.write
    fmt(format_args!("
  • ", page = page)).unwrap(); } } } ```

    Creating Pagination Bars Which Has 2 Pages for Different Current Pages

    ```rust extern crate paginator;

    use paginator::{Paginator, PageItem};

    let mut paginatoriter = Paginator::builder(2).buildpaginator_iter().unwrap();

    for pageitem in paginatoriter.next().unwrap().paginate() { // current_page == 1 }

    for pageitem in paginatoriter.next().unwrap().paginate() { // current_page == 2 } ```

    Pagination Rules

    Before building up a Paginator, there is an important option, max_item_count, can be set via PaginatorBuilder. This option can limit the count of items on the pagination bar. This crate ignores page items far away from the current page item to stick to the count limit of items. The first/last n items can be additionally reserved.

    Look at the following code for more details.

    ```rust extern crate paginator;

    use paginator::{Paginator, pageitemsto_string};

    let mut p = Paginator::builder(8).maxitemcount(9).startsize(1).endsize(1).buildpaginatoriter().unwrap();

    asserteq!("1* 2 3 4 5 6 7 8 >", pageitemstostring(p.next().unwrap().paginate().asslice())); asserteq!("< 1 2* 3 4 5 ... 8 >", pageitemstostring(p.next().unwrap().paginate().asslice())); asserteq!("< 1 2 3* 4 5 ... 8 >", pageitemstostring(p.next().unwrap().paginate().asslice())); asserteq!("< 1 2 3 4* 5 ... 8 >", pageitemstostring(p.next().unwrap().paginate().asslice())); asserteq!("< 1 ... 4 5* 6 7 8 >", pageitemstostring(p.next().unwrap().paginate().asslice())); asserteq!("< 1 ... 4 5 6* 7 8 >", pageitemstostring(p.next().unwrap().paginate().asslice())); asserteq!("< 1 ... 4 5 6 7* 8 >", pageitemstostring(p.next().unwrap().paginate().asslice())); asserteq!("< 1 2 3 4 5 6 7 8*", pageitemstostring(p.next().unwrap().paginate().asslice())); ```

    No Std

    Disable the default features to compile this crate without std.

    toml [dependencies.paginator] version = "*" default-features = false

    Crates.io

    https://crates.io/crates/paginator

    Documentation

    https://docs.rs/paginator

    License

    MIT