11package app
22
33import (
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
1719const (
1820 Collections ViewName = "collections"
21+ Endpoints ViewName = "endpoints"
1922)
2023
24+ type Heading struct {
25+ name string
26+ order int
27+ }
28+
2129type 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 {
8296func (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}
0 commit comments