Skip to content

Commit 765149b

Browse files
committed
changes:
- created a messages package for circulating messages - removed crud from the config and gave it access to only the list func - added generics to the OptionsProvider - styled the input and added functionality to add collections - created and input config - added some styling attributes - extracted some colors from the styles and made vars out of them
1 parent ba724d2 commit 765149b

8 files changed

Lines changed: 84 additions & 39 deletions

File tree

internal/tui/components/OptionsProvider/component.go

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
tea "github.com/charmbracelet/bubbletea"
99
"github.com/charmbracelet/lipgloss"
1010
"github.com/maniac-en/req/internal/tui/keybinds"
11+
"github.com/maniac-en/req/internal/tui/messages"
1112
)
1213

1314
type focusedComp string
@@ -17,13 +18,15 @@ const (
1718
textComponent = "text"
1819
)
1920

20-
type OptionsProvider struct {
21+
type OptionsProvider[T, U any] struct {
2122
list list.Model
2223
input OptionsInput
2324
onSelectAction tea.Msg
2425
width int
2526
height int
2627
focused focusedComp
28+
getItems func(context.Context) ([]T, error)
29+
itemMapper func([]T) []list.Item
2730
}
2831

2932
type Option struct {
@@ -37,11 +40,11 @@ func (o Option) Description() string { return o.Subtext }
3740
func (o Option) Value() int64 { return o.ID }
3841
func (o Option) FilterValue() string { return o.Name }
3942

40-
func (o OptionsProvider) Init() tea.Cmd {
43+
func (o OptionsProvider[T, U]) Init() tea.Cmd {
4144
return nil
4245
}
4346

44-
func (o OptionsProvider) Update(msg tea.Msg) (OptionsProvider, tea.Cmd) {
47+
func (o OptionsProvider[T, U]) Update(msg tea.Msg) (OptionsProvider[T, U], tea.Cmd) {
4548
var cmd tea.Cmd
4649
var cmds []tea.Cmd
4750
switch msg := msg.(type) {
@@ -62,6 +65,15 @@ func (o OptionsProvider) Update(msg tea.Msg) (OptionsProvider, tea.Cmd) {
6265
}
6366
}
6467
}
68+
case messages.ItemAdded:
69+
o.input.OnBlur()
70+
o.focused = listComponent
71+
o.list.SetSize(o.list.Width(), o.height)
72+
newItems, err := o.getItems(context.Background())
73+
if err != nil {
74+
75+
}
76+
o.list.SetItems(o.itemMapper(newItems))
6577

6678
}
6779
switch o.focused {
@@ -74,31 +86,31 @@ func (o OptionsProvider) Update(msg tea.Msg) (OptionsProvider, tea.Cmd) {
7486
return o, tea.Batch(cmds...)
7587
}
7688

77-
func (o OptionsProvider) View() string {
89+
func (o OptionsProvider[T, U]) View() string {
7890
if o.focused == textComponent {
7991
return lipgloss.JoinVertical(lipgloss.Left, o.list.View(), o.input.View())
8092
}
8193
return o.list.View()
8294
}
8395

84-
func (o *OptionsProvider) OnFocus() {
96+
func (o *OptionsProvider[T, U]) OnFocus() {
8597
}
8698

87-
func (o OptionsProvider) OnBlur() {
99+
func (o OptionsProvider[T, U]) OnBlur() {
88100

89101
}
90102

91-
func (o OptionsProvider) GetSelected() Option {
103+
func (o OptionsProvider[T, U]) GetSelected() Option {
92104
return o.list.SelectedItem().(Option)
93105
}
94106

95-
func (o OptionsProvider) IsFiltering() bool {
107+
func (o OptionsProvider[T, U]) IsFiltering() bool {
96108
return o.list.FilterState() == list.Filtering
97109
}
98110

99111
func initList[T, U any](config *ListConfig[T, U]) list.Model {
100112

101-
rawItems, err := config.CrudOps.List(context.Background())
113+
rawItems, err := config.GetItemsFunc(context.Background())
102114

103115
if err != nil {
104116
rawItems = []T{}
@@ -120,11 +132,21 @@ func initList[T, U any](config *ListConfig[T, U]) list.Model {
120132
return list
121133
}
122134

123-
func NewOptionsProvider[T, U any](config *ListConfig[T, U]) OptionsProvider {
124-
return OptionsProvider{
125-
list: initList(config),
126-
focused: listComponent,
127-
input: NewOptionsInput(),
135+
func NewOptionsProvider[T, U any](config *ListConfig[T, U]) OptionsProvider[T, U] {
136+
137+
inputConfig := InputConfig{
138+
CharLimit: 100,
139+
Placeholder: "Add A New Collection...",
140+
Width: 22,
141+
Prompt: "",
142+
}
143+
144+
return OptionsProvider[T, U]{
145+
list: initList(config),
146+
focused: listComponent,
147+
input: NewOptionsInput(&inputConfig),
148+
getItems: config.GetItemsFunc,
149+
itemMapper: config.ItemMapper,
128150
// onSelectAction: config.OnSelectAction,
129151
}
130152
}

internal/tui/components/OptionsProvider/config.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ type ListConfig[T, U any] struct {
2323

2424
ItemMapper func([]T) []list.Item
2525

26-
CrudOps Crud[T, U]
26+
GetItemsFunc func(context.Context) ([]T, error)
2727
// Style lipgloss.Style
2828
}
2929

30-
type Crud[T, U any] struct {
31-
Create func(context.Context, U) (T, error)
32-
Read func(context.Context, int64) (T, error)
33-
Update func(context.Context, int64, U) (T, error)
34-
Delete func(context.Context, int64) error
35-
List func(context.Context) ([]T, error)
30+
type InputConfig struct {
31+
Prompt string
32+
Placeholder string
33+
CharLimit int
34+
Width int
3635
}

internal/tui/components/OptionsProvider/input.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package optionsProvider
22

33
import (
4+
"github.com/charmbracelet/bubbles/key"
45
"github.com/charmbracelet/bubbles/textinput"
56
tea "github.com/charmbracelet/bubbletea"
7+
"github.com/maniac-en/req/internal/tui/keybinds"
8+
"github.com/maniac-en/req/internal/tui/messages"
9+
"github.com/maniac-en/req/internal/tui/styles"
610
)
711

812
type OptionsInput struct {
@@ -12,11 +16,14 @@ type OptionsInput struct {
1216
editId int
1317
}
1418

15-
func NewOptionsInput() OptionsInput {
19+
func NewOptionsInput(config *InputConfig) OptionsInput {
1620
input := textinput.New()
17-
input.CharLimit = 100
18-
input.Placeholder = "Add New Collection..."
19-
input.Width = 22
21+
input.CharLimit = config.CharLimit
22+
input.Placeholder = config.Placeholder
23+
input.Width = config.Width
24+
input.TextStyle = styles.InputStyle
25+
input.Prompt = config.Prompt
26+
2027
return OptionsInput{
2128
input: input,
2229
editId: -1,
@@ -30,6 +37,13 @@ func (i OptionsInput) Init() tea.Cmd {
3037
func (i OptionsInput) Update(msg tea.Msg) (OptionsInput, tea.Cmd) {
3138
var cmd tea.Cmd
3239
var cmds []tea.Cmd
40+
switch msg := msg.(type) {
41+
case tea.KeyMsg:
42+
switch {
43+
case key.Matches(msg, keybinds.Keys.Choose):
44+
return i, func() tea.Msg { return messages.ItemAdded{Item: i.input.Value()} }
45+
}
46+
}
3347

3448
i.input, cmd = i.input.Update(msg)
3549
cmds = append(cmds, cmd)
@@ -38,7 +52,7 @@ func (i OptionsInput) Update(msg tea.Msg) (OptionsInput, tea.Cmd) {
3852
}
3953

4054
func (i OptionsInput) View() string {
41-
return i.input.View()
55+
return styles.InputStyle.Render(i.input.View())
4256
}
4357

4458
func (i *OptionsInput) SetInput(text string) {

internal/tui/messages/messages.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package messages
2+
3+
type ItemAdded struct {
4+
Item string
5+
}

internal/tui/styles/app-styles.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
var (
88
footerNameStyle = lipgloss.NewStyle().Bold(true)
99
footerNameBGStyle = lipgloss.NewStyle().Background(footerNameBG).Padding(0, 3, 0)
10-
FooterSegmentStyle = lipgloss.NewStyle().Background(lipgloss.Color("#262626")).PaddingLeft(2).Foreground(lipgloss.Color("#656565"))
11-
FooterVersionStyle = lipgloss.NewStyle().Background(lipgloss.Color("#262626")).AlignHorizontal(lipgloss.Right).PaddingRight(2).Foreground(lipgloss.Color("#656565"))
10+
FooterSegmentStyle = lipgloss.NewStyle().Background(footerSegmentBG).PaddingLeft(2).Foreground(footerSegmentFG)
11+
FooterVersionStyle = lipgloss.NewStyle().Background(footerSegmentBG).AlignHorizontal(lipgloss.Right).PaddingRight(2).Foreground(footerSegmentFG)
1212
TabHeadingInactive = lipgloss.NewStyle().Width(25).AlignHorizontal(lipgloss.Center).Border(lipgloss.NormalBorder(), false, false, false, true)
1313
TabHeadingActive = lipgloss.NewStyle().Background(accent).Foreground(headingForeground).Width(25).AlignHorizontal(lipgloss.Center).Border(lipgloss.NormalBorder(), false, false, false, true)
1414
)

internal/tui/styles/collections-styles.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ import "github.com/charmbracelet/lipgloss"
44

55
var (
66
SelectedListStyle = lipgloss.NewStyle().Foreground(accent).PaddingLeft(1).Border(lipgloss.NormalBorder(), false, false, false, true).BorderForeground(accent)
7+
InputStyle = lipgloss.NewStyle().Padding(1, 2).Border(lipgloss.NormalBorder(), false, false, false, true).Margin(1, 0)
78
)

internal/tui/styles/colors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ var (
88
footerNameFGTo = lipgloss.Color("#77F07F")
99
accent = lipgloss.Color("#77F07F")
1010
headingForeground = lipgloss.Color("#000000")
11+
footerSegmentBG = lipgloss.Color("#262626")
12+
footerSegmentFG = lipgloss.Color("#656565")
1113
)

internal/tui/views/collections-view.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package views
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/charmbracelet/bubbles/list"
78
tea "github.com/charmbracelet/bubbletea"
89
"github.com/maniac-en/req/internal/backend/collections"
910
optionsProvider "github.com/maniac-en/req/internal/tui/components/OptionsProvider"
11+
"github.com/maniac-en/req/internal/tui/messages"
1012
)
1113

1214
type CollectionsView struct {
13-
width int
14-
height int
15-
list optionsProvider.OptionsProvider
15+
width int
16+
height int
17+
list optionsProvider.OptionsProvider[collections.CollectionEntity, string]
18+
manager *collections.CollectionsManager
1619
}
1720

1821
func (c CollectionsView) Init() tea.Cmd {
@@ -40,6 +43,8 @@ func (c CollectionsView) Update(msg tea.Msg) (ViewInterface, tea.Cmd) {
4043
c.width = msg.Width
4144
c.list, cmd = c.list.Update(msg)
4245
cmds = append(cmds, cmd)
46+
case messages.ItemAdded:
47+
c.manager.Create(context.Background(), msg.Item)
4348
}
4449

4550
c.list, cmd = c.list.Update(msg)
@@ -75,15 +80,12 @@ func itemMapper(items []collections.CollectionEntity) []list.Item {
7580

7681
func NewCollectionsView(collManager *collections.CollectionsManager) *CollectionsView {
7782
config := defaultListConfig[collections.CollectionEntity, string]()
78-
config.CrudOps = optionsProvider.Crud[collections.CollectionEntity, string]{
79-
Create: collManager.Create,
80-
Read: collManager.Read,
81-
Update: collManager.Update,
82-
Delete: collManager.Delete,
83-
List: collManager.List,
84-
}
83+
84+
config.GetItemsFunc = collManager.List
8585
config.ItemMapper = itemMapper
86+
8687
return &CollectionsView{
87-
list: optionsProvider.NewOptionsProvider(config),
88+
list: optionsProvider.NewOptionsProvider(config),
89+
manager: collManager,
8890
}
8991
}

0 commit comments

Comments
 (0)