diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-02-25 20:55:36 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-02-25 21:21:07 +1100 |
commit | f94d0be2c9b0eaaf539d2cdcfaf258f754162a2e (patch) | |
tree | 0fa95e6348ca35d0938d263b007d9c471a09731e /pkg/utils | |
parent | 9fd9fd6816925debe64aa21269cdba5ec74ed5e9 (diff) |
refactor the way we render lists
Diffstat (limited to 'pkg/utils')
-rw-r--r-- | pkg/utils/utils.go | 51 | ||||
-rw-r--r-- | pkg/utils/utils_test.go | 158 |
2 files changed, 0 insertions, 209 deletions
diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 7fa33e769..f42ee130c 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -6,13 +6,10 @@ import ( "log" "os" "path/filepath" - "reflect" "regexp" "strings" "time" - "github.com/go-errors/errors" - "github.com/fatih/color" ) @@ -114,46 +111,6 @@ func Min(x, y int) int { return y } -type Displayable interface { - 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{}, isFocused bool) (string, error) { - s := reflect.ValueOf(slice) - if s.Kind() != reflect.Slice { - return "", errors.New("RenderList given a non-slice type") - } - - displayables := make([]Displayable, s.Len()) - - for i := 0; i < s.Len(); i++ { - value, ok := s.Index(i).Interface().(Displayable) - if !ok { - return "", errors.New("item does not implement the Displayable interface") - } - displayables[i] = value - } - - 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, isFocused bool) (string, error) { - if len(items) == 0 { - return "", nil - } - - stringArrays := getDisplayStringArrays(items, isFocused) - - return RenderDisplayStrings(stringArrays), nil -} - func RenderDisplayStrings(displayStringsArr [][]string) string { padWidths := getPadWidths(displayStringsArr) paddedDisplayStrings := getPaddedDisplayStrings(displayStringsArr, padWidths) @@ -220,14 +177,6 @@ func displayArraysAligned(stringArrays [][]string) bool { return true } -func getDisplayStringArrays(displayables []Displayable, isFocused bool) [][]string { - stringArrays := make([][]string, len(displayables)) - for i, item := range displayables { - stringArrays[i] = item.GetDisplayStrings(isFocused) - } - return stringArrays -} - // IncludesString if the list contains the string func IncludesString(list []string, a string) bool { for _, b := range list { diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 7cf8e958b..82df3e94a 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -196,166 +196,8 @@ func TestDisplayArraysAligned(t *testing.T) { } } -type myDisplayable struct { - strings []string -} - type myStruct struct{} -// GetDisplayStrings is a function. -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 - isFocused bool - expected [][]string - } - - scenarios := []scenario{ - { - []Displayable{ - 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, s.isFocused)) - } -} - -// TestRenderDisplayableList is a function. -func TestRenderDisplayableList(t *testing.T) { - type scenario struct { - input []Displayable - isFocused bool - expectedString string - expectedErrorMessage string - } - - scenarios := []scenario{ - { - []Displayable{ - Displayable(&myDisplayable{[]string{}}), - Displayable(&myDisplayable{[]string{}}), - }, - false, - "\n", - "", - }, - { - []Displayable{ - Displayable(&myDisplayable{[]string{"aa", "b"}}), - Displayable(&myDisplayable{[]string{"c", "d"}}), - }, - false, - "aa b\nc d", - "", - }, - { - []Displayable{ - Displayable(&myDisplayable{[]string{"a"}}), - Displayable(&myDisplayable{[]string{"b", "c"}}), - }, - false, - "a \nb c", - "", - }, - { - []Displayable{ - Displayable(&myDisplayable{[]string{"a"}}), - Displayable(&myDisplayable{[]string{"b"}}), - }, - true, - "a blah\nb blah", - "", - }, - } - - for _, s := range scenarios { - str, err := renderDisplayableList(s.input, s.isFocused) - assert.EqualValues(t, s.expectedString, str) - if s.expectedErrorMessage != "" { - assert.EqualError(t, err, s.expectedErrorMessage) - } else { - assert.NoError(t, err) - } - } -} - -// TestRenderList is a function. -func TestRenderList(t *testing.T) { - type scenario struct { - input interface{} - isFocused bool - expectedString string - expectedErrorMessage string - } - - scenarios := []scenario{ - { - []*myDisplayable{ - {[]string{"aa", "b"}}, - {[]string{"c", "d"}}, - }, - false, - "aa b\nc d", - "", - }, - { - []*myStruct{ - {}, - {}, - }, - 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, s.isFocused) - assert.EqualValues(t, s.expectedString, str) - if s.expectedErrorMessage != "" { - assert.EqualError(t, err, s.expectedErrorMessage) - } else { - assert.NoError(t, err) - } - } -} - // TestGetPaddedDisplayStrings is a function. func TestGetPaddedDisplayStrings(t *testing.T) { type scenario struct { |