-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
branch.go
92 lines (80 loc) · 2.48 KB
/
branch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"sort"
"strings"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/gitty/vcs"
"github.com/muesli/reflow/truncate"
)
func printBranch(branch vcs.Branch, stat *trackStat, maxWidth int) {
genericStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color(theme.colorGray))
numberStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color(theme.colorBlue)).Width(maxWidth)
authorStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color(theme.colorBlue))
timeStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color(theme.colorGreen)).Width(8).Align(lipgloss.Right)
titleStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color(theme.colorDarkGray)).Width(70 - maxWidth)
var s string
s += numberStyle.Render(branch.Name)
s += genericStyle.Render(" ")
s += stat.Render()
s += genericStyle.Render(" ")
s += titleStyle.Render(truncate.StringWithTail(branch.LastCommit.MessageHeadline, uint(70-maxWidth), "…"))
s += genericStyle.Render(" ")
s += timeStyle.Render(ago(branch.LastCommit.CommittedAt))
s += genericStyle.Render(" ")
s += authorStyle.Render(branch.LastCommit.Author)
fmt.Println(s)
}
func printBranches(branches []vcs.Branch, stats map[string]*trackStat) {
headerStyle := lipgloss.NewStyle().
PaddingTop(1).
Foreground(lipgloss.Color(theme.colorMagenta))
// trimmed := false
if *maxBranches > 0 && len(branches) > *maxBranches {
branches = branches[:*maxBranches]
// trimmed = true
}
fmt.Println(headerStyle.Render(fmt.Sprintf("%s %s", "🌳", pluralize(len(branches), "active branch", "active branches"))))
// detect max width of branch name
var maxWidth int
for _, v := range branches {
if len(v.Name) > maxWidth {
maxWidth = len(v.Name)
}
}
for _, v := range branches {
stat, ok := stats[v.Name]
if !ok {
stat = nil
}
printBranch(v, stat, maxWidth)
}
// if trimmed {
// fmt.Println("...")
// }
}
func filterBranches(branches []vcs.Branch) []vcs.Branch {
sort.Slice(branches, func(i, j int) bool {
if branches[i].LastCommit.CommittedAt.Equal(branches[j].LastCommit.CommittedAt) {
return strings.Compare(branches[i].Name, branches[j].Name) < 0
}
return branches[i].LastCommit.CommittedAt.After(branches[j].LastCommit.CommittedAt)
})
// filter list
var b []vcs.Branch //nolint
for _, v := range branches {
if *maxBranchAge > 0 &&
v.LastCommit.CommittedAt.Before(time.Now().Add(-24*time.Duration(*maxBranchAge)*time.Hour)) {
continue
}
b = append(b, v)
}
branches = b
return branches
}