-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch.go
160 lines (133 loc) · 3.66 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"os"
"os/exec"
"regexp"
"strings"
"github.com/fatih/color"
)
func cmdOutput(command string, args ...string) (string, error) {
cmd := exec.Command(command, args...)
b, err := cmd.CombinedOutput()
return string(b), err
}
func cmdRun(command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
return nil
}
// branch describes a Git branch.
type branch struct {
name string
isCurrent bool
}
// branchesState provides the state for the application, based on CQRS
// Includes view and command methods
type branchesState struct {
branches []branch
selected int
}
func (b *branchesState) init(args []string) error {
args = append([]string{"branch"}, args...)
out, err := cmdOutput("git", args...)
if err != nil {
println(out)
return err
}
b.branches = splitBranches(out)
b.selectCurrent()
return nil
}
func splitBranches(output string) []branch {
names := strings.Split(output, "\n")
var branches []branch
for _, name := range names {
if len(name) == 0 {
continue
}
isCurrent := false
if strings.Contains(name, "*") {
name = strings.Replace(name, "*", "", -1)
isCurrent = true
}
name = strings.TrimSpace(name)
branches = append(branches, branch{name: name, isCurrent: isCurrent})
}
return branches
}
func extractBranch(name string) string {
if strings.Contains(name, "->") {
s := strings.Split(name, "->")
return strings.TrimSpace(s[0])
}
return name
}
// Commands
// These methods allow to mutate state
func (b *branchesState) selectCurrent() {
for ; !b.branches[b.selected].isCurrent; b.selected++ {
}
}
func (b *branchesState) selectNext() {
b.selected = (b.selected + len(b.branches) - 1) % len(b.branches)
}
func (b *branchesState) selectPrevious() {
b.selected = ((b.selected + 1) % len(b.branches))
}
// View
// These methods allow to present the state
func (b *branchesState) selectedBranchName() string {
return b.branches[b.selected].name
}
func (b *branchesState) selectedBranchWithColor() string {
formatted := []string{}
fields := strings.Split(b.selectedBranchName(), "/")
for _, f := range fields {
formatted = append(formatted, withColor(f, colorDefaults))
}
out := strings.Join(formatted, withColor("/", colorDefaults))
if b.branches[b.selected].isCurrent {
out += "\t(" + withColor("currently checked-out", colorDefaults) + ")"
}
return out
}
type formatFunc func(format string, a ...interface{}) string
type colorFormatter struct {
pattern *regexp.Regexp
format formatFunc
}
func newColorFormatter(pattern string, format formatFunc) colorFormatter {
return colorFormatter{
pattern: regexp.MustCompile(pattern),
format: format,
}
}
func withColor(str string, colors []colorFormatter) string {
for _, f := range colors {
if f.pattern.MatchString(str) {
return f.format(str)
}
}
return str
}
var colorDefaults = []colorFormatter{
newColorFormatter("feature", color.GreenString),
newColorFormatter("test", color.GreenString),
newColorFormatter("improvement", color.YellowString),
newColorFormatter("refactor", color.YellowString),
newColorFormatter("currently checked-out", color.YellowString),
newColorFormatter("fix", color.RedString),
newColorFormatter("bugfix", color.RedString),
newColorFormatter("bug", color.RedString),
newColorFormatter("fixup", color.RedString),
newColorFormatter("debug", color.RedString),
newColorFormatter("backup", color.RedString),
newColorFormatter("master", color.CyanString),
newColorFormatter("remotes", color.MagentaString),
newColorFormatter("origin", color.MagentaString),
newColorFormatter(".*", color.WhiteString),
}