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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
package gui
import (
"regexp"
"strings"
"github.com/fatih/color"
"github.com/isacikgoz/gitbatch/core/git"
"github.com/isacikgoz/gitbatch/core/job"
)
var (
black = color.New(color.FgBlack)
blue = color.New(color.FgBlue)
green = color.New(color.FgGreen)
red = color.New(color.FgRed)
cyan = color.New(color.FgCyan)
yellow = color.New(color.FgYellow)
white = color.New(color.FgWhite)
magenta = color.New(color.FgMagenta)
bold = color.New(color.Bold)
maxBranchLength = 15
maxRepositoryLength = 20
hashLength = 7
ws = " "
pushable = string(blue.Sprint("↖"))
pullable = string(blue.Sprint("↘"))
dirty = string(yellow.Sprint("✗"))
queuedSymbol = "•"
workingSymbol = "•"
successSymbol = "✔"
pauseSymbol = "॥"
failSymbol = "✗"
fetchSymbol = "↓"
pullSymbol = "↓↳"
mergeSymbol = "↳"
keySymbol = ws + yellow.Sprint("🔑") + ws
modeSeperator = ""
keyBindingSeperator = "░"
selectionIndicator = ws + string(green.Sprint("→")) + ws
tab = ws
)
// this function handles the render and representation of the repository
// TODO: cleanup is required, right now it looks too complicated
func (gui *Gui) repositoryLabel(r *git.Repository) string {
var prefix string
if r.Branch.Pushables != "?" {
prefix = prefix + pushable + ws + r.Branch.Pushables +
ws + pullable + ws + r.Branch.Pullables
} else {
prefix = prefix + pushable + ws + yellow.Sprint(r.Branch.Pushables) +
ws + pullable + ws + yellow.Sprint(r.Branch.Pullables)
}
var repoName string
sr := gui.getSelectedRepository()
if sr == r {
prefix = prefix + selectionIndicator
repoName = green.Sprint(r.Name)
} else {
prefix = prefix + ws
repoName = r.Name
}
// some branch names can be really long, in that times I hope the first
// characters are important and meaningful
branch := adjustTextLength(r.Branch.Name, maxBranchLength)
prefix = prefix + string(cyan.Sprint(branch))
if !r.Branch.Clean {
prefix = prefix + ws + dirty + ws
} else {
prefix = prefix + ws
}
var suffix string
// rendering the satus according to repository's state
if r.State() == git.Queued {
if inQueue, j := gui.State.Queue.IsInTheQueue(r); inQueue {
switch mode := j.JobType; mode {
case job.FetchJob:
suffix = blue.Sprint(queuedSymbol)
case job.PullJob:
suffix = magenta.Sprint(queuedSymbol)
case job.MergeJob:
suffix = cyan.Sprint(queuedSymbol)
default:
suffix = green.Sprint(queuedSymbol)
}
}
return prefix + repoName + ws + suffix
} else if r.State() == git.Working {
// TODO: maybe the type of the job can be written while its working?
return prefix + repoName + ws + green.Sprint(workingSymbol)
} else if r.State() == git.Success {
return prefix + repoName + ws + green.Sprint(successSymbol)
} else if r.State() == git.Paused {
return prefix + repoName + ws + yellow.Sprint("authentication required (u)")
} else if r.State() == git.Fail {
return prefix + repoName + ws + red.Sprint(failSymbol) + ws + red.Sprint(r.Message)
}
return prefix + repoName
}
func commitLabel(c *git.Commit) string {
var body string
switch c.CommitType {
case git.EvenCommit:
body = cyan.Sprint(c.Hash[:hashLength]) + " " + c.Message
case git.LocalCommit:
body = blue.Sprint(c.Hash[:hashLength]) + " " + c.Message
case git.RemoteCommit:
body = yellow.Sprint(c.Hash[:hashLength]) + " " + c.Message
default:
body = c.Hash[:hashLength] + " " + c.Message
}
return body
}
// limit the text length for visual concerns
func adjustTextLength(text string, maxLength int) string {
if len(text) > maxLength {
return text[:maxLength-2] + ".."
}
return text
}
// colorize the plain diff text collected from system output
// the style is near to original diff command
func colorizeDiff(original string) (colorized []string) {
colorized = strings.Split(original, "\n")
re := regexp.MustCompile(`@@ .+ @@`)
for i, line := range colorized {
if len(line) > 0 {
if line[0] == '-' {
colorized[i] = red.Sprint(line)
} else if line[0] == '+' {
colorized[i] = green.Sprint(line)
} else if re.MatchString(line) {
s := re.FindString(line)
colorized[i] = cyan.Sprint(s) + line[len(s):]
} else {
continue
}
} else {
continue
}
}
return colorized
}
// the remote link can be too verbose sometimes, so it is good to trim it
func trimRemoteURL(url string) (urltype string, shorturl string) {
// lets trim the unnecessary .git extension of the url
regit := regexp.MustCompile(`.git`)
if regit.MatchString(url[len(url)-4:]) {
url = url[:len(url)-4]
}
// find out the protocol
ressh := regexp.MustCompile(`git@`)
rehttp := regexp.MustCompile(`http://`)
rehttps := regexp.MustCompile(`https://`)
// separate the protocol and remote link
if ressh.MatchString(url) {
shorturl = ressh.Split(url, 5)[1]
urltype = "ssh"
} else if rehttp.MatchString(url) {
shorturl = rehttp.Split(url, 5)[1]
urltype = "http"
} else if rehttps.MatchString(url) {
shorturl = rehttps.Split(url, 5)[1]
urltype = "https"
}
return urltype, shorturl
}
|