summaryrefslogtreecommitdiffstats
path: root/pkg/utils
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2019-02-16 15:17:44 +1100
committerJesse Duffield <jessedduffield@gmail.com>2019-02-16 15:17:44 +1100
commitad93b4c863dfaa6a1cb6bb740d0dba87fef14404 (patch)
tree125d6bf54cdbf582d689cfe62e630866d7fc2a8e /pkg/utils
parent198cbee498e9201b1f12514f909eb3da98db5ec7 (diff)
consider whether the view has focus when rendering the contents of a view
Diffstat (limited to 'pkg/utils')
-rw-r--r--pkg/utils/utils.go14
-rw-r--r--pkg/utils/utils_test.go50
2 files changed, 51 insertions, 13 deletions
diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go
index 60985dd0b..417823b84 100644
--- a/pkg/utils/utils.go
+++ b/pkg/utils/utils.go
@@ -113,13 +113,13 @@ func Min(x, y int) int {
}
type Displayable interface {
- GetDisplayStrings() []string
+ GetDisplayStrings(bool) []string
}
// RenderList takes a slice of items, confirms they implement the Displayable
// interface, then generates a list of their displaystrings to write to a panel's
// buffer
-func RenderList(slice interface{}) (string, error) {
+func RenderList(slice interface{}, isFocused bool) (string, error) {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
return "", errors.New("RenderList given a non-slice type")
@@ -135,19 +135,19 @@ func RenderList(slice interface{}) (string, error) {
displayables[i] = value
}
- return renderDisplayableList(displayables)
+ return renderDisplayableList(displayables, isFocused)
}
// renderDisplayableList takes a list of displayable items, obtains their display
// strings via GetDisplayStrings() and then returns a single string containing
// each item's string representation on its own line, with appropriate horizontal
// padding between the item's own strings
-func renderDisplayableList(items []Displayable) (string, error) {
+func renderDisplayableList(items []Displayable, isFocused bool) (string, error) {
if len(items) == 0 {
return "", nil
}
- stringArrays := getDisplayStringArrays(items)
+ stringArrays := getDisplayStringArrays(items, isFocused)
if !displayArraysAligned(stringArrays) {
return "", errors.New("Each item must return the same number of strings to display")
@@ -199,10 +199,10 @@ func displayArraysAligned(stringArrays [][]string) bool {
return true
}
-func getDisplayStringArrays(displayables []Displayable) [][]string {
+func getDisplayStringArrays(displayables []Displayable, isFocused bool) [][]string {
stringArrays := make([][]string, len(displayables))
for i, item := range displayables {
- stringArrays[i] = item.GetDisplayStrings()
+ stringArrays[i] = item.GetDisplayStrings(isFocused)
}
return stringArrays
}
diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go
index ee502c1f8..1e232272f 100644
--- a/pkg/utils/utils_test.go
+++ b/pkg/utils/utils_test.go
@@ -203,15 +203,19 @@ type myDisplayable struct {
type myStruct struct{}
// GetDisplayStrings is a function.
-func (d *myDisplayable) GetDisplayStrings() []string {
+func (d *myDisplayable) GetDisplayStrings(isFocused bool) []string {
+ if isFocused {
+ return append(d.strings, "blah")
+ }
return d.strings
}
// TestGetDisplayStringArrays is a function.
func TestGetDisplayStringArrays(t *testing.T) {
type scenario struct {
- input []Displayable
- expected [][]string
+ input []Displayable
+ isFocused bool
+ expected [][]string
}
scenarios := []scenario{
@@ -220,12 +224,21 @@ func TestGetDisplayStringArrays(t *testing.T) {
Displayable(&myDisplayable{[]string{"a", "b"}}),
Displayable(&myDisplayable{[]string{"c", "d"}}),
},
+ false,
[][]string{{"a", "b"}, {"c", "d"}},
},
+ {
+ []Displayable{
+ Displayable(&myDisplayable{[]string{"a", "b"}}),
+ Displayable(&myDisplayable{[]string{"c", "d"}}),
+ },
+ true,
+ [][]string{{"a", "b", "blah"}, {"c", "d", "blah"}},
+ },
}
for _, s := range scenarios {
- assert.EqualValues(t, s.expected, getDisplayStringArrays(s.input))
+ assert.EqualValues(t, s.expected, getDisplayStringArrays(s.input, s.isFocused))
}
}
@@ -233,6 +246,7 @@ func TestGetDisplayStringArrays(t *testing.T) {
func TestRenderDisplayableList(t *testing.T) {
type scenario struct {
input []Displayable
+ isFocused bool
expectedString string
expectedErrorMessage string
}
@@ -243,6 +257,7 @@ func TestRenderDisplayableList(t *testing.T) {
Displayable(&myDisplayable{[]string{}}),
Displayable(&myDisplayable{[]string{}}),
},
+ false,
"\n",
"",
},
@@ -251,6 +266,7 @@ func TestRenderDisplayableList(t *testing.T) {
Displayable(&myDisplayable{[]string{"aa", "b"}}),
Displayable(&myDisplayable{[]string{"c", "d"}}),
},
+ false,
"aa b\nc d",
"",
},
@@ -259,13 +275,23 @@ func TestRenderDisplayableList(t *testing.T) {
Displayable(&myDisplayable{[]string{"a"}}),
Displayable(&myDisplayable{[]string{"b", "c"}}),
},
+ false,
"",
"Each item must return the same number of strings to display",
},
+ {
+ []Displayable{
+ Displayable(&myDisplayable{[]string{"a"}}),
+ Displayable(&myDisplayable{[]string{"b"}}),
+ },
+ true,
+ "a blah\nb blah",
+ "",
+ },
}
for _, s := range scenarios {
- str, err := renderDisplayableList(s.input)
+ str, err := renderDisplayableList(s.input, s.isFocused)
assert.EqualValues(t, s.expectedString, str)
if s.expectedErrorMessage != "" {
assert.EqualError(t, err, s.expectedErrorMessage)
@@ -279,6 +305,7 @@ func TestRenderDisplayableList(t *testing.T) {
func TestRenderList(t *testing.T) {
type scenario struct {
input interface{}
+ isFocused bool
expectedString string
expectedErrorMessage string
}
@@ -289,6 +316,7 @@ func TestRenderList(t *testing.T) {
{[]string{"aa", "b"}},
{[]string{"c", "d"}},
},
+ false,
"aa b\nc d",
"",
},
@@ -297,18 +325,28 @@ func TestRenderList(t *testing.T) {
{},
{},
},
+ false,
"",
"item does not implement the Displayable interface",
},
{
&myStruct{},
+ false,
"",
"RenderList given a non-slice type",
},
+ {
+ []*myDisplayable{
+ {[]string{"a"}},
+ },
+ true,
+ "a blah",
+ "",
+ },
}
for _, s := range scenarios {
- str, err := RenderList(s.input)
+ str, err := RenderList(s.input, s.isFocused)
assert.EqualValues(t, s.expectedString, str)
if s.expectedErrorMessage != "" {
assert.EqualError(t, err, s.expectedErrorMessage)