SHARE
TWEET

Untitled

a guest Jun 19th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // You can edit this code!
  2. // Click here and start typing.
  3. package main
  4.  
  5. import (
  6.     "fmt"
  7.     "math"
  8. )
  9.  
  10. func pager(items []int, current_page int, page_size int, padding int) string {
  11.     var out string
  12.  
  13.     num_items := len(items)
  14.  
  15.     num_pages := int(math.Ceil(float64(num_items)/float64(page_size)))
  16.  
  17.     in_buffer := false
  18.  
  19.     for page := 1; page <= num_pages; page++ {
  20.         if page == current_page {
  21.             out+=" *"+fmt.Sprintf("%d",page)+"*"
  22.         } else {
  23.             // Always show the first and last pages
  24.             if page == 1 ||
  25.                page == num_pages ||
  26.            
  27.                // Show the current page and [padding] pages to either side
  28.                (page >= current_page - padding && page <= current_page + padding) ||
  29.            
  30.                // These two conditions will show a page number that exceeds the padding
  31.                // if the padding is only a single number anyways
  32.                // e.g. 1 2 3 4 *5* instead of 1 ... 3 4 *5*
  33.                (page == 2 && current_page - padding - 1 == 2) ||
  34.                (page == num_pages - 1 && current_page + padding + 1 == num_pages - 1) {
  35.                 out+=" "+fmt.Sprintf("%d",page)
  36.                 in_buffer = false
  37.             } else if !in_buffer {
  38.                 in_buffer = true
  39.                 out+=" ..."
  40.             }
  41.         }
  42.     }
  43.    
  44.     return out
  45. }
  46.  
  47. func main() {
  48.     for i := 1; i <= 34; i++ {
  49.         fmt.Println(pager(make([]int, 100, 100), i, 3, 2))
  50.     }
  51. }
RAW Paste Data
Top