selector.go

 1package selector
 2
 3import (
 4	"github.com/charmbracelet/bubbles/list"
 5	tea "github.com/charmbracelet/bubbletea"
 6	"github.com/charmbracelet/soft-serve/ui/common"
 7)
 8
 9type Selector struct {
10	list   list.Model
11	common common.Common
12}
13
14func New(common common.Common, items []list.Item) *Selector {
15	l := list.New(items, ItemDelegate{common.Styles}, common.Width, common.Height)
16	l.SetShowTitle(false)
17	l.SetShowHelp(false)
18	l.SetShowStatusBar(false)
19	l.DisableQuitKeybindings()
20	s := &Selector{
21		list:   l,
22		common: common,
23	}
24	s.SetSize(common.Width, common.Height)
25	return s
26}
27
28func (s *Selector) KeyMap() list.KeyMap {
29	return s.list.KeyMap
30}
31
32func (s *Selector) SetSize(width, height int) {
33	s.common.SetSize(width, height)
34	s.list.SetSize(width, height)
35}
36
37func (s *Selector) SetItems(items []list.Item) tea.Cmd {
38	return s.list.SetItems(items)
39}
40
41func (s *Selector) Init() tea.Cmd {
42	return nil
43}
44
45func (s *Selector) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
46	cmds := make([]tea.Cmd, 0)
47	switch msg := msg.(type) {
48	default:
49		m, cmd := s.list.Update(msg)
50		s.list = m
51		if cmd != nil {
52			cmds = append(cmds, cmd)
53		}
54	}
55	return s, tea.Batch(cmds...)
56}
57
58func (s *Selector) View() string {
59	return s.list.View()
60}