11package optionsProvider
22
33import (
4+ "context"
5+
46 "github.com/charmbracelet/bubbles/list"
57 tea "github.com/charmbracelet/bubbletea"
68)
@@ -13,28 +15,40 @@ type OptionsProvider struct {
1315}
1416
1517type Option struct {
16- title string
17- value string
18- description string
18+ Name string
19+ ID int64
20+ Subtext string
1921}
2022
21- func (o Option ) Title () string { return o .title }
22- func (o Option ) Description () string { return o .description }
23- func (o Option ) Value () string { return o .value }
24- func (o Option ) FilterValue () string { return o .title }
23+ func (o Option ) Title () string { return o .Name }
24+ func (o Option ) Description () string { return o .Subtext }
25+ func (o Option ) Value () int64 { return o .ID }
26+ func (o Option ) FilterValue () string { return o .Name }
2527
2628func (o OptionsProvider ) Init () tea.Cmd {
2729 return nil
2830}
2931
3032func (o OptionsProvider ) Update (msg tea.Msg ) (OptionsProvider , tea.Cmd ) {
33+ var cmd tea.Cmd
34+ var cmds []tea.Cmd
3135 switch msg := msg .(type ) {
3236 case tea.WindowSizeMsg :
3337 o .height = msg .Height
3438 o .width = msg .Width
3539 o .list .SetSize (o .list .Width (), o .height )
40+ case tea.KeyMsg :
41+
42+ o .list , cmd = o .list .Update (msg )
43+ cmds = append (cmds , cmd )
44+
45+ switch msg .String () {
46+
47+ default :
48+ }
49+
3650 }
37- return o , nil
51+ return o , tea . Batch ( cmds ... )
3852}
3953
4054func (o OptionsProvider ) View () string {
@@ -49,11 +63,21 @@ func (o OptionsProvider) OnBlur() {
4963
5064}
5165
52- func initList [T , C any ](config * ListConfig [T , C ]) list.Model {
66+ func (o OptionsProvider ) GetSelected () Option {
67+ return o .list .SelectedItem ().(Option )
68+ }
69+
70+ func initList [T , U any ](config * ListConfig [T , U ]) list.Model {
71+
72+ rawItems , err := config .CrudOps .List (context .Background ())
73+
74+ if err != nil {
75+ rawItems = []T {}
76+ }
5377
54- // items := config.ItemMapper(config.Items )
78+ items := config .ItemMapper (rawItems )
5579
56- list := list .New ([]list. Item {} , list .NewDefaultDelegate (), 30 , 0 )
80+ list := list .New (items , list .NewDefaultDelegate (), 30 , 30 )
5781
5882 // list configuration
5983 list .SetFilteringEnabled (config .FilteringEnabled )
@@ -67,7 +91,7 @@ func initList[T, C any](config *ListConfig[T, C]) list.Model {
6791 return list
6892}
6993
70- func NewOptionsProvider [T , C any ](config * ListConfig [T , C ]) OptionsProvider {
94+ func NewOptionsProvider [T , U any ](config * ListConfig [T , U ]) OptionsProvider {
7195 return OptionsProvider {
7296 list : initList (config ),
7397 // onSelectAction: config.OnSelectAction,
0 commit comments