I can't work out the algorithm for a pager I want to use. I've been struggling with this for a few days now and need some help, I don't even have a good strategy.


There are two groups, start and end. I want to represent missing pages with ... and to supply the minimum no of adjacent pages there should always be (where possible).

the first two and last two pages should always be shown
[n] = current page
... is attached to end, should only appear when there are missing pages

The best way to describe it is by showing expected output


total_pages = 6
adjacent_pages = 1
[1] 2 ... 5 6
1 [2] 3 ... 5 6
1 2 [3] 4 5 6
1 2 3 [4] 5 6
1 2 ... 4 [5] 6
1 2 ... 5 [6]

total_pages = 6
adjacent_pages = 2
[1] 2 3 ... 5 6
1 [2] 3 4 5 6
1 2 [3] 4 5 6
1 2 3 [4] 5 6
1 2 3 4 [5] 6
1 2 ... 4 5 [6]

total_pages = 7
adjacent_pages = 2
[1] 2 3 ... 6 7
1 [2] 3 4 ... 6 7
1 2 [3] 4 5 6 7
1 2 3 [4] 5 6 7
1 2 3 4 [5] 6 7
1 2 ... 4 5 [6] 7
1 2 ... 5 6 [7]


Ideally I would also like to have a middle section as well eg

total_pages = 9
adjacent_pages = 1
1 2 3 [4] 5 ... 8 9 (... attached to end group)
1 2 ... 4 [5] 6 ... 8 9 (first ... attached to middle group)
1 2 ... 5 [6] 7 8 9 (... attached to end group)