Skip to content

Commit

Permalink
Issue: #44: Add option to set default peeks aka context lines
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhijeet Rastogi committed Sep 8, 2018
1 parent 60d58b0 commit 4c08789
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
QuitApplication = "quit"
ShowHelp = "help"
ContinuousEvaluation = "cont_eval"
DefaultContextLines = "default_context_lines"
)

// defaultBinding is used when the user has not specified any of the
Expand All @@ -54,6 +55,7 @@ var defaultBinding = Binding{
QuitApplication: "q",
ShowHelp: "h",
ContinuousEvaluation: "false",
DefaultContextLines: "2",
}

// LoadSettings looks for a user specified key-binding settings file - `$HOME/.fac.yml`
Expand Down
17 changes: 17 additions & 0 deletions conflict/conflict.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"sort"
"strconv"
"strings"

"github.com/alecthomas/chroma"
Expand Down Expand Up @@ -98,6 +99,22 @@ func (c *Conflict) Update(incoming []string) (err error) {
return
}

// ContextLines sets the TopPeek and BottomPeek peek values
func (c *Conflict) SetContextLines(contextLines string) (err error) {
// Set context lines to show
peek, err := strconv.ParseInt(contextLines, 10, 32)
if err != nil {
return
}
if peek < 0 {
err = errors.New("Invalid context lines, expecting a positive number")
return
}
c.TopPeek = int(peek)
c.BottomPeek = int(peek)
return
}

// PaddingLines returns top and bottom padding lines based on
// `TopPeek` and `BottomPeek` values
func (c *Conflict) PaddingLines() (topPadding, bottomPadding []string) {
Expand Down
24 changes: 24 additions & 0 deletions conflict/conflict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ func TestEqual(t *testing.T) {
testhelper.Assert(t, !(c1.Equal(&c3)), "%s and %s should not be equal", c1, c2)
}

func TestSetContextLines(t *testing.T) {
var testCases = []struct {
in string
expected int
err bool
}{
{"2", 2, false},
{"0", 0, false},
{"-2", 0, true},
{"foo", 0, true},
}

c := Conflict{}

for _, tt := range testCases {
if err := c.SetContextLines(tt.in); err != nil {
testhelper.Assert(t, tt.err, "Didn't expect error to be returned, input: %s", tt.in)
}

testhelper.Assert(t, c.TopPeek == tt.expected, "TopPeek, expected: %s, returned: %s", tt.expected, c.TopPeek)
testhelper.Assert(t, c.BottomPeek == tt.expected, "BottomPeek, expected: %s, returned: %s", tt.expected, c.BottomPeek)
}
}

func TestPaddingLines(t *testing.T) {
f := File{Lines: dummyFile.lines}
c := Conflict{
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ func findConflicts() (files []conflict.File, err error) {
for i := range files {
file := &files[i]
for j := range file.Conflicts {
conflicts = append(conflicts, &file.Conflicts[j])
// Set context lines to show
c := &file.Conflicts[j]
if err = c.SetContextLines(keyBinding[binding.DefaultContextLines]); err != nil {
return
}
conflicts = append(conflicts, c)
}
}

Expand Down

0 comments on commit 4c08789

Please sign in to comment.