summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAbhijeet Rastogi <arastogi@linkedin.com>2018-09-05 20:41:29 -0700
committerAbhijeet Rastogi <arastogi@linkedin.com>2018-09-07 20:59:14 -0700
commit4c08789c12d532c26cc95d1512dbb677cdedbf5a (patch)
treeaed205e115e2b24965e79468586d9757a5648c73
parent60d58b01a7c316ef3ffdc41bd76854734095c391 (diff)
Issue: #44: Add option to set default peeks aka context lines
-rw-r--r--binding/binding.go2
-rw-r--r--conflict/conflict.go17
-rw-r--r--conflict/conflict_test.go24
-rw-r--r--main.go7
4 files changed, 49 insertions, 1 deletions
diff --git a/binding/binding.go b/binding/binding.go
index 8df142e..dab92f7 100644
--- a/binding/binding.go
+++ b/binding/binding.go
@@ -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
@@ -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`
diff --git a/conflict/conflict.go b/conflict/conflict.go
index facc8ad..f6a11c2 100644
--- a/conflict/conflict.go
+++ b/conflict/conflict.go
@@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"sort"
+ "strconv"
"strings"
"github.com/alecthomas/chroma"
@@ -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) {
diff --git a/conflict/conflict_test.go b/conflict/conflict_test.go
index 1fb26dd..ff5faae 100644
--- a/conflict/conflict_test.go
+++ b/conflict/conflict_test.go
@@ -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{
diff --git a/main.go b/main.go
index 902a0aa..0a63b4c 100644
--- a/main.go
+++ b/main.go
@@ -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)
}
}