Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue: #44: (New feature) Add option to set default peeks aka context lines #45

Merged
merged 1 commit into from
Sep 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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