Skip to content

Commit

Permalink
Seperate conflict as seperate package
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed Jan 1, 2018
1 parent 2cbb1cd commit b11a1b9
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 286 deletions.
61 changes: 0 additions & 61 deletions color.go

This file was deleted.

193 changes: 0 additions & 193 deletions conflict.go

This file was deleted.

2 changes: 1 addition & 1 deletion command.go → conflict/command.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package conflict

import (
"bytes"
Expand Down
96 changes: 96 additions & 0 deletions conflict/conflict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package conflict

import (
"strings"

"github.com/mkchoi212/fac/color"
"github.com/sergi/go-diff/diffmatchpatch"
)

// Conflict represents a single conflict that may have occured
type Conflict struct {
Choice int
FileName string
AbsolutePath string
Start int
Middle int
End int

CurrentLines []string
ForeignLines []string
ColoredCurrentLines []string
ColoredForeignLines []string

CurrentName string
ForeignName string

TopPeek int
BottomPeek int
DisplayDiff bool
}

var All = []Conflict{}
var Count int

type ErrNoConflict struct {
message string
}

func NewErrNoConflict(message string) *ErrNoConflict {
return &ErrNoConflict{
message: message,
}
}

func (e *ErrNoConflict) Error() string {
return e.message
}

func (c *Conflict) Equal(c2 *Conflict) bool {
return c.FileName == c2.FileName && c.Start == c2.Start
}

func (c *Conflict) ToggleDiff() {
c.DisplayDiff = !(c.DisplayDiff)
}

func (c *Conflict) PaddingLines() (topPadding, bottomPadding []string) {
lines := FileLines[c.AbsolutePath]
start, end := c.Start-1, c.End

if c.TopPeek >= start {
c.TopPeek = start
} else if c.TopPeek < 0 {
c.TopPeek = 0
}

for _, l := range lines[start-c.TopPeek : start] {
topPadding = append(topPadding, color.Black(color.Regular, l))
}

if c.BottomPeek >= len(lines)-c.End {
c.BottomPeek = len(lines) - c.End
} else if c.BottomPeek < 0 {
c.BottomPeek = 0
}

for _, l := range lines[end : end+c.BottomPeek] {
bottomPadding = append(bottomPadding, color.Black(color.Regular, l))
}
return
}

func In(fname string) (list []Conflict) {
for _, c := range All {
if c.AbsolutePath == fname && c.Choice != 0 {
list = append(list, c)
}
}
return
}

func (c *Conflict) Diff() []string {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(strings.Join(c.CurrentLines, ""), strings.Join(c.ForeignLines, ""), false)
return []string{dmp.DiffPrettyText(diffs)}
}
Loading

0 comments on commit b11a1b9

Please sign in to comment.