Skip to content

Commit

Permalink
Update comments on functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed May 12, 2018
1 parent ecba69c commit d8fe975
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 56 deletions.
12 changes: 6 additions & 6 deletions color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,32 @@ func colorString(color int, style int, format string, a ...interface{}) string {
return fmt.Sprintf("\033[3%d;%dm%s\033[0m", color, style, str)
}

// Black is a convenient helper function to return a string with black foreground
// Black returns a string with black foreground
func Black(style int, format string, a ...interface{}) string {
return colorString(FgBlack, style, format, a...)
}

// Red is a convenient helper function to return a string with red foreground
// Red returns a string with red foreground
func Red(style int, format string, a ...interface{}) string {
return colorString(FgRed, style, format, a...)
}

// Green is a convenient helper function to return a string with green foreground
// Green returns a string with green foreground
func Green(style int, format string, a ...interface{}) string {
return colorString(FgGreen, style, format, a...)
}

// Yellow is a convenient helper function to return a string with yellow foreground
// Yellow returns a string with yellow foreground
func Yellow(style int, format string, a ...interface{}) string {
return colorString(FgYellow, style, format, a...)
}

// Blue is a convenient helper function to return a string with blue foreground
// Blue returns a string with blue foreground
func Blue(style int, format string, a ...interface{}) string {
return colorString(FgBlue, style, format, a...)
}

// Purple is a convenient helper function to return a string with purple foreground
// Purple returns a string with purple foreground
func Purple(style int, format string, a ...interface{}) string {
return colorString(FgPurple, style, format, a...)
}
12 changes: 6 additions & 6 deletions conflict/conflict.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ type Conflict struct {
DisplayDiff bool
}

var All = []Conflict{}
var Count int

// ErrNoConflict is used to indicate that there
// are no errors present in the git repo
type ErrNoConflict struct {
message string
}
Expand Down Expand Up @@ -77,10 +76,11 @@ func (c *Conflict) PaddingLines() (topPadding, bottomPadding []string) {
return
}

