File: batchbranchesview.go

package info (click to toggle)
gitbatch 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 644 kB
  • sloc: makefile: 5; sh: 1
file content (142 lines) | stat: -rw-r--r-- 3,559 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package gui

import (
	"fmt"
	"sort"
	"strings"
	"unicode"

	"github.com/jroimartin/gocui"
)

// close confirmation view
func (gui *Gui) openBatchBranchView(g *gocui.Gui, v *gocui.View) error {
	if _, err := g.SetViewOnTop(batchBranchViewFeature.Name); err != nil {
		return err
	}
	_ = gui.renderBatchBranches(true)
	return gui.focusToView(batchBranchViewFeature.Name)
}

// close confirmation view
func (gui *Gui) closeBatchBranchesView(g *gocui.Gui, v *gocui.View) error {
	if gui.order == focus {
		return nil
	}
	if _, err := g.SetViewOnBottom(batchBranchViewFeature.Name); err != nil {
		return err
	}
	return gui.focusToView(mainViewFeature.Name)
}

// updates the renderBatchBranches for given entity
func (gui *Gui) renderBatchBranches(calculate bool) error {
	v, err := gui.g.View(batchBranchViewFeature.Name)
	if err != nil {
		return err
	}
	v.Clear()
	if len(gui.State.Repositories) == 0 {
		return nil
	}
	branchMap := make(map[string]int)
	for _, r := range gui.State.Repositories {
		for _, b := range r.Branches {
			branchMap[b.Name] = branchMap[b.Name] + 1
		}
	}

	ss := make([]*branchCountMap, 0)
	for k, v := range branchMap {
		ss = append(ss, &branchCountMap{k, v})
	}

	sort.Slice(ss, func(i, j int) bool {
		if ss[i].Count == ss[j].Count {
			return lessAlphabetical(ss, i, j)
		}
		return ss[i].Count > ss[j].Count
	})
	if calculate {
		gui.State.totalBranches = ss
	}
	si := 0
	if len(gui.State.targetBranch) == 0 {
		gui.State.targetBranch = ss[0].BranchName
	}
	for i, kv := range gui.State.totalBranches {
		rule := gui.renderRules()
		n, branch := align(kv.BranchName, rule.MaxBranch, true)
		branch = branch + strings.Repeat(" ", n)
		if kv.BranchName == gui.State.targetBranch {
			si = i
			fmt.Fprintf(v, "%s%s%s%d\n", ws, green.Sprint(branch), sep, kv.Count)
		} else {
			fmt.Fprintf(v, "%s%s%s%d\n", tab, branch, sep, kv.Count)
		}
	}
	_ = adjustAnchor(si, len(gui.State.totalBranches), v)
	return nil
}

// Less is the interface implementation for Alphabetical sorting function
func lessAlphabetical(s []*branchCountMap, i, j int) bool {
	iRunes := []rune(s[i].BranchName)
	jRunes := []rune(s[j].BranchName)

	max := len(iRunes)
	if max > len(jRunes) {
		max = len(jRunes)
	}

	for idx := 0; idx < max; idx++ {
		ir := iRunes[idx]
		jr := jRunes[idx]

		lir := unicode.ToLower(ir)
		ljr := unicode.ToLower(jr)

		if lir != ljr {
			return lir < ljr
		}

		// the lowercase runes are the same, so compare the original
		if ir != jr {
			return ir < jr
		}
	}
	return false
}

// close confirmation view
func (gui *Gui) openSuggestBranchView(g *gocui.Gui, v *gocui.View) error {
	if _, err := g.SetViewOnTop(suggestBranchViewFeature.Name); err != nil {
		return err
	}
	return gui.focusToView(suggestBranchViewFeature.Name)
}

// close confirmation view
func (gui *Gui) closeSuggestBranchesView(g *gocui.Gui, v *gocui.View) error {
	if gui.order == focus {
		return nil
	}
	if _, err := g.SetViewOnBottom(suggestBranchViewFeature.Name); err != nil {
		return err
	}
	_ = gui.renderBatchBranches(false)
	return gui.focusToView(batchBranchViewFeature.Name)
}

// close confirmation view
func (gui *Gui) closeSuggestBranchesViewWithAdd(g *gocui.Gui, v *gocui.View) error {
	newBranch := strings.TrimSpace(v.ViewBuffer())
	if len(newBranch) <= 0 {
		return gui.closeSuggestBranchesView(g, v)
	}
	nm := &branchCountMap{BranchName: newBranch}
	gui.State.totalBranches = append(gui.State.totalBranches, nm)
	gui.State.targetBranch = nm.BranchName
	_ = gui.renderBatchBranches(false)
	return gui.closeSuggestBranchesView(g, v)
}