This repository has no description
1package repo
2
3import (
4 "maps"
5 "slices"
6 "sort"
7 "strings"
8
9 "tangled.org/core/types"
10)
11
12func sortFiles(files []types.NiceTree) {
13 sort.Slice(files, func(i, j int) bool {
14 iIsFile := files[i].IsFile()
15 jIsFile := files[j].IsFile()
16 if iIsFile != jIsFile {
17 return !iIsFile
18 }
19 return files[i].Name < files[j].Name
20 })
21}
22
23func sortBranches(branches []types.Branch) {
24 slices.SortFunc(branches, func(a, b types.Branch) int {
25 if a.IsDefault {
26 return -1
27 }
28 if b.IsDefault {
29 return 1
30 }
31 if a.Commit != nil && b.Commit != nil {
32 if a.Commit.Committer.When.Before(b.Commit.Committer.When) {
33 return 1
34 } else {
35 return -1
36 }
37 }
38 return strings.Compare(a.Name, b.Name)
39 })
40}
41
42func uniqueEmails(commits []types.Commit) []string {
43 emails := make(map[string]struct{})
44 for _, commit := range commits {
45 emails[commit.Author.Email] = struct{}{}
46 emails[commit.Committer.Email] = struct{}{}
47 for _, c := range commit.CoAuthors() {
48 emails[c.Email] = struct{}{}
49 }
50 }
51
52 // delete empty emails if any, from the set
53 delete(emails, "")
54
55 return slices.Collect(maps.Keys(emails))
56}
57
58func balanceIndexItems(commitCount, branchCount, tagCount, fileCount int) (commitsTrunc int, branchesTrunc int, tagsTrunc int) {
59 if commitCount == 0 && tagCount == 0 && branchCount == 0 {
60 return
61 }
62
63 // typically 1 item on right side = 2 files in height
64 availableSpace := fileCount / 2
65
66 // clamp tagcount
67 if tagCount > 0 {
68 tagsTrunc = 1
69 availableSpace -= 1 // an extra subtracted for headers etc.
70 }
71
72 // clamp branchcount
73 if branchCount > 0 {
74 branchesTrunc = min(max(branchCount, 1), 3)
75 availableSpace -= branchesTrunc // an extra subtracted for headers etc.
76 }
77
78 // show
79 if commitCount > 0 {
80 commitsTrunc = max(availableSpace, 3)
81 }
82
83 return
84}