mirror of
https://github.com/zoriya/bubbles.git
synced 2026-08-16 01:43:21 +00:00
chore(list): rework New() function to use functional arguments
This commit is contained in:
+30
-5
@@ -179,8 +179,29 @@ type Model struct {
|
|||||||
delegate ItemDelegate
|
delegate ItemDelegate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ListOption func(*Model)
|
||||||
|
|
||||||
|
func WithSize(width, height int) ListOption {
|
||||||
|
return func(m *Model) {
|
||||||
|
m.width = width
|
||||||
|
m.height = height
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithItems(i ...Item) ListOption {
|
||||||
|
return func(m *Model) {
|
||||||
|
m.items = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithDelegate(d ItemDelegate) ListOption {
|
||||||
|
return func(m *Model) {
|
||||||
|
m.delegate = d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// New returns a new model with sensible defaults.
|
// New returns a new model with sensible defaults.
|
||||||
func New(items []Item, delegate ItemDelegate, width, height int) Model {
|
func New(opts ...ListOption) Model {
|
||||||
styles := DefaultStyles()
|
styles := DefaultStyles()
|
||||||
|
|
||||||
sp := spinner.New()
|
sp := spinner.New()
|
||||||
@@ -215,15 +236,19 @@ func New(items []Item, delegate ItemDelegate, width, height int) Model {
|
|||||||
FilterInput: filterInput,
|
FilterInput: filterInput,
|
||||||
StatusMessageLifetime: time.Second,
|
StatusMessageLifetime: time.Second,
|
||||||
|
|
||||||
width: width,
|
|
||||||
height: height,
|
|
||||||
delegate: delegate,
|
|
||||||
items: items,
|
|
||||||
Paginator: p,
|
Paginator: p,
|
||||||
spinner: sp,
|
spinner: sp,
|
||||||
Help: help.New(),
|
Help: help.New(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&m)
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.delegate == nil {
|
||||||
|
m.delegate = NewDefaultDelegate()
|
||||||
|
}
|
||||||
|
|
||||||
m.updatePagination()
|
m.updatePagination()
|
||||||
m.updateKeybindings()
|
m.updateKeybindings()
|
||||||
return m
|
return m
|
||||||
|
|||||||
Reference in New Issue
Block a user