| 1 | package gui |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "regexp" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/fatih/color" |
| 10 | "github.com/isacikgoz/gitbatch/internal/command" |
| 11 | "github.com/isacikgoz/gitbatch/internal/git" |
| 12 | "github.com/isacikgoz/gitbatch/internal/job" |
| 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | blue = color.New(color.FgBlue) |
| 17 | green = color.New(color.FgGreen) |
| 18 | red = color.New(color.FgRed) |
| 19 | cyan = color.New(color.FgCyan) |
| 20 | yellow = color.New(color.FgYellow) |
| 21 | magenta = color.New(color.FgMagenta) |
| 22 | |
| 23 | keySymbol = " " + yellow.Sprint("🔑") + ws |
| 24 | sep = " " + yellow.Sprint("|") + ws |
| 25 | |
| 26 | pushable = "↖" |
| 27 | pullable = "↘" |
| 28 | ) |
| 29 | |
| 30 | const ( |
| 31 | maxBranchLength = 40 |
| 32 | maxRepositoryLength = 80 |
| 33 | hashLength = 7 |
| 34 | |
| 35 | ws = " " |
| 36 | queuedSymbol = "•" |
| 37 | workingSymbol = "•" |
| 38 | successSymbol = "✔" |
| 39 | failSymbol = "✗" |
| 40 | |
| 41 | fetchSymbol = "↓" |
| 42 | pullSymbol = "↓↳" |
| 43 | mergeSymbol = "↳" |
| 44 | checkoutSymbol = "↱" |
| 45 | modeSeperator = "" |
| 46 | keyBindingSeperator = "â–‘" |
| 47 | |
| 48 | selectionIndicator = "→" + ws |
| 49 | tab = ws |
| 50 | ) |
| 51 | |
| 52 | // RepositoryDecorationRules is a rule set for creating repository labels |
| 53 | type RepositoryDecorationRules struct { |
| 54 | MaxName int |
| 55 | MaxPushables int |
| 56 | MaxPullables int |
| 57 | MaxBranch int |
| 58 | } |
| 59 | |
| 60 | // repository render rules |
| 61 | func (gui *Gui) renderRules() *RepositoryDecorationRules { |
| 62 | rules := &RepositoryDecorationRules{ |
| 63 | MaxBranch: 15, |
| 64 | MaxName: 30, |
| 65 | } |
| 66 | |
| 67 | for _, r := range gui.State.Repositories { |
| 68 | if len(r.State.Branch.Pullables) > rules.MaxPullables { |
| 69 | rules.MaxPullables = len(r.State.Branch.Pullables) |
| 70 | } |
| 71 | if len(r.State.Branch.Pushables) > rules.MaxPushables { |
| 72 | rules.MaxPushables = len(r.State.Branch.Pushables) |
| 73 | } |
| 74 | if len(r.State.Branch.Name) > maxBranchLength { |
| 75 | rules.MaxBranch = maxBranchLength |
| 76 | } |
| 77 | if len(r.Name) > maxRepositoryLength { |
| 78 | rules.MaxName = maxRepositoryLength |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | rules.MaxBranch = rules.MaxBranch + len(cyan.Sprint("")) + 2 |
| 83 | return rules |
| 84 | } |
| 85 | |
| 86 | // this function handles the render and representation of the repository |
| 87 | // TODO: cleanup is required, right now it looks too complicated |
| 88 | func (gui *Gui) repositoryLabel(r *git.Repository) string { |
| 89 | renderRules := gui.renderRules() |
| 90 | |
| 91 | gui.renderTableHeader(renderRules) |
| 92 | |
| 93 | var line string |
| 94 | |
| 95 | line = line + renderRevCount(r, renderRules) + sep |
| 96 | line = line + renderBranchName(r, renderRules) + sep |
| 97 | line = line + gui.renderRepoName(r, renderRules) + sep |
| 98 | line = line + gui.renderStatus(r) |
| 99 | |
| 100 | return line |
| 101 | } |
| 102 | |
| 103 | // render repo name, print green if cursor is on the repository |
| 104 | func (gui *Gui) renderRepoName(r *git.Repository, rule *RepositoryDecorationRules) string { |
| 105 | var repoName string |
| 106 | sr := gui.getSelectedRepository() |
| 107 | if sr == r { |
| 108 | n, in := align(r.Name, rule.MaxName-2, true) |
| 109 | in = in + strings.Repeat(" ", n) |
| 110 | return selectionIndicator + green.Sprint(in) |
| 111 | } |
| 112 | |
| 113 | n, in := align(r.Name, rule.MaxName, true) |
| 114 | in = in + strings.Repeat(" ", n) |
| 115 | repoName = in |
| 116 | |
| 117 | return repoName |
| 118 | } |
| 119 | |
| 120 | // render branch, add x if it is dirty |
| 121 | func renderBranchName(r *git.Repository, rule *RepositoryDecorationRules) string { |
| 122 | b := r.State.Branch |
| 123 | branch := b.Name |
| 124 | if !b.Clean { |
| 125 | n, in := align(branch, rule.MaxBranch-2, true) |
| 126 | return cyan.Sprint(in) + " " + yellow.Sprint("✗") + strings.Repeat(" ", n) |
| 127 | } |
| 128 | n, in := align(branch, rule.MaxBranch, true) |
| 129 | return cyan.Sprint(in) + strings.Repeat(" ", n) |
| 130 | } |
| 131 | |
| 132 | // render ahead and behind info |
| 133 | func renderRevCount(r *git.Repository, rule *RepositoryDecorationRules) string { |
| 134 | var revCount string |
| 135 | b := r.State.Branch |
| 136 | if b.Pushables != "?" { |
| 137 | n1, part1 := align(b.Pushables, rule.MaxPushables, false) |
| 138 | n2, part2 := align(b.Pullables, rule.MaxPullables, false) |
| 139 | revCount = blue.Sprint(pushable) + ws + strings.Repeat(" ", n1) + part1 + |
| 140 | ws + blue.Sprint(pullable) + ws + strings.Repeat(" ", n2) + part2 |
| 141 | } else { |
| 142 | n1, part1 := align(b.Pushables, rule.MaxPushables, false) |
| 143 | n2, part2 := align(b.Pullables, rule.MaxPullables, false) |
| 144 | revCount = blue.Sprint(pushable) + ws + strings.Repeat(" ", n1) + yellow.Sprint(part1) + |
| 145 | ws + blue.Sprint(pullable) + ws + strings.Repeat(" ", n2) + yellow.Sprint(part2) |
| 146 | } |
| 147 | return revCount |
| 148 | } |
| 149 | |
| 150 | // render working status of the repository |
| 151 | func (gui *Gui) renderStatus(r *git.Repository) string { |
| 152 | var status string |
| 153 | if r.WorkStatus() == git.Queued { |
| 154 | if inQueue, j := gui.State.Queue.IsInTheQueue(r); inQueue { |
| 155 | status = printQueued(r, j) |
| 156 | } |
| 157 | } else if r.WorkStatus() == git.Working { |
| 158 | status = green.Sprint(workingSymbol) + ws + r.State.Message |
| 159 | } else if r.WorkStatus() == git.Success { |
| 160 | status = green.Sprint(successSymbol) + ws + r.State.Message |
| 161 | } else if r.WorkStatus() == git.Paused { |
| 162 | status = yellow.Sprint("! authentication required (u)") |
| 163 | } else if r.WorkStatus() == git.Fail { |
| 164 | status = red.Sprint(failSymbol) + ws + red.Sprint(r.State.Message) |
| 165 | } |
| 166 | return status |
| 167 | } |
| 168 | |
| 169 | // render header of the table layout |
| 170 | func (gui *Gui) renderTableHeader(rule *RepositoryDecorationRules) { |
| 171 | v, err := gui.g.View(mainViewFrameFeature.Name) |
| 172 | if err != nil { |
| 173 | return |
| 174 | } |
| 175 | v.Clear() |
| 176 | var header string |
| 177 | revlen := 2 + rule.MaxPullables + 2 + rule.MaxPushables + 1 |
| 178 | n, in := align("revs", revlen, true) |
| 179 | header = ws + magenta.Sprint(in) + strings.Repeat(" ", n) + sep |
| 180 | n, in = align("branch", rule.MaxBranch, true) |
| 181 | header = header + magenta.Sprint(in) + strings.Repeat(" ", n) + sep |
| 182 | n, in = align("name", rule.MaxName, true) |
| 183 | header = header + magenta.Sprint(in) + strings.Repeat(" ", n) + sep |
| 184 | fmt.Fprintln(v, header) |
| 185 | } |
| 186 | |
| 187 | // print queued item with the mode color |
| 188 | func printQueued(r *git.Repository, j *job.Job) string { |
| 189 | var info string |
| 190 | switch jt := j.JobType; jt { |
| 191 | case job.FetchJob: |
| 192 | info = blue.Sprint(queuedSymbol) + ws + "(" + blue.Sprint("fetch") + ws + r.State.Remote.Name + ")" |
| 193 | case job.PullJob: |
| 194 | info = magenta.Sprint(queuedSymbol) + ws + "(" + magenta.Sprint("pull") + ws + r.State.Remote.Name + ")" |
| 195 | case job.MergeJob: |
| 196 | info = cyan.Sprint(queuedSymbol) + ws + "(" + cyan.Sprint("merge") + ws + r.State.Branch.Upstream.Name + ")" |
| 197 | case job.CheckoutJob: |
| 198 | refName := j.Options.(*command.CheckoutOptions).TargetRef |
| 199 | info = green.Sprint(queuedSymbol) + ws + "(" + cyan.Sprint("switch branch to") + ws + refName + ")" |
| 200 | default: |
| 201 | info = green.Sprint(queuedSymbol) |
| 202 | } |
| 203 | return info |
| 204 | } |
| 205 | |
| 206 | // render commit label according to its status(local/even/remote) |
| 207 | func commitLabel(c *git.Commit, sel bool) string { |
| 208 | re := regexp.MustCompile(`\r?\n`) |
| 209 | msg := re.ReplaceAllString(c.Message, " ") |
| 210 | if sel { |
| 211 | msg = green.Sprint(msg) |
| 212 | } |
| 213 | var body string |
| 214 | switch c.CommitType { |
| 215 | case git.EvenCommit: |
| 216 | body = cyan.Sprint(c.Hash[:hashLength]) + " " + msg |
| 217 | case git.LocalCommit: |
| 218 | body = blue.Sprint(c.Hash[:hashLength]) + " " + msg |
| 219 | case git.RemoteCommit: |
| 220 | if len(c.Hash) > hashLength { |
| 221 | body = yellow.Sprint(c.Hash[:hashLength]) + " " + msg |
| 222 | } else { |
| 223 | body = yellow.Sprint(c.Hash[:len(c.Hash)]) + " " + msg |
| 224 | } |
| 225 | default: |
| 226 | body = c.Hash[:hashLength] + " " + msg |
| 227 | } |
| 228 | return body |
| 229 | } |
| 230 | |
| 231 | // colorize the plain diff text collected from system output |
| 232 | // the style is near to original diff command |
| 233 | func colorizeDiff(original string) (colorized []string) { |
| 234 | colorized = strings.Split(original, "\n") |
| 235 | re := regexp.MustCompile(`@@ .+ @@`) |
| 236 | for i, line := range colorized { |
| 237 | if len(line) > 0 { |
| 238 | switch rn := line[0]; rn { |
| 239 | case '-': |
| 240 | colorized[i] = red.Sprint(line) |
| 241 | continue |
| 242 | case '+': |
| 243 | colorized[i] = green.Sprint(line) |
| 244 | continue |
| 245 | default: |
| 246 | } |
| 247 | |
| 248 | if re.MatchString(line) { |
| 249 | s := re.FindString(line) |
| 250 | colorized[i] = cyan.Sprint(s) + line[len(s):] |
| 251 | } |
| 252 | continue |
| 253 | |
| 254 | } else { |
| 255 | continue |
| 256 | } |
| 257 | } |
| 258 | return colorized |
| 259 | } |
| 260 | |
| 261 | // the remote link can be too verbose sometimes, so it is good to trim it |
| 262 | func trimRemoteURL(url string) (urltype string, shorturl string) { |
| 263 | // lets trim the unnecessary .git extension of the url |
| 264 | regit := regexp.MustCompile(`.git`) |
| 265 | if regit.MatchString(url[len(url)-4:]) { |
| 266 | url = url[:len(url)-4] |
| 267 | } |
| 268 | |
| 269 | // find out the protocol |
| 270 | ressh := regexp.MustCompile(`git@`) |
| 271 | rehttp := regexp.MustCompile(`http://`) |
| 272 | rehttps := regexp.MustCompile(`https://`) |
| 273 | |
| 274 | // separate the protocol and remote link |
| 275 | if ressh.MatchString(url) { |
| 276 | shorturl = ressh.Split(url, 5)[1] |
| 277 | urltype = "ssh" |
| 278 | } else if rehttp.MatchString(url) { |
| 279 | shorturl = rehttp.Split(url, 5)[1] |
| 280 | urltype = "http" |
| 281 | } else if rehttps.MatchString(url) { |
| 282 | shorturl = rehttps.Split(url, 5)[1] |
| 283 | urltype = "https" |
| 284 | } |
| 285 | return urltype, shorturl |
| 286 | } |
| 287 | |
| 288 | // DiffStatDecorationRules is a rule set for creating diffstat text |
| 289 | type DiffStatDecorationRules struct { |
| 290 | MaxNameLength int |
| 291 | MaxChangeCountLength int |
| 292 | MaxChangesLength int |
| 293 | } |
| 294 | |
| 295 | // DiffStatItem is a line of a diff stat |
| 296 | type DiffStatItem struct { |
| 297 | FileName string |
| 298 | ChangeCount string |
| 299 | Changes string |
| 300 | } |
| 301 | |
| 302 | // get output of "git show <commit> --shortstat" and convert it to DiffStatItem |
| 303 | // slice and generate rules |
| 304 | func genDiffStat(in string) (*DiffStatDecorationRules, []*DiffStatItem) { |
| 305 | rules := &DiffStatDecorationRules{} |
| 306 | stats := make([]*DiffStatItem, 0) |
| 307 | |
| 308 | re := regexp.MustCompile(`\s+\|\s+`) |
| 309 | r1 := regexp.MustCompile(`\d+\s+`) |
| 310 | |
| 311 | for _, line := range strings.Split(in, "\n") { |
| 312 | s := re.Split(line, 2) |
| 313 | ds := &DiffStatItem{} |
| 314 | ds.FileName = s[0] |
| 315 | |
| 316 | if rules.MaxNameLength < len(ds.FileName) { |
| 317 | rules.MaxNameLength = len(ds.FileName) |
| 318 | } |
| 319 | |
| 320 | if len(s) > 1 && r1.MatchString(s[1]) { |
| 321 | cc := r1.FindString(s[1]) |
| 322 | ds.ChangeCount = strings.TrimSpace(cc) |
| 323 | if rules.MaxChangeCountLength < len(ds.ChangeCount) { |
| 324 | rules.MaxChangeCountLength = len(ds.ChangeCount) |
| 325 | } |
| 326 | d := r1.Split(s[1], 2) |
| 327 | |
| 328 | ds.Changes = d[1] |
| 329 | if rules.MaxChangesLength < len(ds.Changes) { |
| 330 | rules.MaxChangesLength = len(ds.Changes) |
| 331 | } |
| 332 | } |
| 333 | stats = append(stats, ds) |
| 334 | } |
| 335 | return rules, stats |
| 336 | } |
| 337 | |
| 338 | // colorize diff stat |
| 339 | func decorateDiffStat(in string, sum bool) string { |
| 340 | var d string |
| 341 | |
| 342 | s := strings.Split(in, "\n") |
| 343 | if sum { |
| 344 | d = strconv.Itoa(len(s)-1) + " file(s) changed." + "\n\n" |
| 345 | } |
| 346 | rule, stats := genDiffStat(in) |
| 347 | for _, stat := range stats { |
| 348 | if len(stat.FileName) <= 0 { |
| 349 | continue |
| 350 | } |
| 351 | n1, part1 := align(stat.FileName, rule.MaxNameLength, true) |
| 352 | n2, part2 := align(stat.ChangeCount, rule.MaxChangeCountLength, false) |
| 353 | d = d + cyan.Sprint(part1) + strings.Repeat(" ", n1) + yellow.Sprint(" | ") + strings.Repeat(" ", n2) + part2 + " " |
| 354 | for _, r := range stat.Changes { |
| 355 | switch r { |
| 356 | case '+': |
| 357 | d = d + green.Sprint(string(r)) |
| 358 | case '-': |
| 359 | d = d + red.Sprint(string(r)) |
| 360 | default: |
| 361 | d = d + string(r) |
| 362 | } |
| 363 | } |
| 364 | d = d + "\n" |
| 365 | } |
| 366 | return d |
| 367 | } |
| 368 | |
| 369 | // align text with whitespaces |
| 370 | func align(in string, max int, trim bool) (int, string) { |
| 371 | realmax := 50 |
| 372 | il := len(in) |
| 373 | if max > realmax { |
| 374 | max = 50 |
| 375 | } |
| 376 | if trim && il > max { |
| 377 | return 0, in[:max-2] + ".." |
| 378 | } |
| 379 | if il < max { |
| 380 | return max - il, in |
| 381 | //true |
| 382 | //in = in + strings.Repeat(" ", max-il) |
| 383 | //false |
| 384 | //return max-il, in |
| 385 | //in = strings.Repeat(" ", max-il) + in |
| 386 | } |
| 387 | return 0, in |
| 388 | } |
| 389 | |
| 390 | // colorize commit info |
| 391 | func decorateCommit(in string) string { |
| 392 | var d string |
| 393 | lines := strings.Split(in, "\n") |
| 394 | d = d + strings.Replace(lines[0], "Hash:", cyan.Sprint("Hash:"), 1) + "\n" |
| 395 | d = d + strings.Replace(lines[1], "Author:", cyan.Sprint("Author:"), 1) + "\n" |
| 396 | d = d + strings.Replace(lines[2], "Date:", cyan.Sprint("Date:"), 1) + "\n" |
| 397 | for _, l := range lines[3:] { |
| 398 | d = d + l + "\n" |
| 399 | } |
| 400 | return d |
| 401 | } |