-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (66 loc) · 1.52 KB
/
main.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
package main
import (
"fmt"
"log"
"sort"
"sync"
tea "github.com/charmbracelet/bubbletea"
)
func getBranches() (branches []*Branch) {
mutex := sync.Mutex{}
wg := sync.WaitGroup{}
for _, merged := range [2]bool{true, false} {
wg.Add(1)
go func(merged bool) {
defer wg.Done()
result := gitBranch(merged)
mutex.Lock()
branches = append(branches, result...)
mutex.Unlock()
}(merged)
}
wg.Wait()
return
}
func sortBranches(branches []*Branch) {
sort.Slice(branches, func(i, j int) bool {
b1, b2 := branches[i], branches[j]
if b1.Current != b2.Current {
return b1.Current // current branch first
} else if b1.Merged != b2.Merged {
return b1.Merged // merged branches second
}
return b1.Name < b2.Name // sort alphabetically otherwise
})
}
func launchInterface(options []*Branch) *model {
m := initialModel(options)
p := tea.NewProgram(&m)
if err := p.Start(); err != nil {
log.Fatalf("Error: %v", err)
}
return &m
}
func getSelectedBranchNames(model *model) (selected []string) {
for i := range model.selected {
selected = append(selected, model.options[i].Name)
}
return
}
func deleteBranches(selected []string) {
if len(selected) > 0 {
output := gitBranchDelete(selected)
fmt.Printf("%s\nDeleted %d branches", output, len(selected))
} else {
fmt.Println("No branches selected")
}
}
func main() {
parseFlags()
gitFetch()
branches := getBranches()
sortBranches(branches)
model := launchInterface(branches)
selected := getSelectedBranchNames(model)
deleteBranches(selected)
}