aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2025-02-06 23:32:18 +0100
committerGabriel A. Giovanini <mail@gabrielgio.me>2025-02-06 23:32:18 +0100
commite8265117372cf262ce7cb4f6ce3b61f19373aee6 (patch)
treeeee905d4e0697cf3cb057d9d703e26825941ec83 /pkg/handler
parent77f5ad1047dc2ae72cd616dc2acc839ea38d881f (diff)
downloadcerrado-e8265117372cf262ce7cb4f6ce3b61f19373aee6.tar.gz
cerrado-e8265117372cf262ce7cb4f6ce3b61f19373aee6.tar.bz2
cerrado-e8265117372cf262ce7cb4f6ce3b61f19373aee6.zip
feat: Add configuration for ordering repo list
Now it is possible to order the list by; unordered, alphabetical and last commit both ascendent and descendent.
Diffstat (limited to 'pkg/handler')
-rw-r--r--pkg/handler/git/handler.go28
1 files changed, 27 insertions, 1 deletions
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go
index 436d364..9c7ba5b 100644
--- a/pkg/handler/git/handler.go
+++ b/pkg/handler/git/handler.go
@@ -9,8 +9,10 @@ import (
"net/http"
"os"
"path/filepath"
+ "sort"
"strings"
+ "git.gabrielgio.me/cerrado/pkg/config"
"git.gabrielgio.me/cerrado/pkg/ext"
"git.gabrielgio.me/cerrado/pkg/service"
"git.gabrielgio.me/cerrado/pkg/u"
@@ -34,6 +36,7 @@ type (
configurationRepository interface {
GetRootReadme() string
GetSyntaxHighlight() string
+ GetOrderBy() config.OrderBy
}
)
@@ -79,7 +82,7 @@ func (g *GitHandler) List(w http.ResponseWriter, r *http.Request) error {
bs = markdown.Render(doc, renderer)
gitList := &templates.GitListPage{
- Respositories: repos,
+ Respositories: orderBy(repos, g.config.GetOrderBy()),
About: bs,
}
templates.WritePageTemplate(w, gitList, r.Context())
@@ -390,3 +393,26 @@ func GetLexers(filename string) chroma.Lexer {
func isPublic(r *service.Repository) bool {
return r.Public
}
+
+func orderBy(repos []*service.Repository, order config.OrderBy) []*service.Repository {
+ switch order {
+ case config.AlphabeticalAsc:
+ sort.Slice(repos, func(i, j int) bool {
+ return repos[i].Name < repos[j].Name
+ })
+ case config.AlphabeticalDesc:
+ sort.Slice(repos, func(i, j int) bool {
+ return repos[i].Name > repos[j].Name
+ })
+ case config.LastCommitAsc:
+ sort.Slice(repos, func(i, j int) bool {
+ return repos[i].LastCommit.Committer.When.Before(repos[j].LastCommit.Committer.When)
+ })
+ case config.LastCommitDesc:
+ sort.Slice(repos, func(i, j int) bool {
+ return repos[i].LastCommit.Committer.When.After(repos[j].LastCommit.Committer.When)
+ })
+ }
+
+ return repos
+}