Skip to content

Commit 2c438d9

Browse files
committed
changes:
- bug fixes in main model - made it so that colors are not exposed, only styles are - updated the update func for options provider to forward messages to the list component - set up the list crud op and the item mapper for collections view - changed generics in the list config so it makes more sense - updated the footer func for collections
1 parent bffb049 commit 2c438d9

8 files changed

Lines changed: 85 additions & 33 deletions

File tree

internal/tui/app/model.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@ func (a AppModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
3636
a.width = msg.Width
3737
a.Views[a.focusedView], cmd = a.Views[a.focusedView].Update(tea.WindowSizeMsg{Height: a.AvailableHeight(), Width: msg.Width})
3838
cmds = append(cmds, cmd)
39+
return a, tea.Batch(cmds...)
3940
case tea.KeyMsg:
4041
switch msg.String() {
4142
case "ctrl+c":
4243
return a, tea.Quit
4344
}
4445
}
46+
47+
a.Views[a.focusedView], cmd = a.Views[a.focusedView].Update(msg)
48+
cmds = append(cmds, cmd)
49+
4550
return a, tea.Batch(cmds...)
4651
}
4752

@@ -73,9 +78,9 @@ func (a AppModel) Header() string {
7378
}
7479

7580
func (a AppModel) Footer() string {
76-
name := styles.GradientText("REQ", styles.FooterNameFGFrom, styles.FooterNameFGTo, styles.FooterNameStyle, styles.FooterNameBGStyle)
81+
name := styles.ApplyGradientToFooter("REQ")
7782
footerText := styles.FooterSegmentStyle.Render(a.Views[a.focusedView].GetFooterSegment())
78-
version := styles.FooterVersionStyle.Width(a.width - lipgloss.Width(name) - lipgloss.Width(footerText)).Render("v0.1.0-alpha2")
83+
version := styles.FooterVersionStyle.Width(a.width - lipgloss.Width(name) - lipgloss.Width(footerText)).Render("v0.1.0-alpha.2")
7984
return lipgloss.JoinHorizontal(lipgloss.Left, name, footerText, version)
8085
}
8186

internal/tui/components/OptionsProvider/component.go

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

