From 10a3f0927dd8bae263a499563ec2857649d06b0a Mon Sep 17 00:00:00 2001 From: Seednode Date: Sat, 30 Sep 2023 08:51:13 -0500 Subject: [PATCH] Add pagination button to both bottom and top of html info pages when enabled --- cmd/info.go | 112 +++++++++++++++++++++++++++++++--------------------- cmd/root.go | 2 +- 2 files changed, 67 insertions(+), 47 deletions(-) diff --git a/cmd/info.go b/cmd/info.go index 121e5be..c03aefa 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -19,7 +19,64 @@ import ( "seedno.de/seednode/roulette/types" ) -func serveIndexHtml(args []string, index *fileIndex, paginate bool) httprouter.Handle { +func paginate(page int, fileCount int, ending bool) string { + var html strings.Builder + + var firstPage int = 1 + var lastPage int + + if fileCount%PageLength == 0 { + lastPage = fileCount / PageLength + } else { + lastPage = (fileCount / PageLength) + 1 + } + + var prevStatus, nextStatus string = "", "" + + if page <= 1 { + prevStatus = " disabled" + } + + if page >= lastPage { + nextStatus = " disabled" + } + + prevPage := page - 1 + if prevPage < 1 { + prevPage = 1 + } + + nextPage := page + 1 + if nextPage > lastPage { + nextPage = fileCount / PageLength + } + + if ending { + html.WriteString("") + } else { + html.WriteString("") + } + + html.WriteString(fmt.Sprintf("", + firstPage)) + + html.WriteString(fmt.Sprintf("", + prevPage, + prevStatus)) + + html.WriteString(fmt.Sprintf("", + nextPage, + nextStatus)) + + html.WriteString(fmt.Sprintf("", + lastPage)) + + html.WriteString("\n") + + return html.String() +} + +func serveIndexHtml(args []string, index *fileIndex, shouldPaginate bool) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { w.Header().Set("Content-Type", "text/html") @@ -56,8 +113,13 @@ func serveIndexHtml(args []string, index *fileIndex, paginate bool) httprouter.H htmlBody.WriteString(``) htmlBody.WriteString(faviconHtml) htmlBody.WriteString(``) + htmlBody.WriteString(`table,td,tr{border:none;}td{border-bottom:1px solid black;}td{white-space:nowrap;padding:.5em}`) htmlBody.WriteString(fmt.Sprintf("Index contains %d files", fileCount)) + + if shouldPaginate { + htmlBody.WriteString(paginate(page, fileCount, false)) + } + if len(indexDump) > 0 { for _, v := range indexDump[startIndex:stopIndex] { var shouldSort = "" @@ -68,51 +130,9 @@ func serveIndexHtml(args []string, index *fileIndex, paginate bool) httprouter.H htmlBody.WriteString(fmt.Sprintf("\n", Prefix, mediaPrefix, v, shouldSort, v)) } } - if PageLength != 0 { - var firstPage int = 1 - var lastPage int - if fileCount%PageLength == 0 { - lastPage = fileCount / PageLength - } else { - lastPage = (fileCount / PageLength) + 1 - } - - if paginate { - var prevStatus, nextStatus string = "", "" - - if page <= 1 { - prevStatus = " disabled" - } - - if page >= lastPage { - nextStatus = " disabled" - } - - prevPage := page - 1 - if prevPage < 1 { - prevPage = 1 - } - - nextPage := page + 1 - if nextPage > lastPage { - nextPage = fileCount / PageLength - } - - htmlBody.WriteString(fmt.Sprintf("", - firstPage)) - - htmlBody.WriteString(fmt.Sprintf("", - prevPage, - prevStatus)) - - htmlBody.WriteString(fmt.Sprintf("", - nextPage, - nextStatus)) - - htmlBody.WriteString(fmt.Sprintf("", - lastPage)) - } + if shouldPaginate { + htmlBody.WriteString(paginate(page, fileCount, true)) } htmlBody.WriteString(`
%s
`) diff --git a/cmd/root.go b/cmd/root.go index ef63dd6..3221c5e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,7 +12,7 @@ import ( ) const ( - ReleaseVersion string = "2.0.4" + ReleaseVersion string = "2.1.0" ) var (