tinyrelayfile
tinyrelay/tinyclient/repo_navigation.go/

tinyclient/repo_navigation.go raw
1package tinyclient
2
3import (
4 "fmt"
5 "net/url"
6 "strings"
7 "time"
8)
9
10type repoCrumb struct{ Label, URL string }
11
12func repoURL(query url.Values, view, path string) string {
13 values := url.Values{"owner": {query.Get("owner")}, "repo": {query.Get("repo")}, "ref": {query.Get("ref")}, "view": {view}}
14 if path != "" {
15 if view == "issue" || view == "pr" {
16 values.Set("id", path)
17 } else {
18 values.Set("path", path)
19 }
20 }
21 return "/repo?" + values.Encode()
22}
23
24func repoCommitURL(query url.Values, oid string) string {
25 values := url.Values{"owner": {query.Get("owner")}, "repo": {query.Get("repo")}, "ref": {oid}}
26 return repoURL(values, "commit", "")
27}
28
29func hasNextOffset(value any) bool {
30 switch offset := value.(type) {
31 case int:
32 return offset >= 0
33 case float64:
34 return offset >= 0
35 default:
36 return false
37 }
38}
39
40func repoBreadcrumbs(query url.Values) []repoCrumb {
41 crumbs := []repoCrumb{{Label: query.Get("repo"), URL: repoURL(query, "tree", "")}}
42 if query.Get("path") == "" {
43 return crumbs
44 }
45 parts := strings.Split(query.Get("path"), "/")
46 for index, label := range parts {
47 crumbs = append(crumbs, repoCrumb{Label: label, URL: repoURL(query, "tree", strings.Join(parts[:index+1], "/"))})
48 }
49 crumbs[len(crumbs)-1].URL = ""
50 return crumbs
51}
52
53func browsePageURL(route string, query url.Values, field string, value any) string {
54 values := make(url.Values, len(query)+1)
55 for name, items := range query {
56 values[name] = append([]string(nil), items...)
57 }
58 values.Set(field, fmt.Sprint(value))
59 return route + "?" + values.Encode()
60}
61
62func shortID(value any) string {
63 text, ok := value.(string)
64 if !ok {
65 return ""
66 }
67 if len(text) > 12 {
68 return text[:12]
69 }
70 return text
71}
72
73func repoDate(value any) string {
74 var seconds int64
75 switch value := value.(type) {
76 case float64:
77 seconds = int64(value)
78 case int64:
79 seconds = value
80 }
81 if seconds == 0 {
82 return ""
83 }
84 return time.Unix(seconds, 0).UTC().Format("Jan. 2, 2006")
85}

About

ownerc01503e8035f
visibilitypublic
headd730679639bf

Maintainers

  • c01503e8035f owner
  • ba1b5beed5b5 agent hermes

Clone

  • https://012.run/npub1cq2s86qrtadf36eqq3epm38wmhdynrrcwxf84eldk3f94dkv7ttsrulrz5/tinyrelay.git

Refs