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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
package gui
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/isacikgoz/gitbatch/internal/command"
"github.com/isacikgoz/gitbatch/internal/git"
"github.com/isacikgoz/gitbatch/internal/job"
)
var (
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)
magenta = color.New(color.FgMagenta)
keySymbol = " " + yellow.Sprint("🔑") + ws
sep = " " + yellow.Sprint("|") + ws
pushable = "↖"
pullable = "↘"
)
const (
maxBranchLength = 40
maxRepositoryLength = 80
hashLength = 7
ws = " "
queuedSymbol = "•"
workingSymbol = "•"
successSymbol = "✔"
failSymbol = "✗"
fetchSymbol = "↓"
pullSymbol = "↓↳"
mergeSymbol = "↳"
checkoutSymbol = "↱"
modeSeperator = ""
keyBindingSeperator = "â–‘"
selectionIndicator = "→" + ws
tab = ws
)
// RepositoryDecorationRules is a rule set for creating repository labels
type RepositoryDecorationRules struct {
MaxName int
MaxPushables int
MaxPullables int
MaxBranch int
}
// repository render rules
func (gui *Gui) renderRules() *RepositoryDecorationRules {
rules := &RepositoryDecorationRules{
MaxBranch: 15,
MaxName: 30,
}
for _, r := range gui.State.Repositories {
if len(r.State.Branch.Pullables) > rules.MaxPullables {
rules.MaxPullables = len(r.State.Branch.Pullables)
}
if len(r.State.Branch.Pushables) > rules.MaxPushables {
rules.MaxPushables = len(r.State.Branch.Pushables)
}
if len(r.State.Branch.Name) > maxBranchLength {
rules.MaxBranch = maxBranchLength
}
if len(r.Name) > maxRepositoryLength {
rules.MaxName = maxRepositoryLength
}
}
rules.MaxBranch = rules.MaxBranch + len(cyan.Sprint("")) + 2
return rules
}
// 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 {
renderRules := gui.renderRules()
gui.renderTableHeader(renderRules)
var line string
line = line + renderRevCount(r, renderRules) + sep
line = line + renderBranchName(r, renderRules) + sep
line = line + gui.renderRepoName(r, renderRules) + sep
line = line + gui.renderStatus(r)
return line
}
// render repo name, print green if cursor is on the repository
func (gui *Gui) renderRepoName(r *git.Repository, rule *RepositoryDecorationRules) string {
var repoName string
sr := gui.getSelectedRepository()
if sr == r {
n, in := align(r.Name, rule.MaxName-2, true)
in = in + strings.Repeat(" ", n)
return selectionIndicator + green.Sprint(in)
}
n, in := align(r.Name, rule.MaxName, true)
in = in + strings.Repeat(" ", n)
repoName = in
return repoName
}
// render branch, add x if it is dirty
func renderBranchName(r *git.Repository, rule *RepositoryDecorationRules) string {
b := r.State.Branch
branch := b.Name
if !b.Clean {
n, in := align(branch, rule.MaxBranch-2, true)
return cyan.Sprint(in) + " " + yellow.Sprint("✗") + strings.Repeat(" ", n)
}
n, in := align(branch, rule.MaxBranch, true)
return cyan.Sprint(in) + strings.Repeat(" ", n)
}
// render ahead and behind info
func renderRevCount(r *git.Repository, rule *RepositoryDecorationRules) string {
var revCount string
b := r.State.Branch
if b.Pushables != "?" {
n1, part1 := align(b.Pushables, rule.MaxPushables, false)
n2, part2 := align(b.Pullables, rule.MaxPullables, false)
revCount = blue.Sprint(pushable) + ws + strings.Repeat(" ", n1) + part1 +
ws + blue.Sprint(pullable) + ws + strings.Repeat(" ", n2) + part2
} else {
n1, part1 := align(b.Pushables, rule.MaxPushables, false)
n2, part2 := align(b.Pullables, rule.MaxPullables, false)
revCount = blue.Sprint(pushable) + ws + strings.Repeat(" ", n1) + yellow.Sprint(part1) +
ws + blue.Sprint(pullable) + ws + strings.Repeat(" ", n2) + yellow.Sprint(part2)
}
return revCount
}
// render working status of the repository
func (gui *Gui) renderStatus(r *git.Repository) string {
var status string
if r.WorkStatus() == git.Queued {
if inQueue, j := gui.State.Queue.IsInTheQueue(r); inQueue {
status = printQueued(r, j)
}
} else if r.WorkStatus() == git.Working {
status = green.Sprint(workingSymbol) + ws + r.State.Message
} else if r.WorkStatus() == git.Success {
status = green.Sprint(successSymbol) + ws + r.State.Message
} else if r.WorkStatus() == git.Paused {
status = yellow.Sprint("! authentication required (u)")
} else if r.WorkStatus() == git.Fail {
status = red.Sprint(failSymbol) + ws + red.Sprint(r.State.Message)
}
return status
}
// render header of the table layout
func (gui *Gui) renderTableHeader(rule *RepositoryDecorationRules) {
v, err := gui.g.View(mainViewFrameFeature.Name)
if err != nil {
return
}
v.Clear()
var header string
revlen := 2 + rule.MaxPullables + 2 + rule.MaxPushables + 1
n, in := align("revs", revlen, true)
header = ws + magenta.Sprint(in) + strings.Repeat(" ", n) + sep
n, in = align("branch", rule.MaxBranch, true)
header = header + magenta.Sprint(in) + strings.Repeat(" ", n) + sep
n, in = align("name", rule.MaxName, true)
header = header + magenta.Sprint(in) + strings.Repeat(" ", n) + sep
fmt.Fprintln(v, header)
}
// print queued item with the mode color
func printQueued(r *git.Repository, j *job.Job) string {
var info string
switch jt := j.JobType; jt {
case job.FetchJob:
info = blue.Sprint(queuedSymbol) + ws + "(" + blue.Sprint("fetch") + ws + r.State.Remote.Name + ")"
case job.PullJob:
info = magenta.Sprint(queuedSymbol) + ws + "(" + magenta.Sprint("pull") + ws + r.State.Remote.Name + ")"
case job.MergeJob:
info = cyan.Sprint(queuedSymbol) + ws + "(" + cyan.Sprint("merge") + ws + r.State.Branch.Upstream.Name + ")"
case job.CheckoutJob:
refName := j.Options.(*command.CheckoutOptions).TargetRef
info = green.Sprint(queuedSymbol) + ws + "(" + cyan.Sprint("switch branch to") + ws + refName + ")"
default:
info = green.Sprint(queuedSymbol)
}
return info
}
// render commit label according to its status(local/even/remote)
func commitLabel(c *git.Commit, sel bool) string {
re := regexp.MustCompile(`\r?\n`)
msg := re.ReplaceAllString(c.Message, " ")
if sel {
msg = green.Sprint(msg)
}
var body string
switch c.CommitType {
case git.EvenCommit:
body = cyan.Sprint(c.Hash[:hashLength]) + " " + msg
case git.LocalCommit:
body = blue.Sprint(c.Hash[:hashLength]) + " " + msg
case git.RemoteCommit:
if len(c.Hash) > hashLength {
body = yellow.Sprint(c.Hash[:hashLength]) + " " + msg
} else {
body = yellow.Sprint(c.Hash[:len(c.Hash)]) + " " + msg
}
default:
body = c.Hash[:hashLength] + " " + msg
}
return body
}
// 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 {
switch rn := line[0]; rn {
case '-':
colorized[i] = red.Sprint(line)
continue
case '+':
colorized[i] = green.Sprint(line)
continue
default:
}
if re.MatchString(line) {
s := re.FindString(line)
colorized[i] = cyan.Sprint(s) + line[len(s):]
}
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
}
// DiffStatDecorationRules is a rule set for creating diffstat text
type DiffStatDecorationRules struct {
MaxNameLength int
MaxChangeCountLength int
MaxChangesLength int
}
// DiffStatItem is a line of a diff stat
type DiffStatItem struct {
FileName string
ChangeCount string
Changes string
}
// get output of "git show <commit> --shortstat" and convert it to DiffStatItem
// slice and generate rules
func genDiffStat(in string) (*DiffStatDecorationRules, []*DiffStatItem) {
rules := &DiffStatDecorationRules{}
stats := make([]*DiffStatItem, 0)
re := regexp.MustCompile(`\s+\|\s+`)
r1 := regexp.MustCompile(`\d+\s+`)
for _, line := range strings.Split(in, "\n") {
s := re.Split(line, 2)
ds := &DiffStatItem{}
ds.FileName = s[0]
if rules.MaxNameLength < len(ds.FileName) {
rules.MaxNameLength = len(ds.FileName)
}
if len(s) > 1 && r1.MatchString(s[1]) {
cc := r1.FindString(s[1])
ds.ChangeCount = strings.TrimSpace(cc)
if rules.MaxChangeCountLength < len(ds.ChangeCount) {
rules.MaxChangeCountLength = len(ds.ChangeCount)
}
d := r1.Split(s[1], 2)
ds.Changes = d[1]
if rules.MaxChangesLength < len(ds.Changes) {
rules.MaxChangesLength = len(ds.Changes)
}
}
stats = append(stats, ds)
}
return rules, stats
}
// colorize diff stat
func decorateDiffStat(in string, sum bool) string {
var d string
s := strings.Split(in, "\n")
if sum {
d = strconv.Itoa(len(s)-1) + " file(s) changed." + "\n\n"
}
rule, stats := genDiffStat(in)
for _, stat := range stats {
if len(stat.FileName) <= 0 {
continue
}
n1, part1 := align(stat.FileName, rule.MaxNameLength, true)
n2, part2 := align(stat.ChangeCount, rule.MaxChangeCountLength, false)
d = d + cyan.Sprint(part1) + strings.Repeat(" ", n1) + yellow.Sprint(" | ") + strings.Repeat(" ", n2) + part2 + " "
for _, r := range stat.Changes {
switch r {
case '+':
d = d + green.Sprint(string(r))
case '-':
d = d + red.Sprint(string(r))
default:
d = d + string(r)
}
}
d = d + "\n"
}
return d
}
// align text with whitespaces
func align(in string, max int, trim bool) (int, string) {
realmax := 50
il := len(in)
if max > realmax {
max = 50
}
if trim && il > max {
return 0, in[:max-2] + ".."
}
if il < max {
return max - il, in
//true
//in = in + strings.Repeat(" ", max-il)
//false
//return max-il, in
//in = strings.Repeat(" ", max-il) + in
}
return 0, in
}
// colorize commit info
func decorateCommit(in string) string {
var d string
lines := strings.Split(in, "\n")
d = d + strings.Replace(lines[0], "Hash:", cyan.Sprint("Hash:"), 1) + "\n"
d = d + strings.Replace(lines[1], "Author:", cyan.Sprint("Author:"), 1) + "\n"
d = d + strings.Replace(lines[2], "Date:", cyan.Sprint("Date:"), 1) + "\n"
for _, l := range lines[3:] {
d = d + l + "\n"
}
return d
}
|