diff options
author | Mike JS. Choi <mkchoi212@icloud.com> | 2017-12-31 22:02:36 -0600 |
---|---|---|
committer | Mike JS. Choi <mkchoi212@icloud.com> | 2017-12-31 22:02:36 -0600 |
commit | 41a8085638cade50293d784683d89713b324899f (patch) | |
tree | 6c9c1d6e2cf97cac08891c6bad273fc428c46791 | |
parent | 47c59228a01a907135bf2efb0dab8d6703c55ca0 (diff) |
Major refactoring to go with seperated packages
-rw-r--r-- | layout.go | 115 | ||||
-rw-r--r-- | main.go | 53 | ||||
-rw-r--r-- | prompt.go | 2 | ||||
-rw-r--r-- | summary.go | 35 |
4 files changed, 154 insertions, 51 deletions
@@ -15,6 +15,11 @@ const ( Panel = "panel" Prompt = "prompt" Input = "input prompt" + + Local = 1 + Incoming = 2 + Up = 3 + Down = 4 ) func layout(g *gocui.Gui) error { @@ -47,7 +52,7 @@ func layout(g *gocui.Gui) error { return err } v.Frame = false - prompt := Green(Regular, "[wasd] >>") + prompt := color.Green(color.Regular, "[wasd] >>") v.Write([]byte(prompt)) v.MoveCursor(11, 0, true) } @@ -66,3 +71,111 @@ func layout(g *gocui.Gui) error { } return nil } + +func Select(c *conflict.Conflict, g *gocui.Gui, showHelp bool) error { + g.Update(func(g *gocui.Gui) error { + v, err := g.View(Panel) + if err != nil { + return err + } + v.Clear() + + for idx, conflict := range conflict.All { + var out string + if conflict.Choice != 0 { + out = color.Green(color.Regular, "✅ %s:%d", conflict.FileName, conflict.Start) + } else { + out = color.Red(color.Regular, "%d. %s:%d", idx+1, conflict.FileName, conflict.Start) + } + + if conflict.Equal(c) { + fmt.Fprintf(v, "%s <-\n", out) + } else { + fmt.Fprintf(v, "%s\n", out) + } + } + + if showHelp { + printHelp(v) + } + return nil + }) + + g.Update(func(g *gocui.Gui) error { + v, err := g.View(Current) + if err != nil { + return err + } + var buf bytes.Buffer + buf.WriteString(c.CurrentName) + buf.WriteString(" (Current Change) ") + v.Title = buf.String() + + top, bottom := c.PaddingLines() + v.Clear() + printLines(v, top) + if c.DisplayDiff { + printLines(v, c.Diff()) + } else { + printLines(v, c.ColoredCurrentLines) + } + printLines(v, bottom) + + v, err = g.View(Foreign) + if err != nil { + return err + } + buf.Reset() + buf.WriteString(c.ForeignName) + buf.WriteString(" (Incoming Change) ") + v.Title = buf.String() + + top, bottom = c.PaddingLines() + v.Clear() + printLines(v, top) + printLines(v, c.ColoredForeignLines) + printLines(v, bottom) + return nil + }) + return nil +} + +func Resolve(c *conflict.Conflict, g *gocui.Gui, v *gocui.View, version int) error { + g.Update(func(g *gocui.Gui) error { + c.Choice = version + NextConflict(g, v) + return nil + }) + return nil +} + +func NextConflict(g *gocui.Gui, v *gocui.View) error { + originalCur := cur + + for originalCur != cur { + cur++ + if cur >= conflictCount { + cur = 0 + } + } + + if originalCur == cur { + globalQuit(g) + } + + Select(&conflict.All[cur], g, false) + return nil +} + +func Scroll(g *gocui.Gui, c *conflict.Conflict, direction int) { + if direction == Up { + c.TopPeek-- + c.BottomPeek++ + } else if direction == Down { + c.TopPeek++ + } else { + return + } + + Select(c, g, false) +} @@ -10,13 +10,13 @@ import ( "strings" "github.com/jroimartin/gocui" + "github.com/mkchoi212/fac/color" + "github.com/mkchoi212/fac/conflict" ) var ( - cur = 0 - conflictCount = 0 - conflicts = []Conflict{} - + cur = 0 + conflictCount = 0 consecutiveError = 0 ) @@ -34,33 +34,33 @@ func parseInput(g *gocui.Gui, v *gocui.View) error { evalCmd := func(in rune, g *gocui.Gui) { switch { case in == 'j': - scroll(g, &conflicts[cur], Up) + Scroll(g, &conflict.All[cur], Up) case in == 'k': - scroll(g, &conflicts[cur], Down) + Scroll(g, &conflict.All[cur], Down) case in == 'w': - conflicts[cur].topPeek++ - conflicts[cur].Select(g, false) + conflict.All[cur].TopPeek++ + Select(&conflict.All[cur], g, false) case in == 's': - conflicts[cur].bottomPeek++ - conflicts[cur].Select(g, false) + conflict.All[cur].BottomPeek++ + Select(&conflict.All[cur], g, false) case in == 'a': - conflicts[cur].Resolve(g, v, Local) + Resolve(&conflict.All[cur], g, v, Local) case in == 'd': - conflicts[cur].Resolve(g, v, Incoming) + Resolve(&conflict.All[cur], g, v, Incoming) case in == 'h' || in == '?': - conflicts[cur].Select(g, true) + Select(&conflict.All[cur], g, true) case in == 'q': globalQuit(g) case in == 'z': - conflicts[cur].toggleDiff() - conflicts[cur].Select(g, false) + conflict.All[cur].ToggleDiff() + Select(&conflict.All[cur], g, false) default: - printPrompt(g, Red(Regular, "[wasd] >>")) + PrintPrompt(g, color.Red(color.Regular, "[wasd] >>")) consecutiveError++ } if consecutiveError == 2 { consecutiveError = 0 - conflicts[cur].Select(g, true) + Select(&conflict.All[cur], g, true) } } @@ -71,7 +71,7 @@ func parseInput(g *gocui.Gui, v *gocui.View) error { if len(in) > 1 { for _, r := range [...]rune{'a', 'd', 'h', 'z'} { if strings.ContainsRune(in, r) { - printPrompt(g, Red(Regular, "[wasd] >>")) + PrintPrompt(g, color.Red(color.Regular, "[wasd] >>")) return nil } } @@ -84,18 +84,15 @@ func parseInput(g *gocui.Gui, v *gocui.View) error { } func main() { - var err error - conflicts, err = FindConflicts() - if err != nil { + if err := conflict.Find(); err != nil { switch err.(type) { - case *ErrNoConflict: - fmt.Println(Green(Regular, err.Error())) + case *conflict.ErrNoConflict: + fmt.Println(color.Green(color.Regular, err.Error())) default: - fmt.Print(Red(Regular, err.Error())) + fmt.Print(color.Red(color.Regular, err.Error())) } return } - conflictCount = len(conflicts) g, err := gocui.NewGui(gocui.OutputNormal) if err != nil { @@ -111,7 +108,7 @@ func main() { log.Panic(err) } - conflicts[0].Select(g, false) + Select(&conflict.All[0], g, false) if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { log.Panicln(err) @@ -119,9 +116,9 @@ func main() { g.Close() - for fname := range allFileLines { + for fname := range conflict.FileLines { if err := FinalizeChanges(fname); err != nil { - fmt.Println(Red(Highlight, "%s\n", err)) + fmt.Println(color.Red(color.Underline, "%s\n", err)) } } printSummary() @@ -36,7 +36,7 @@ func globalQuit(g *gocui.Gui) { }) } -func printPrompt(g *gocui.Gui, str string) { +func PrintPrompt(g *gocui.Gui, str string) { g.Update(func(g *gocui.Gui) error { v, err := g.View(Prompt) if err != nil { @@ -7,6 +7,8 @@ import ( "os" "github.com/jroimartin/gocui" + "github.com/mkchoi212/fac/color" + "github.com/mkchoi212/fac/conflict" ) func printHelp(v *gocui.View) { @@ -23,44 +25,35 @@ func printHelp(v *gocui.View) { h | ? - print help q | Ctrl+c - quit ` - fmt.Fprintf(v, Blue(Regular, instruction)) + fmt.Fprintf(v, color.Blue(color.Regular, instruction)) } func printSummary() { resolvedCnt := 0 var line string - for _, c := range conflicts { + for _, c := range conflict.All { if c.Choice != 0 { - line = Green(Regular, "✔ %s: %d", c.FileName, c.Start) + line = color.Green(color.Regular, "✔ %s: %d", c.FileName, c.Start) resolvedCnt++ } else { - line = Red(Regular, "✘ %s: %d", c.FileName, c.Start) + line = color.Red(color.Regular, "✘ %s: %d", c.FileName, c.Start) } fmt.Println(line) } var buf bytes.Buffer - if resolvedCnt != len(conflicts) { + if resolvedCnt != conflict.Count { buf.WriteString("\nResolved ") - buf.WriteString(Red(Light, "%d ", resolvedCnt)) + buf.WriteString(color.Red(color.Light, "%d ", resolvedCnt)) buf.WriteString("conflict(s) out of ") - buf.WriteString(Red(Light, "%d", len(conflicts))) + buf.WriteString(color.Red(color.Light, "%d", conflict.Count)) } else { - buf.WriteString(Green(Regular, "\nFixed All Conflicts 🎉")) + buf.WriteString(color.Green(color.Regular, "\nFixed All Conflicts 🎉")) } fmt.Println(buf.String()) } -func conflictsIn(fname string) (list []Conflict) { - for _, c := range conflicts { - if c.AbsolutePath == fname && c.Choice != 0 { - list = append(list, c) - } - } - return -} - func writeChanges(absPath string) (err error) { f, err := os.Create(absPath) if err != nil { @@ -69,7 +62,7 @@ func writeChanges(absPath string) (err error) { defer f.Close() w := bufio.NewWriter(f) - for _, line := range allFileLines[absPath] { + for _, line := range conflict.FileLines[absPath] { if _, err = w.WriteString(line); err != nil { return } @@ -79,7 +72,7 @@ func writeChanges(absPath string) (err error) { } func FinalizeChanges(absPath string) (err error) { - targetConflicts := conflictsIn(absPath) + targetConflicts := conflict.In(absPath) var replacementLines []string @@ -92,10 +85,10 @@ func FinalizeChanges(absPath string) (err error) { i := 0 for ; i < len(replacementLines); i++ { - allFileLines[absPath][c.Start+i-1] = replacementLines[i] + conflict.FileLines[absPath][c.Start+i-1] = replacementLines[i] } for ; c.End-c.Start >= i; i++ { - allFileLines[absPath][c.Start+i-1] = "" + conflict.FileLines[absPath][c.Start+i-1] = "" } } |