Skip to content

Commit bdbccbe

Browse files
committed
changes:
- added the boilerplate for an endpoints view - added a way to order views so that they dont switch around in the heading
1 parent 96de05f commit bdbccbe

7 files changed

Lines changed: 111 additions & 7 deletions

File tree

internal/tui/app/model.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package app
22

33
import (
4+
"sort"
45
"strings"
56

67
"github.com/charmbracelet/bubbles/help"
78
"github.com/charmbracelet/bubbles/key"
89
tea "github.com/charmbracelet/bubbletea"
910
"github.com/charmbracelet/lipgloss"
1011
"github.com/maniac-en/req/internal/tui/keybinds"
12+
"github.com/maniac-en/req/internal/tui/messages"
1113
"github.com/maniac-en/req/internal/tui/styles"
1214
"github.com/maniac-en/req/internal/tui/views"
1315
)
@@ -16,8 +18,14 @@ type ViewName string
1618

1719
const (
1820
Collections ViewName = "collections"
21+
Endpoints ViewName = "endpoints"
1922
)
2023

24+
type Heading struct {
25+
name string
26+
order int
27+
}
28+
2129
type AppModel struct {
2230
ctx *Context
2331
width int
@@ -39,9 +47,15 @@ func (a AppModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
3947
case tea.WindowSizeMsg:
4048
a.height = msg.Height
4149
a.width = msg.Width
42-
a.Views[a.focusedView], cmd = a.Views[a.focusedView].Update(tea.WindowSizeMsg{Height: a.AvailableHeight(), Width: msg.Width})
50+
for key, _ := range a.Views {
51+
a.Views[key], cmd = a.Views[key].Update(tea.WindowSizeMsg{Height: a.AvailableHeight(), Width: msg.Width})
52+
cmds = append(cmds, cmd)
53+
}
4354
cmds = append(cmds, cmd)
4455
return a, tea.Batch(cmds...)
56+
case messages.ChooseCollection:
57+
a.focusedView = Endpoints
58+
return a, tea.Batch(cmds...)
4559
case tea.KeyMsg:
4660
switch {
4761
case key.Matches(msg, keybinds.Keys.Quit):
@@ -82,14 +96,28 @@ func (a *AppModel) AvailableHeight() int {
8296
func (a AppModel) Header() string {
8397
var b strings.Builder
8498

85-
for key, value := range a.Views {
86-
if key == a.focusedView {
87-
b.WriteString(styles.TabHeadingActive.Render(value.Name()))
99+
// INFO: this might be a bit messy, could be a nice idea to look into OrderedMaps maybe?
100+
views := []Heading{}
101+
for key := range a.Views {
102+
views = append(views, Heading{
103+
name: a.Views[key].Name(),
104+
order: a.Views[key].Order(),
105+
})
106+
}
107+
sort.Slice(views, func(i, j int) bool {
108+
return views[i].order < views[j].order
109+
})
110+
111+
for _, item := range views {
112+
if item.name == a.Views[a.focusedView].Name() {
113+
b.WriteString(styles.TabHeadingActive.Render(item.name))
88114
} else {
89-
b.WriteString(styles.TabHeadingInactive.Render(value.Name()))
115+
b.WriteString(styles.TabHeadingInactive.Render(item.name))
90116
}
91117
}
118+
92119
b.WriteString(styles.TabHeadingInactive.Render(""))
120+
93121
return b.String()
94122
}
95123

@@ -112,7 +140,8 @@ func NewAppModel(ctx *Context) AppModel {
112140
keys: appKeybinds,
113141
}
114142
model.Views = map[ViewName]views.ViewInterface{
115-
Collections: views.NewCollectionsView(model.ctx.Collections),
143+
Collections: views.NewCollectionsView(model.ctx.Collections, 1),
144+
Endpoints: views.NewEndpointsView(2),
116145
}
117146
return model
118147
}

internal/tui/components/OptionsProvider/component.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ func (o OptionsProvider[T, U]) Update(msg tea.Msg) (OptionsProvider[T, U], tea.C
6767
return o, tea.Batch(cmds...)
6868
case key.Matches(msg, o.keys.DeleteItem):
6969
return o, func() tea.Msg { return messages.DeleteItem{ItemID: int64(o.GetSelected().ID)} }
70+
case key.Matches(msg, o.keys.Choose):
71+
return o, func() tea.Msg { return messages.ChooseCollection{} }
7072
case key.Matches(msg, o.keys.EditItem):
7173
o.list.SetSize(o.list.Width(), o.height-lipgloss.Height(o.input.View()))
7274
o.input.SetInput(o.GetSelected().Name)

internal/tui/keybinds/collections-binds.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type ListKeyMap struct {
1414
AddItem key.Binding
1515
EditItem key.Binding
1616
DeleteItem key.Binding
17+
Choose key.Binding
1718
Accept key.Binding
1819
Back key.Binding
1920
}
@@ -41,6 +42,7 @@ func NewListKeyMap() *ListKeyMap {
4142
AddItem: Keys.InsertItem,
4243
DeleteItem: Keys.Remove,
4344
EditItem: Keys.EditItem,
45+
Choose: Keys.Choose,
4446
Accept: Keys.Choose,
4547
Back: Keys.Back,
4648
}

internal/tui/messages/messages.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ type ItemEdited struct {
77
Item string
88
ItemID int64
99
}
10+
1011
type DeleteItem struct {
1112
ItemID int64
1213
}
14+
15+
type ChooseCollection struct{}
16+
1317
type DeactivateView struct{}

internal/tui/views/collections-view.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type CollectionsView struct {
2121
manager *collections.CollectionsManager
2222
help help.Model
2323
keys *keybinds.ListKeyMap
24+
order int
2425
}
2526

2627
func (c CollectionsView) Init() tea.Cmd {
@@ -67,6 +68,10 @@ func (c CollectionsView) View() string {
6768
return c.list.View()
6869
}
6970

71+
func (c CollectionsView) Order() int {
72+
return c.order
73+
}
74+
7075
func (c CollectionsView) OnFocus() {
7176

7277
}
@@ -88,7 +93,7 @@ func itemMapper(items []collections.CollectionEntity) []list.Item {
8893
return opts
8994
}
9095

91-
func NewCollectionsView(collManager *collections.CollectionsManager) *CollectionsView {
96+
func NewCollectionsView(collManager *collections.CollectionsManager, order int) *CollectionsView {
9297
keybinds := keybinds.NewListKeyMap()
9398
config := defaultListConfig[collections.CollectionEntity, string](keybinds)
9499

@@ -101,5 +106,6 @@ func NewCollectionsView(collManager *collections.CollectionsManager) *Collection
101106
manager: collManager,
102107
help: help.New(),
103108
keys: keybinds,
109+
order: order,
104110
}
105111
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package views
2+
3+
import (
4+
"github.com/charmbracelet/bubbles/key"
5+
tea "github.com/charmbracelet/bubbletea"
6+
"github.com/charmbracelet/lipgloss"
7+
)
8+
9+
type EndpointsView struct {
10+
height int
11+
width int
12+
order int
13+
}
14+
15+
func (e EndpointsView) Init() tea.Cmd {
16+
return nil
17+
}
18+
19+
func (e EndpointsView) Name() string {
20+
return "Endpoints"
21+
}
22+
23+
func (e EndpointsView) Help() []key.Binding {
24+
return []key.Binding{}
25+
}
26+
27+
func (e EndpointsView) GetFooterSegment() string {
28+
return ""
29+
}
30+
31+
func (e EndpointsView) Update(msg tea.Msg) (ViewInterface, tea.Cmd) {
32+
switch msg := msg.(type) {
33+
case tea.WindowSizeMsg:
34+
e.height = msg.Height
35+
e.width = msg.Width
36+
}
37+
return e, nil
38+
}
39+
40+
func (e EndpointsView) View() string {
41+
return lipgloss.NewStyle().Height(e.height).Width(e.width).Align(lipgloss.Center, lipgloss.Center).Render("Endpoints View!")
42+
}
43+
44+
func (e EndpointsView) OnFocus() {
45+
46+
}
47+
48+
func (e EndpointsView) OnBlur() {
49+
50+
}
51+
52+
func (e EndpointsView) Order() int {
53+
return e.order
54+
}
55+
56+
func NewEndpointsView(order int) *EndpointsView {
57+
return &EndpointsView{
58+
order: order,
59+
}
60+
}

internal/tui/views/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type ViewInterface interface {
1212
GetFooterSegment() string
1313
Update(tea.Msg) (ViewInterface, tea.Cmd)
1414
View() string
15+
Order() int
1516
OnFocus()
1617
OnBlur()
1718
}

0 commit comments

Comments
 (0)