Debian Codegraph @

internal/gui/batchbranchesview.go

raw symbols SCIP
1 package gui
2
3 import (
4 "fmt"
5 "sort"
6 "strings"
7 "unicode"
8
9 "github.com/jroimartin/gocui"
10 )
11
12 // close confirmation view
13 func (gui *Gui) openBatchBranchView(g *gocui.Gui, v *gocui.View) error {
14 if _, err := g.SetViewOnTop(batchBranchViewFeature.Name); err != nil {
15 return err
16 }
17 _ = gui.renderBatchBranches(true)
18 return gui.focusToView(batchBranchViewFeature.Name)
19 }
20
21 // close confirmation view
22 func (gui *Gui) closeBatchBranchesView(g *gocui.Gui, v *gocui.View) error {
23 if gui.order == focus {
24 return nil
25 }
26 if _, err := g.SetViewOnBottom(batchBranchViewFeature.Name); err != nil {
27 return err
28 }
29 return gui.focusToView(mainViewFeature.Name)
30 }
31
32 // updates the renderBatchBranches for given entity
33 func (gui *Gui) renderBatchBranches(calculate bool) error {
34 v, err := gui.g.View(batchBranchViewFeature.Name)
35 if err != nil {
36 return err
37 }
38 v.Clear()
39 if len(gui.State.Repositories) == 0 {
40 return nil
41 }
42 branchMap := make(map[string]int)
43 for _, r := range gui.State.Repositories {
44 for _, b := range r.Branches {
45 branchMap[b.Name] = branchMap[b.Name] + 1
46 }
47 }
48
49 ss := make([]*branchCountMap, 0)
50 for k, v := range branchMap {
51 ss = append(ss, &branchCountMap{k, v})
52 }
53
54 sort.Slice(ss, func(i, j int) bool {
55 if ss[i].Count == ss[j].Count {
56 return lessAlphabetical(ss, i, j)
57 }
58 return ss[i].Count > ss[j].Count
59 })
60 if calculate {
61 gui.State.totalBranches = ss
62 }
63 si := 0
64 if len(gui.State.targetBranch) == 0 {
65 gui.State.targetBranch = ss[0].BranchName
66 }
67 for i, kv := range gui.State.totalBranches {
68 rule := gui.renderRules()
69 n, branch := align(kv.BranchName, rule.MaxBranch, true)
70 branch = branch + strings.Repeat(" ", n)
71 if kv.BranchName == gui.State.targetBranch {
72 si = i
73 fmt.Fprintf(v, "%s%s%s%d\n", ws, green.Sprint(branch), sep, kv.Count)
74 } else {
75 fmt.Fprintf(v, "%s%s%s%d\n", tab, branch, sep, kv.Count)
76 }
77 }
78 _ = adjustAnchor(si, len(gui.State.totalBranches), v)
79 return nil
80 }
81
82 // Less is the interface implementation for Alphabetical sorting function
83 func lessAlphabetical(s []*branchCountMap, i, j int) bool {
84 iRunes := []rune(s[i].BranchName)
85 jRunes := []rune(s[j].BranchName)
86
87 max := len(iRunes)
88 if max > len(jRunes) {
89 max = len(jRunes)
90 }
91
92 for idx := 0; idx < max; idx++ {
93 ir := iRunes[idx]
94 jr := jRunes[idx]
95
96 lir := unicode.ToLower(ir)
97 ljr := unicode.ToLower(jr)
98
99 if lir != ljr {
100 return lir < ljr
101 }
102
103 // the lowercase runes are the same, so compare the original
104 if ir != jr {
105 return ir < jr
106 }
107 }
108 return false
109 }
110
111 // close confirmation view
112 func (gui *Gui) openSuggestBranchView(g *gocui.Gui, v *gocui.View) error {
113 if _, err := g.SetViewOnTop(suggestBranchViewFeature.Name); err != nil {
114 return err
115 }
116 return gui.focusToView(suggestBranchViewFeature.Name)
117 }
118
119 // close confirmation view
120 func (gui *Gui) closeSuggestBranchesView(g *gocui.Gui, v *gocui.View) error {
121 if gui.order == focus {
122 return nil
123 }
124 if _, err := g.SetViewOnBottom(suggestBranchViewFeature.Name); err != nil {
125 return err
126 }
127 _ = gui.renderBatchBranches(false)
128 return gui.focusToView(batchBranchViewFeature.Name)
129 }
130
131 // close confirmation view
132 func (gui *Gui) closeSuggestBranchesViewWithAdd(g *gocui.Gui, v *gocui.View) error {
133 newBranch := strings.TrimSpace(v.ViewBuffer())
134 if len(newBranch) <= 0 {
135 return gui.closeSuggestBranchesView(g, v)
136 }
137 nm := &branchCountMap{BranchName: newBranch}
138 gui.State.totalBranches = append(gui.State.totalBranches, nm)
139 gui.State.targetBranch = nm.BranchName
140 _ = gui.renderBatchBranches(false)
141 return gui.closeSuggestBranchesView(g, v)
142 }