# Paginate the results
#
# This methods keeps only a subset of `results` depending on the current `page` and the
# number of elements to return set by `limit`.
#
# The `count` can be specified when `results` does not contain all the results.
# For example when the results are already limited from a DB statement.
fun paginate do
var results = self.results
if results == null then return
var limit = self.limit
if limit == null then return
var page = self.page
if page == null or page <= 0 then page = 1
var count = self.count
if count == null then count = results.length
var max = count / limit
if max == 0 then
page = 1
max = 1
else if page > max then
page = max
end
var lstart = (page - 1) * limit
var lend = limit
if lstart + lend > count then lend = count - lstart
self.results = results.subarray(lstart, lend)
self.max = max
self.limit = limit
self.page = page
self.count = count
end
src/doc/commands/commands_base.nit:269,2--305,4