1 package pagination 2 3 const ( 4 defaultEnd = 3 5 defaultSurrounding = 3 6 ) 7 8 // Config is the configuation for a Pagination 9 type Config struct { 10 Ends, Surrounding uint 11 } 12 13 // New returns a default configuration for Pagination 14 func New() Config { 15 return Config{ 16 Ends: defaultEnd, 17 Surrounding: defaultSurrounding, 18 } 19 } 20 21 // Get returns the Section information for Pagination 22 func (c Config) Get(currPage, lastPage uint) Pagination { 23 if currPage > lastPage { 24 lastPage = currPage 25 } 26 p := Pagination{page: currPage} 27 start := uint(0) 28 for page := uint(0); page <= lastPage; page++ { 29 if !(page < c.Ends || // Beginning 30 page > lastPage-c.Ends || // End 31 ((c.Surrounding > currPage || page >= currPage-c.Surrounding) && page <= currPage+c.Surrounding) || // Middle 32 c.Ends > 0 && ((currPage-c.Surrounding-1 == c.Ends && page == c.Ends) || // Merge Begining and Middle if close enough 33 (currPage+c.Surrounding+1 == lastPage-c.Ends && page == lastPage-c.Ends))) { // Merge Middle and End if close enough 34 if page != start { 35 p.sections[p.numSections] = section{start, page - 1} 36 p.numSections++ 37 } 38 start = page + 1 39 } 40 } 41 if start < lastPage { 42 p.sections[p.numSections] = section{start, lastPage} 43 p.numSections++ 44 } 45 return p 46 }