33
import (
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

1517
type 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

2628
func (o OptionsProvider) Init() tea.Cmd {
2729
return nil
2830
}
2931

3032
func (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

4054
func (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,

internal/tui/components/OptionsProvider/config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
tea "github.com/charmbracelet/bubbletea"
88
)
99

10-
type ListConfig[T, C any] struct {
10+
type ListConfig[T, U any] struct {
1111
OnSelectAction tea.Msg
1212

1313
ShowPagination bool
@@ -23,14 +23,14 @@ type ListConfig[T, C any] struct {
2323

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

26-
CrudOps Crud[C]
26+
CrudOps Crud[T, U]
2727
// Style lipgloss.Style
2828
}
2929

30-
type Crud[T any] struct {
31-
Create func(context.Context, string) (T, error)
30+
type Crud[T, U any] struct {
31+
Create func(context.Context, U) (T, error)
3232
Read func(context.Context, int64) (T, error)
33-
Update func(context.Context, int64, string) (T, error)
33+
Update func(context.Context, int64, U) (T, error)
3434
Delete func(context.Context, int64) error
3535
List func(context.Context) ([]T, error)
3636
}

internal/tui/styles/app-styles.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
)
66

77
var (
8-
FooterNameStyle = lipgloss.NewStyle().Bold(true)
9-
FooterNameBGStyle = lipgloss.NewStyle().Background(FooterNameBG).Padding(0, 3, 0)
8+
footerNameStyle = lipgloss.NewStyle().Bold(true)
9+
footerNameBGStyle = lipgloss.NewStyle().Background(footerNameBG).Padding(0, 3, 0)
1010
FooterSegmentStyle = lipgloss.NewStyle().Background(lipgloss.Color("#262626")).PaddingLeft(2).Foreground(lipgloss.Color("#656565"))
1111
FooterVersionStyle = lipgloss.NewStyle().Background(lipgloss.Color("#262626")).AlignHorizontal(lipgloss.Right).PaddingRight(2).Foreground(lipgloss.Color("#656565"))
1212
TabHeadingInactive = lipgloss.NewStyle().Width(25).AlignHorizontal(lipgloss.Center).Border(lipgloss.NormalBorder(), false, false, false, true)
13-
TabHeadingActive = lipgloss.NewStyle().Background(Accent).Foreground(HeadingForeground).Width(25).AlignHorizontal(lipgloss.Center).Border(lipgloss.NormalBorder(), false, false, false, true)
13+
TabHeadingActive = lipgloss.NewStyle().Background(accent).Foreground(headingForeground).Width(25).AlignHorizontal(lipgloss.Center).Border(lipgloss.NormalBorder(), false, false, false, true)
1414
)

internal/tui/styles/colors.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package styles
33
import "github.com/charmbracelet/lipgloss"
44

55
var (
6-
FooterNameBG = lipgloss.Color("#1a1a1a")
7-
FooterNameFGFrom = lipgloss.Color("#41A0AE")
8-
FooterNameFGTo = lipgloss.Color("#77F07F")
9-
Accent = lipgloss.Color("#77F07F")
10-
HeadingForeground = lipgloss.Color("#000000")
6+
footerNameBG = lipgloss.Color("#1a1a1a")
7+
footerNameFGFrom = lipgloss.Color("#41A0AE")
8+
footerNameFGTo = lipgloss.Color("#77F07F")
9+
accent = lipgloss.Color("#77F07F")
10+
headingForeground = lipgloss.Color("#000000")
1111
)

internal/tui/styles/functions.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/charmbracelet/lipgloss"
77
)
88

9-
func GradientText(text string, startColor, endColor lipgloss.Color, base, additional lipgloss.Style) string {
9+
func gradientText(text string, startColor, endColor lipgloss.Color, base, additional lipgloss.Style) string {
1010
n := len(text)
1111
result := ""
1212

@@ -37,3 +37,7 @@ func hexToRGB(hex string) (int, int, int) {
3737
fmt.Sscanf(hex, "#%02x%02x%02x", &r, &g, &b)
3838
return r, g, b
3939
}
40+
41+
func ApplyGradientToFooter(text string) string {
42+
return gradientText("REQ", footerNameFGFrom, footerNameFGTo, footerNameStyle, footerNameBGStyle)
43+
}

internal/tui/views/collections-view-helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
optionsProvider "github.com/maniac-en/req/internal/tui/components/OptionsProvider"
55
)
66

7-
func defaultListConfig[T, C any]() *optionsProvider.ListConfig[T, C] {
8-
config := optionsProvider.ListConfig[T, C]{
7+
func defaultListConfig[T, U any]() *optionsProvider.ListConfig[T, U] {
8+
config := optionsProvider.ListConfig[T, U]{
99
ShowPagination: false,
1010
ShowStatusBar: false,
1111
ShowHelp: false,

internal/tui/views/collections-view.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package views
22

33
import (
4+
"github.com/charmbracelet/bubbles/list"
45
tea "github.com/charmbracelet/bubbletea"
56
"github.com/maniac-en/req/internal/backend/collections"
67
optionsProvider "github.com/maniac-en/req/internal/tui/components/OptionsProvider"
@@ -25,7 +26,7 @@ func (c CollectionsView) Help() string {
2526
}
2627

2728
func (c CollectionsView) GetFooterSegment() string {
28-
return "Collections"
29+
return c.list.GetSelected().Title()
2930
}
3031

3132
func (c CollectionsView) Update(msg tea.Msg) (ViewInterface, tea.Cmd) {
@@ -38,6 +39,10 @@ func (c CollectionsView) Update(msg tea.Msg) (ViewInterface, tea.Cmd) {
3839
c.list, cmd = c.list.Update(msg)
3940
cmds = append(cmds, cmd)
4041
}
42+
43+
c.list, cmd = c.list.Update(msg)
44+
cmds = append(cmds, cmd)
45+
4146
return c, tea.Batch(cmds...)
4247
}
4348

@@ -53,15 +58,29 @@ func (c CollectionsView) OnBlur() {
5358

5459
}
5560

61+
func itemMapper(items []collections.CollectionEntity) []list.Item {
62+
opts := make([]list.Item, len(items))
63+
for i, item := range items {
64+
newOpt := optionsProvider.Option{
65+
Name: item.GetName(),
66+
Subtext: "Sample",
67+
ID: item.GetID(),
68+
}
69+
opts[i] = newOpt
70+
}
71+
return opts
72+
}
73+
5674
func NewCollectionsView(collManager *collections.CollectionsManager) *CollectionsView {
57-
config := defaultListConfig[string, collections.CollectionEntity]()
58-
config.CrudOps = optionsProvider.Crud[collections.CollectionEntity]{
75+
config := defaultListConfig[collections.CollectionEntity, string]()
76+
config.CrudOps = optionsProvider.Crud[collections.CollectionEntity, string]{
5977
Create: collManager.Create,
6078
Read: collManager.Read,
6179
Update: collManager.Update,
6280
Delete: collManager.Delete,
6381
List: collManager.List,
6482
}
83+
config.ItemMapper = itemMapper
6584
return &CollectionsView{
6685
list: optionsProvider.NewOptionsProvider(config),
6786
}

0 commit comments

Comments
 (0)