blob: 50bc9458193d2f035bfe5bb2aaf61e1541f70d3f (
about) (
plain)
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
|
package gui
import (
"regexp"
"github.com/fatih/color"
"github.com/isacikgoz/gitbatch/pkg/git"
"github.com/isacikgoz/gitbatch/pkg/queue"
)
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("↘"))
confidentArrow = string(magenta.Sprint(""))
unconfidentArrow = string(yellow.Sprint(""))
dirty = string(yellow.Sprint("✗"))
unknown = magenta.Sprint("?")
queuedSymbol = "•"
workingSymbol = "•"
successSymbol = "✔"
failSymbol = "✗"
fetchSymbol = "↓"
pullSymbol = "↓↳"
mergeSymbol = "↳"
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) displayString(entity *git.RepoEntity) string {
suffix := ""
prefix := ""
repoName := ""
if entity.Branch.Pushables != "?" {
prefix = prefix + pushable + ws + entity.Branch.Pushables +
ws + pullable + ws + entity.Branch.Pullables
} else {
prefix = prefix + pushable + ws + yellow.Sprint(entity.Branch.Pushables) +
ws + pullable + ws + yellow.Sprint(entity.Branch.Pullables)
}
selectedEntity := gui.getSelectedRepository()
if selectedEntity == entity {
prefix = prefix + selectionIndicator
repoName = green.Sprint(entity.Name)
} else {
prefix = prefix + ws
repoName = entity.Name
}
// some branch names can be really long, in that times I hope the first
// characters are important and meaningful
branch := adjustTextLength(entity.Branch.Name, maxBranchLength)
prefix = prefix + string(cyan.Sprint(branch))
if !entity.Branch.Clean {
prefix = prefix + ws + dirty + ws
} else {
prefix = prefix + ws
}
// rendering the satus according to repository's state
if entity.State == git.Queued {
if inQueue, ty := gui.State.Queue.IsInTheQueue(entity); inQueue {
switch mode := ty; mode {
case queue.Fetch:
suffix = blue.Sprint(queuedSymbol)
case queue.Pull:
suffix = magenta.Sprint(queuedSymbol)
case queue.Merge:
suffix = cyan.Sprint(queuedSymbol)
default:
suffix = green.Sprint(queuedSymbol)
}
}
return prefix + repoName + ws + suffix
} else if entity.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 entity.State == git.Success {
return prefix + repoName + ws + green.Sprint(successSymbol)
} else if entity.State == git.Fail {
return prefix + repoName + ws + red.Sprint(failSymbol)
} else {
return prefix + repoName
}
}
// limit the text length for visual concerns
func adjustTextLength(text string, maxLength int) (adjusted string) {
if len(text) > maxLength {
adjusted := text[:maxLength-2] + ".."
return adjusted
}
return text
}
// 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
}
|