Skip to content

Commit 96de05f

Browse files
committed
help menu is now structured in a better way
1 parent 05b8fb1 commit 96de05f

8 files changed

Lines changed: 104 additions & 10 deletions

File tree

internal/tui/app/model.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type AppModel struct {
2424
height int
2525
Views map[ViewName]views.ViewInterface
2626
focusedView ViewName
27+
keys []key.Binding
2728
help help.Model
2829
}
2930

@@ -63,14 +64,18 @@ func (a AppModel) View() string {
6364
}
6465

6566
func (a AppModel) Help() string {
66-
appHelp := styles.AppHelpStyle.Render(" • " + a.help.View(keybinds.Keys))
67-
return lipgloss.JoinHorizontal(lipgloss.Left, a.Views[a.focusedView].Help(), appHelp)
67+
viewHelp := a.Views[a.focusedView].Help()
68+
appHelp := append(viewHelp, a.keys...)
69+
helpStruct := keybinds.Help{
70+
Keys: appHelp,
71+
}
72+
return styles.HelpStyle.Render(a.help.View(helpStruct))
6873
}
6974

7075
func (a *AppModel) AvailableHeight() int {
7176
footer := a.Footer()
7277
header := a.Header()
73-
help := a.Views[a.focusedView].Help()
78+
help := a.Help()
7479
return a.height - lipgloss.Height(header) - lipgloss.Height(footer) - lipgloss.Height(help)
7580
}
7681

@@ -96,10 +101,15 @@ func (a AppModel) Footer() string {
96101
}
97102