func In(fname string) (list []Conflict) {
for _, c := range All {
// In finds `Conflict`s that are from the provided file name
func In(conflicts []Conflict, fname string) (res []Conflict) {
for _, c := range conflicts {
if c.AbsolutePath == fname && c.Choice != 0 {
list = append(list, c)
res = append(res, c)
}
}
return
Expand Down
46 changes: 24 additions & 22 deletions conflict/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,26 @@ Run below command to change to a compatible conflict style
return parsedConflicts, nil
}

func Find() (err error) {
cwd, _ := os.Getwd()
// Find runs `git --no-pager diff --check` in order to detect git conflicts
// If there are no conflicts, it returns a `ErrNoConflict`
// If there are conflicts, it parses the corresponding files
func Find(cwd string) ([]Conflict, error) {
conflicts := []Conflict{}

stdout, stderr, _ := RunCommand("git", cwd, "rev-parse", "--show-toplevel")
if len(stderr) != 0 {
return errors.New(stderr)
return nil, errors.New(stderr)
} else if len(stdout) == 0 {
return errors.New("no git top-level path")
return nil, errors.New(stderr)
}
topLevelPath := string(strings.Split(stdout, "\n")[0])

stdout, stderr, _ = RunCommand("git", cwd, "--no-pager", "diff", "--check")

if len(stderr) != 0 {
return errors.New(stderr)
return nil, errors.New(stderr)
} else if len(stdout) == 0 {
return NewErrNoConflict("No conflicts detected 🎉")
return nil, NewErrNoConflict("No conflicts detected 🎉")
}

stdoutLines := strings.Split(stdout, "\n")
Expand All @@ -175,36 +178,35 @@ func Find() (err error) {
continue
}

if err = parseRawOutput(line, diffMap); err != nil {
return
if err := parseRawOutput(line, diffMap); err != nil {
return nil, err
}
}

for fname := range diffMap {
absPath := path.Join(topLevelPath, fname)
if err = ReadFile(absPath); err != nil {
return
if err := ReadFile(absPath); err != nil {
return nil, err
}
if conflicts, err := New(absPath, diffMap[fname]); err == nil {
All = append(All, conflicts...)
if newConflicts, err := New(absPath, diffMap[fname]); err == nil {
conflicts = append(conflicts, newConflicts...)
} else {
return err
return nil, err
}
}

Count = len(All)
if Count == 0 {
return NewErrNoConflict("No conflicts detected 🎉")
if len(conflicts) == 0 {
return nil, NewErrNoConflict("No conflicts detected 🎉")
}

for i := range All {
if err = All[i].ExtractLines(); err != nil {
return
for i := range conflicts {
if err := conflicts[i].ExtractLines(); err != nil {
return nil, err
}
if err = All[i].HighlightSyntax(); err != nil {
return
if err := conflicts[i].HighlightSyntax(); err != nil {
return nil, err
}
}

return nil
return conflicts, nil
}
12 changes: 6 additions & 6 deletions layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func Select(c *conflict.Conflict, g *gocui.Gui, showHelp bool) error {
}
v.Clear()

for idx, conflict := range conflict.All {
for idx, conflict := range all {
var out string
if conflict.Choice != 0 {
out = color.Green(color.Regular, "✔ %s:%d", conflict.FileName, conflict.Start)
Expand Down Expand Up @@ -223,22 +223,22 @@ func MoveToItem(dir int, g *gocui.Gui, v *gocui.View) error {
cur++
}

if cur >= conflict.Count {
if cur >= numConflicts {
cur = 0
} else if cur < 0 {
cur = conflict.Count - 1
cur = numConflicts - 1
}

if conflict.All[cur].Choice == 0 || originalCur == cur {
if all[cur].Choice == 0 || originalCur == cur {
break
}
}

if originalCur == cur && conflict.All[cur].Choice != 0 {
if originalCur == cur && all[cur].Choice != 0 {
globalQuit(g)
}

Select(&conflict.All[cur], g, false)
Select(&all[cur], g, false)
return nil
}

Expand Down
32 changes: 20 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"fmt"
"log"
"os"
"strings"

"github.com/jroimartin/gocui"
Expand All @@ -17,6 +18,8 @@ import (
var (
cur = 0
consecutiveError = 0
all = []conflict.Conflict{}
numConflicts = 0
)

func printLines(v *gocui.View, lines []string) {
Expand All @@ -33,19 +36,19 @@ func parseInput(g *gocui.Gui, v *gocui.View) error {
evalCmd := func(in rune, g *gocui.Gui) {
switch in {
case 'j':
Scroll(g, &conflict.All[cur], Up)
Scroll(g, &all[cur], Up)
case 'k':
Scroll(g, &conflict.All[cur], Down)
Scroll(g, &all[cur], Down)
case 'w':
conflict.All[cur].TopPeek++
Select(&conflict.All[cur], g, false)
all[cur].TopPeek++
Select(&all[cur], g, false)
case 's':
conflict.All[cur].BottomPeek++
Select(&conflict.All[cur], g, false)
all[cur].BottomPeek++
Select(&all[cur], g, false)
case 'a':
Resolve(&conflict.All[cur], g, v, Local)
Resolve(&all[cur], g, v, Local)
case 'd':
Resolve(&conflict.All[cur], g, v, Incoming)
Resolve(&all[cur], g, v, Incoming)
case 'n':
MoveToItem(Down, g, v)
case 'p':
Expand All @@ -54,15 +57,15 @@ func parseInput(g *gocui.Gui, v *gocui.View) error {
ViewOrientation = ^ViewOrientation
layout(g)
case 'h', '?':
Select(&conflict.All[cur], g, true)
Select(&all[cur], g, true)
case 'q':
globalQuit(g)
default:
PrintPrompt(g, color.Red(color.Regular, promptString))
consecutiveError++
if consecutiveError == 2 {
consecutiveError = 0
Select(&conflict.All[cur], g, true)
Select(&all[cur], g, true)
}
return
}
Expand Down Expand Up @@ -90,7 +93,9 @@ func parseInput(g *gocui.Gui, v *gocui.View) error {
}

func main() {
if err := conflict.Find(); err != nil {
cwd, _ := os.Getwd()
conflicts, err := conflict.Find(cwd)
if err != nil {
switch err.(type) {
case *conflict.ErrNoConflict:
fmt.Println(color.Green(color.Regular, err.Error()))
Expand All @@ -100,6 +105,9 @@ func main() {
return
}

all = conflicts
numConflicts = len(conflicts)

g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Panicln(err)
Expand All @@ -114,7 +122,7 @@ func main() {
log.Panic(err)
}

Select(&conflict.All[0], g, false)
Select(&all[0], g, false)

if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
Expand Down
8 changes: 4 additions & 4 deletions summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func printSummary() {
resolvedCnt := 0
var line string

for _, c := range conflict.All {
for _, c := range all {
if c.Choice != 0 {
line = color.Green(color.Regular, "✔ %s: %d", c.FileName, c.Start)
resolvedCnt++
Expand All @@ -46,11 +46,11 @@ func printSummary() {
}

var buf bytes.Buffer
if resolvedCnt != conflict.Count {
if resolvedCnt != numConflicts {
buf.WriteString("\nResolved ")
buf.WriteString(color.Red(color.Light, "%d ", resolvedCnt))
buf.WriteString("conflict(s) out of ")
buf.WriteString(color.Red(color.Light, "%d", conflict.Count))
buf.WriteString(color.Red(color.Light, "%d", numConflicts))
} else {
buf.WriteString(color.Green(color.Regular, "\nFixed All Conflicts 🎉"))
}
Expand All @@ -75,7 +75,7 @@ func writeChanges(absPath string) (err error) {
}

func FinalizeChanges(absPath string) (err error) {
targetConflicts := conflict.In(absPath)
targetConflicts := conflict.In(all, absPath)

var replacementLines []string

Expand Down

0 comments on commit d8fe975

Please sign in to comment.