|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/fatih/color" |
| 10 | +) |
| 11 | + |
| 12 | +func cmdOutput(command string, args ...string) (string, error) { |
| 13 | + cmd := exec.Command(command, args...) |
| 14 | + b, err := cmd.CombinedOutput() |
| 15 | + return string(b), err |
| 16 | +} |
| 17 | + |
| 18 | +func cmdRun(command string, args ...string) error { |
| 19 | + cmd := exec.Command(command, args...) |
| 20 | + cmd.Stdout = os.Stdout |
| 21 | + cmd.Stderr = os.Stderr |
| 22 | + if err := cmd.Run(); err != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + return nil |
| 26 | +} |
| 27 | + |
| 28 | +// branch describes a Git branch. |
| 29 | +type branch struct { |
| 30 | + name string |
| 31 | + isCurrent bool |
| 32 | +} |
| 33 | + |
| 34 | +// branchesState provides the state for the application, based on CQRS |
| 35 | +// Includes view and command methods |
| 36 | +type branchesState struct { |
| 37 | + branches []branch |
| 38 | + selected int |
| 39 | +} |
| 40 | + |
| 41 | +func (b *branchesState) init(args []string) error { |
| 42 | + args = append([]string{"branch"}, args...) |
| 43 | + out, err := cmdOutput("git", args...) |
| 44 | + if err != nil { |
| 45 | + println(out) |
| 46 | + return err |
| 47 | + } |
| 48 | + b.branches = splitBranches(out) |
| 49 | + b.selectCurrent() |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +func splitBranches(output string) []branch { |
| 54 | + names := strings.Split(output, "\n") |
| 55 | + var branches []branch |
| 56 | + for _, name := range names { |
| 57 | + if len(name) == 0 { |
| 58 | + continue |
| 59 | + } |
| 60 | + isCurrent := false |
| 61 | + if strings.Contains(name, "*") { |
| 62 | + name = strings.Replace(name, "*", "", -1) |
| 63 | + isCurrent = true |
| 64 | + } |
| 65 | + |
| 66 | + name = strings.TrimSpace(name) |
| 67 | + branches = append(branches, branch{name: name, isCurrent: isCurrent}) |
| 68 | + } |
| 69 | + return branches |
| 70 | +} |
| 71 | + |
| 72 | +func extractBranch(name string) string { |
| 73 | + if strings.Contains(name, "->") { |
| 74 | + s := strings.Split(name, "->") |
| 75 | + return strings.TrimSpace(s[0]) |
| 76 | + } |
| 77 | + return name |
| 78 | +} |
| 79 | + |
| 80 | +// Commands |
| 81 | +// These methods allow to mutate state |
| 82 | + |
| 83 | +func (b *branchesState) selectCurrent() { |
| 84 | + for ; !b.branches[b.selected].isCurrent; b.selected++ { |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (b *branchesState) selectNext() { |
| 89 | + b.selected = (b.selected + len(b.branches) - 1) % len(b.branches) |
| 90 | +} |
| 91 | + |
| 92 | +func (b *branchesState) selectPrevious() { |
| 93 | + b.selected = ((b.selected + 1) % len(b.branches)) |
| 94 | +} |
| 95 | + |
| 96 | +// View |
| 97 | +// These methods allow to present the state |
| 98 | + |
| 99 | +func (b *branchesState) selectedBranchName() string { |
| 100 | + return b.branches[b.selected].name |
| 101 | +} |
| 102 | + |
| 103 | +func (b *branchesState) selectedBranchWithColor() string { |
| 104 | + formatted := []string{} |
| 105 | + fields := strings.Split(b.selectedBranchName(), "/") |
| 106 | + for _, f := range fields { |
| 107 | + formatted = append(formatted, withColor(f, colorDefaults)) |
| 108 | + } |
| 109 | + out := strings.Join(formatted, withColor("/", colorDefaults)) |
| 110 | + if b.branches[b.selected].isCurrent { |
| 111 | + out += "\t(" + withColor("currently checked-out", colorDefaults) + ")" |
| 112 | + } |
| 113 | + return out |
| 114 | +} |
| 115 | + |
| 116 | +type formatFunc func(format string, a ...interface{}) string |
| 117 | + |
| 118 | +type colorFormatter struct { |
| 119 | + pattern *regexp.Regexp |
| 120 | + format formatFunc |
| 121 | +} |
| 122 | + |
| 123 | +func newColorFormatter(pattern string, format formatFunc) colorFormatter { |
| 124 | + return colorFormatter{ |
| 125 | + pattern: regexp.MustCompile(pattern), |
| 126 | + format: format, |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func withColor(str string, colors []colorFormatter) string { |
| 131 | + for _, f := range colors { |
| 132 | + if f.pattern.MatchString(str) { |
| 133 | + return f.format(str) |
| 134 | + } |
| 135 | + } |
| 136 | + return str |
| 137 | +} |
| 138 | + |
| 139 | +var colorDefaults = []colorFormatter{ |
| 140 | + newColorFormatter("feature", color.GreenString), |
| 141 | + newColorFormatter("test", color.GreenString), |
| 142 | + |
| 143 | + newColorFormatter("improvement", color.YellowString), |
| 144 | + newColorFormatter("refactor", color.YellowString), |
| 145 | + newColorFormatter("currently checked-out", color.YellowString), |
| 146 | + |
| 147 | + newColorFormatter("fix", color.RedString), |
| 148 | + newColorFormatter("bugfix", color.RedString), |
| 149 | + newColorFormatter("bug", color.RedString), |
| 150 | + newColorFormatter("fixup", color.RedString), |
| 151 | + newColorFormatter("debug", color.RedString), |
| 152 | + newColorFormatter("backup", color.RedString), |
| 153 | + |
| 154 | + newColorFormatter("master", color.CyanString), |
| 155 | + |
| 156 | + newColorFormatter("remotes", color.MagentaString), |
| 157 | + newColorFormatter("origin", color.MagentaString), |
| 158 | + |
| 159 | + newColorFormatter(".*", color.WhiteString), |
| 160 | +} |
0 commit comments