98103
func NewAppModel(ctx *Context) AppModel {
104+
appKeybinds := []key.Binding{
105+
keybinds.Keys.Quit,
106+
}
107+
99108
model := AppModel{
100109
focusedView: Collections,
101110
ctx: ctx,
102111
help: help.New(),
112+
keys: appKeybinds,
103113
}
104114
model.Views = map[ViewName]views.ViewInterface{
105115
Collections: views.NewCollectionsView(model.ctx.Collections),
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
package input
22

3+
import "github.com/charmbracelet/bubbles/key"
4+
35
type InputConfig struct {
46
Prompt string
57
Placeholder string
68
CharLimit int
79
Width int
10+
KeyMap InputKeyMaps
11+
}
12+
13+
type InputKeyMaps struct {
14+
Accept key.Binding
15+
Back key.Binding
816
}

internal/tui/components/Input/input.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"github.com/charmbracelet/bubbles/key"
55
"github.com/charmbracelet/bubbles/textinput"
66
tea "github.com/charmbracelet/bubbletea"
7-
"github.com/maniac-en/req/internal/tui/keybinds"
87
"github.com/maniac-en/req/internal/tui/messages"
98
"github.com/maniac-en/req/internal/tui/styles"
109
)
@@ -14,6 +13,7 @@ type OptionsInput struct {
1413
height int
1514
width int
1615
editId int64
16+
keys InputKeyMaps
1717
}
1818

1919
func NewOptionsInput(config *InputConfig) OptionsInput {
@@ -27,6 +27,7 @@ func NewOptionsInput(config *InputConfig) OptionsInput {
2727
return OptionsInput{
2828
input: input,
2929
editId: -1,
30+
keys: config.KeyMap,
3031
}
3132
}
3233

@@ -40,14 +41,14 @@ func (i OptionsInput) Update(msg tea.Msg) (OptionsInput, tea.Cmd) {
4041
switch msg := msg.(type) {
4142
case tea.KeyMsg:
4243
switch {
43-
case key.Matches(msg, keybinds.Keys.Choose):
44+
case key.Matches(msg, i.keys.Accept):
4445
itemName := i.input.Value()
4546
i.input.SetValue("")
4647
if i.editId == -1 {
4748
return i, func() tea.Msg { return messages.ItemAdded{Item: itemName} }
4849
}
4950
return i, func() tea.Msg { return messages.ItemEdited{Item: itemName, ItemID: i.editId} }
50-
case key.Matches(msg, keybinds.Keys.Back):
51+
case key.Matches(msg, i.keys.Back):
5152
return i, func() tea.Msg { return messages.DeactivateView{} }
5253
}
5354
}
@@ -62,6 +63,13 @@ func (i OptionsInput) View() string {
6263
return styles.InputStyle.Render(i.input.View())
6364
}
6465

66+
func (i OptionsInput) Help() []key.Binding {
67+
return []key.Binding{
68+
i.keys.Accept,
69+
i.keys.Back,
70+
}
71+
}
72+
6573
func (i *OptionsInput) SetInput(text string) {
6674
i.input.SetValue(text)
6775
}

internal/tui/components/OptionsProvider/component.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ func (o OptionsProvider[T, U]) OnBlur() {
111111
}
112112

113113
func (o OptionsProvider[T, U]) GetSelected() Option {
114+
if o.IsFiltering() {
115+
return Option{
116+
Name: "Filtering....",
117+
ID: -1,
118+
Subtext: "",
119+
}
120+
}
114121
return o.list.SelectedItem().(Option)
115122
}
116123

@@ -127,6 +134,36 @@ func (o *OptionsProvider[T, U]) RefreshItems() {
127134
o.list.SetItems(o.itemMapper(newItems))
128135
}
129136

137+
func (o *OptionsProvider[T, U]) Help() []key.Binding {
138+
var binds []key.Binding
139+
switch o.focused {
140+
case listComponent:
141+
if o.IsFiltering() {
142+
binds = []key.Binding{
143+
o.keys.AcceptWhileFiltering,
144+
o.keys.CancelWhileFiltering,
145+
o.keys.ClearFilter,
146+
}
147+
} else {
148+
binds = []key.Binding{
149+
o.keys.CursorUp,
150+
o.keys.CursorDown,
151+
o.keys.NextPage,
152+
o.keys.PrevPage,
153+
o.keys.Filter,
154+
o.keys.AddItem,
155+
o.keys.EditItem,
156+
o.keys.DeleteItem,
157+
}
158+
}
159+
case textComponent:
160+
binds = o.input.Help()
161+
default:
162+
binds = []key.Binding{}
163+
}
164+
return binds
165+
}
166+
130167
func initList[T, U any](config *ListConfig[T, U]) list.Model {
131168

132169
rawItems, err := config.GetItemsFunc(context.Background())
@@ -157,6 +194,10 @@ func NewOptionsProvider[T, U any](config *ListConfig[T, U]) OptionsProvider[T, U
157194
Placeholder: "Add A New Collection...",
158195
Width: 22,
159196
Prompt: "",
197+
KeyMap: input.InputKeyMaps{
198+
Accept: config.AdditionalKeymaps.Accept,
199+
Back: config.AdditionalKeymaps.Back,
200+
},
160201
}
161202

162203
return OptionsProvider[T, U]{

internal/tui/keybinds/collections-binds.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type ListKeyMap struct {
1414
AddItem key.Binding
1515
EditItem key.Binding
1616
DeleteItem key.Binding
17+
Accept key.Binding
18+
Back key.Binding
1719
}
1820

1921
func (c ListKeyMap) ShortHelp() []key.Binding {
@@ -39,5 +41,7 @@ func NewListKeyMap() *ListKeyMap {
3941
AddItem: Keys.InsertItem,
4042
DeleteItem: Keys.Remove,
4143
EditItem: Keys.EditItem,
44+
Accept: Keys.Choose,
45+
Back: Keys.Back,
4246
}
4347
}

internal/tui/keybinds/types.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package keybinds
2+
3+
import (
4+
"github.com/charmbracelet/bubbles/key"
5+
)
6+
7+
type Help struct {
8+
Keys []key.Binding
9+
}
10+
11+
func (h Help) ShortHelp() []key.Binding {
12+
return h.Keys
13+
}
14+
15+
func (h Help) FullHelp() [][]key.Binding {
16+
// TODO: Figure how you wanna show this
17+
return [][]key.Binding{}
18+
}
19+
20+
func (h Help) SetHelp(helpMenu []key.Binding) {
21+
h.Keys = helpMenu
22+
}

internal/tui/views/collections-view.go

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

77
"github.com/charmbracelet/bubbles/help"
8+
"github.com/charmbracelet/bubbles/key"
89
"github.com/charmbracelet/bubbles/list"
910
tea "github.com/charmbracelet/bubbletea"
1011
"github.com/maniac-en/req/internal/backend/collections"
1112
optionsProvider "github.com/maniac-en/req/internal/tui/components/OptionsProvider"
1213
"github.com/maniac-en/req/internal/tui/keybinds"
1314
"github.com/maniac-en/req/internal/tui/messages"
14-
"github.com/maniac-en/req/internal/tui/styles"
1515
)
1616

1717
type CollectionsView struct {
@@ -31,8 +31,8 @@ func (c CollectionsView) Name() string {
3131
return "Collections"
3232
}
3333

34-
func (c CollectionsView) Help() string {
35-
return styles.HelpStyle.Render(c.help.View(c.keys))
34+
func (c CollectionsView) Help() []key.Binding {
35+
return c.list.Help()
3636
}
3737

3838
func (c CollectionsView) GetFooterSegment() string {

internal/tui/views/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package views
22

33
import (
4+
"github.com/charmbracelet/bubbles/key"
45
tea "github.com/charmbracelet/bubbletea"
56
)
67

78
type ViewInterface interface {
89
Init() tea.Cmd
910
Name() string
10-
Help() string
11+
Help() []key.Binding
1112
GetFooterSegment() string
1213
Update(tea.Msg) (ViewInterface, tea.Cmd)
1314
View() string

0 commit comments

Comments
 (0)