Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // You can edit this code!
- // Click here and start typing.
- package main
- import (
- "fmt"
- "math"
- )
- func pager(items []int, current_page int, page_size int, padding int) string {
- var out string
- num_items := len(items)
- num_pages := int(math.Ceil(float64(num_items)/float64(page_size)))
- in_buffer := false
- for page := 1; page <= num_pages; page++ {
- if page == current_page {
- out+=" *"+fmt.Sprintf("%d",page)+"*"
- } else {
- // Always show the first and last pages
- if page == 1 ||
- page == num_pages ||
- // Show the current page and [padding] pages to either side
- (page >= current_page - padding && page <= current_page + padding) ||
- // These two conditions will show a page number that exceeds the padding
- // if the padding is only a single number anyways
- // e.g. 1 2 3 4 *5* instead of 1 ... 3 4 *5*
- (page == 2 && current_page - padding - 1 == 2) ||
- (page == num_pages - 1 && current_page + padding + 1 == num_pages - 1) {
- out+=" "+fmt.Sprintf("%d",page)
- in_buffer = false
- } else if !in_buffer {
- in_buffer = true
- out+=" ..."
- }
- }
- }
- return out
- }
- func main() {
- for i := 1; i <= 34; i++ {
- fmt.Println(pager(make([]int, 100, 100), i, 3, 2))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement