Skip to content

Commit 7857dc9

Browse files
Rewrote Tests to be more specific
- rewrote tests - Move exampleSuffixer to suffixer.go changed other files accordingly - removed redundant Border checks within Lines() - changed DefaultPrefixer to mark wraped line if selected
1 parent c416b54 commit 7857dc9

File tree

5 files changed

+186
-348
lines changed

5 files changed

+186
-348
lines changed

list/example/main.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"github.com/charmbracelet/bubbles/list"
77
tea "github.com/charmbracelet/bubbletea"
8-
"github.com/muesli/reflow/ansi"
98
"os"
109
"strconv"
1110
"strings"
@@ -46,16 +45,16 @@ func main() {
4645
stringerList := list.MakeStringerList(itemList)
4746

4847
endResult := make(chan string, 1)
49-
list := list.NewModel()
50-
list.AddItems(stringerList)
51-
list.SuffixGen = &exampleSuffixer{currentMarker: "<"}
48+
l := list.NewModel()
49+
l.AddItems(stringerList)
50+
l.SuffixGen = list.NewSuffixer()
5251

5352
// Since in this example we only use UNIQUE string items we can use a String Comparison for the equals methode
5453
// but be aware that different items in your case can have the same string -> false-positiv
5554
// Better: Assert back to your struct and test on something unique within it!
56-
list.SetEquals(func(first, second fmt.Stringer) bool { return first.String() == second.String() })
55+
l.SetEquals(func(first, second fmt.Stringer) bool { return first.String() == second.String() })
5756
m := model{}
58-
m.list = list
57+
m.list = l
5958

6059
m.endResult = endResult
6160

@@ -100,7 +99,7 @@ func (m model) View() string {
10099
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
101100
if m.list.PrefixGen == nil {
102101
// use default
103-
m.list.PrefixGen = list.NewDefault()
102+
m.list.PrefixGen = list.NewPrefixer()
104103
}
105104

106105
switch msg := msg.(type) {
@@ -213,22 +212,3 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
213212
}
214213
return m, nil
215214
}
216-
217-
type exampleSuffixer struct {
218-
viewPos list.ViewPos
219-
currentMarker string
220-
markerLenght int
221-
}
222-
223-
func (e *exampleSuffixer) InitSuffixer(viewPos list.ViewPos, screen list.ScreenInfo) int {
224-
e.viewPos = viewPos
225-
e.markerLenght = ansi.PrintableRuneWidth(e.currentMarker)
226-
return e.markerLenght
227-
}
228-
229-
func (e *exampleSuffixer) Suffix(item, line int, selected bool) string {
230-
if item == e.viewPos.Cursor && line == 0 {
231-
return e.currentMarker
232-
}
233-
return strings.Repeat(" ", e.markerLenght)
234-
}

list/list.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ func (m *Model) Lines() []string {
108108
out:
109109
// Handle list items, start at first visible and go till end of list or visible (break)
110110
for index := offset; index < len(m.listItems); index++ {
111-
if index >= len(m.listItems) || index < 0 {
112-
// TODO log error
113-
break
114-
}
115-
116111
item := m.listItems[index]
117112

118113
lines := m.itemLines(item)
@@ -161,10 +156,6 @@ out:
161156
}
162157
}
163158
}
164-
lenght := len(stringLines)
165-
if lenght > m.Screen.Height {
166-
panic(fmt.Sprintf("can't display %d lines when screen has %d lines.", lenght, m.Screen.Height))
167-
}
168159
return stringLines
169160
}
170161

0 commit comments

Comments
 (0)