Skip to content

Commit 41eb9a1

Browse files
committed
fix: menu keybinds
1 parent b9ef76f commit 41eb9a1

5 files changed

Lines changed: 111 additions & 54 deletions

File tree

pkg/keys/keys.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
"github.com/muesli/reflow/ansi"
1010

11+
bubblekey "github.com/charmbracelet/bubbles/key"
12+
1113
"github.com/macropower/kat/pkg/ui/theme"
1214
)
1315

@@ -109,6 +111,15 @@ func (kb *KeyBind) Match(key string) bool {
109111
return false
110112
}
111113

114+
func (kb *KeyBind) BubbleKey(opts ...bubblekey.BindingOpt) bubblekey.Binding {
115+
codes := []string{}
116+
for _, k := range kb.Keys {
117+
codes = append(codes, k.Code)
118+
}
119+
120+
return bubblekey.NewBinding(append(opts, bubblekey.WithKeys(codes...))...)
121+
}
122+
112123
// IsTextInputAction checks if the key should be applied as text input.
113124
func IsTextInputAction(key string) bool {
114125
alwaysNonInput := []string{

pkg/ui/configeditor/configeditor.go

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/macropower/kat/pkg/profile"
1616
"github.com/macropower/kat/pkg/ui/common"
1717
"github.com/macropower/kat/pkg/ui/filepicker"
18-
"github.com/macropower/kat/pkg/ui/theme"
1918
)
2019

2120
const (
@@ -50,7 +49,7 @@ type Commander interface {
5049
FS() (*command.FilteredFS, error)
5150
}
5251

53-
func NewModel(cmd Commander, t *theme.Theme) Model {
52+
func NewModel(cmd Commander, t *huh.Theme, km *huh.KeyMap) Model {
5453
var (
5554
m Model
5655
selectedPath string
@@ -102,60 +101,12 @@ func NewModel(cmd Commander, t *theme.Theme) Model {
102101
),
103102
).
104103
WithShowHelp(false).
105-
WithTheme(ThemeToHuhTheme(t))
104+
WithTheme(t).
105+
WithKeyMap(km)
106106

107107
return m
108108
}
109109

110-
func ThemeToHuhTheme(t *theme.Theme) *huh.Theme {
111-
h := huh.ThemeBase()
112-
113-
h.Focused.Base = h.Focused.Base.BorderForeground(t.SelectedStyle.GetForeground())
114-
h.Focused.Card = h.Focused.Base
115-
h.Focused.Title = h.Focused.Title.Foreground(t.SelectedStyle.GetForeground()).Bold(true)
116-
h.Focused.NoteTitle = h.Focused.NoteTitle.Foreground(t.SelectedStyle.GetForeground()).Bold(true).MarginBottom(1)
117-
h.Focused.Directory = h.Focused.Directory.Foreground(t.SelectedSubtleStyle.GetForeground())
118-
h.Focused.Description = h.Focused.Description.Foreground(t.SelectedSubtleStyle.GetForeground())
119-
h.Focused.ErrorIndicator = h.Focused.ErrorIndicator.Foreground(t.ErrorTextStyle.GetForeground())
120-
h.Focused.ErrorMessage = h.Focused.ErrorMessage.Foreground(t.ErrorTextStyle.GetForeground())
121-
h.Focused.SelectSelector = h.Focused.SelectSelector.Foreground(t.SelectedStyle.GetForeground())
122-
h.Focused.NextIndicator = h.Focused.NextIndicator.Foreground(t.SelectedStyle.GetForeground())
123-
h.Focused.PrevIndicator = h.Focused.PrevIndicator.Foreground(t.SelectedStyle.GetForeground())
124-
h.Focused.Option = h.Focused.Option.Foreground(t.GenericTextStyle.GetBackground())
125-
h.Focused.MultiSelectSelector = h.Focused.MultiSelectSelector.Foreground(t.SelectedStyle.GetForeground())
126-
h.Focused.SelectedOption = h.Focused.SelectedOption.Foreground(t.SelectedStyle.GetForeground())
127-
h.Focused.SelectedPrefix = lipgloss.NewStyle().
128-
Foreground(t.SelectedStyle.GetForeground()).
129-
SetString("✓ ")
130-
h.Focused.UnselectedPrefix = lipgloss.NewStyle().
131-
Foreground(t.SubtleStyle.GetForeground()).
132-
SetString("• ")
133-
h.Focused.UnselectedOption = h.Focused.UnselectedOption.
134-
Foreground(t.GenericTextStyle.GetBackground())
135-
h.Focused.FocusedButton = h.Focused.FocusedButton.
136-
Foreground(t.LogoStyle.GetForeground()).
137-
Background(t.LogoStyle.GetBackground())
138-
h.Focused.Next = h.Focused.FocusedButton
139-
h.Focused.BlurredButton = h.Focused.BlurredButton.
140-
Foreground(t.LogoStyle.GetForeground()).
141-
Background(t.SubtleStyle.GetForeground())
142-
143-
h.Focused.TextInput.Cursor = h.Focused.TextInput.Cursor.Foreground(t.SelectedStyle.GetForeground())
144-
h.Focused.TextInput.Placeholder = h.Focused.TextInput.Placeholder.Foreground(t.SubtleStyle.GetForeground())
145-
h.Focused.TextInput.Prompt = h.Focused.TextInput.Prompt.Foreground(t.SelectedStyle.GetForeground())
146-
147-
h.Blurred = h.Focused
148-
h.Blurred.Base = h.Focused.Base.BorderStyle(lipgloss.HiddenBorder())
149-
h.Blurred.Card = h.Blurred.Base
150-
h.Blurred.NextIndicator = lipgloss.NewStyle()
151-
h.Blurred.PrevIndicator = lipgloss.NewStyle()
152-
153-
h.Group.Title = h.Focused.Title
154-
h.Group.Description = h.Focused.Description
155-
156-
return h
157-
}
158-
159110
func (m Model) Init() tea.Cmd {
160111
return m.form.Init()
161112
}

pkg/ui/menu/keys.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package menu
22

33
import (
4+
"github.com/charmbracelet/huh"
5+
46
tea "github.com/charmbracelet/bubbletea"
57

68
"github.com/macropower/kat/pkg/keys"
@@ -59,7 +61,7 @@ func (kb *KeyBinds) GetKeyBinds() []keys.KeyBind {
5961
}
6062

6163
type KeyHandler struct {
62-
kb *KeyBinds //nolint:unused // TODO: Use it.
64+
kb *KeyBinds
6365
ckb *common.KeyBinds
6466
}
6567

@@ -82,3 +84,35 @@ func (h *KeyHandler) HandleKeys(m Model, msg tea.KeyMsg) (Model, tea.Cmd) {
8284

8385
return m, cmd
8486
}
87+
88+
func (h *KeyHandler) HuhKeyMap() *huh.KeyMap {
89+
km := huh.NewDefaultKeyMap()
90+
91+
km.Input.Prev = h.ckb.Prev.BubbleKey()
92+
km.Input.Next = h.ckb.Next.BubbleKey()
93+
km.Input.Submit = h.kb.Select.BubbleKey()
94+
95+
km.FilePicker.GotoTop = h.kb.Home.BubbleKey()
96+
km.FilePicker.GotoBottom = h.kb.End.BubbleKey()
97+
km.FilePicker.PageUp = h.kb.PageUp.BubbleKey()
98+
km.FilePicker.PageDown = h.kb.PageDown.BubbleKey()
99+
km.FilePicker.Back = h.ckb.Left.BubbleKey()
100+
km.FilePicker.Up = h.ckb.Up.BubbleKey()
101+
km.FilePicker.Down = h.ckb.Down.BubbleKey()
102+
km.FilePicker.Close = h.ckb.Escape.BubbleKey()
103+
km.FilePicker.Prev = h.ckb.Prev.BubbleKey()
104+
km.FilePicker.Next = h.ckb.Next.BubbleKey()
105+
km.FilePicker.Select = h.kb.Select.BubbleKey()
106+
km.FilePicker.Submit = h.kb.Select.BubbleKey()
107+
// Note: `km.FilePicker.Open = h.ckb.Right.BubbleKey()` unset.
108+
109+
km.Text.Prev = h.ckb.Prev.BubbleKey()
110+
km.Text.Next = h.ckb.Next.BubbleKey()
111+
km.Text.Submit = h.kb.Select.BubbleKey()
112+
113+
km.Note.Prev = h.ckb.Prev.BubbleKey()
114+
km.Note.Next = h.ckb.Next.BubbleKey()
115+
km.Note.Submit = h.kb.Select.BubbleKey()
116+
117+
return km
118+
}

pkg/ui/menu/menu.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ func (m Model) Init() tea.Cmd {
7070
}
7171

7272
func (m *Model) addConfigEditor() {
73-
m.configeditor = configeditor.NewModel(m.cm.Cmd, m.cm.Theme)
73+
m.configeditor = configeditor.NewModel(
74+
m.cm.Cmd,
75+
themeToHuhTheme(m.cm.Theme),
76+
m.keyHandler.HuhKeyMap(),
77+
)
7478
}
7579

7680
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {

pkg/ui/menu/theme.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package menu
2+
3+
import (
4+
"github.com/charmbracelet/huh"
5+
"github.com/charmbracelet/lipgloss"
6+
7+
"github.com/macropower/kat/pkg/ui/theme"
8+
)
9+
10+
func themeToHuhTheme(t *theme.Theme) *huh.Theme {
11+
h := huh.ThemeBase()
12+
13+
h.Focused.Base = h.Focused.Base.BorderForeground(t.SelectedStyle.GetForeground())
14+
h.Focused.Card = h.Focused.Base
15+
h.Focused.Title = h.Focused.Title.Foreground(t.SelectedStyle.GetForeground()).Bold(true)
16+
h.Focused.NoteTitle = h.Focused.NoteTitle.Foreground(t.SelectedStyle.GetForeground()).Bold(true).MarginBottom(1)
17+
h.Focused.Directory = h.Focused.Directory.Foreground(t.SelectedSubtleStyle.GetForeground())
18+
h.Focused.Description = h.Focused.Description.Foreground(t.SelectedSubtleStyle.GetForeground())
19+
h.Focused.ErrorIndicator = h.Focused.ErrorIndicator.Foreground(t.ErrorTextStyle.GetForeground())
20+
h.Focused.ErrorMessage = h.Focused.ErrorMessage.Foreground(t.ErrorTextStyle.GetForeground())
21+
h.Focused.SelectSelector = h.Focused.SelectSelector.Foreground(t.SelectedStyle.GetForeground())
22+
h.Focused.NextIndicator = h.Focused.NextIndicator.Foreground(t.SelectedStyle.GetForeground())
23+
h.Focused.PrevIndicator = h.Focused.PrevIndicator.Foreground(t.SelectedStyle.GetForeground())
24+
h.Focused.Option = h.Focused.Option.Foreground(t.GenericTextStyle.GetBackground())
25+
h.Focused.MultiSelectSelector = h.Focused.MultiSelectSelector.Foreground(t.SelectedStyle.GetForeground())
26+
h.Focused.SelectedOption = h.Focused.SelectedOption.Foreground(t.SelectedStyle.GetForeground())
27+
h.Focused.SelectedPrefix = lipgloss.NewStyle().
28+
Foreground(t.SelectedStyle.GetForeground()).
29+
SetString("✓ ")
30+
h.Focused.UnselectedPrefix = lipgloss.NewStyle().
31+
Foreground(t.SubtleStyle.GetForeground()).
32+
SetString("• ")
33+
h.Focused.UnselectedOption = h.Focused.UnselectedOption.
34+
Foreground(t.GenericTextStyle.GetBackground())
35+
h.Focused.FocusedButton = h.Focused.FocusedButton.
36+
Foreground(t.LogoStyle.GetForeground()).
37+
Background(t.LogoStyle.GetBackground())
38+
h.Focused.Next = h.Focused.FocusedButton
39+
h.Focused.BlurredButton = h.Focused.BlurredButton.
40+
Foreground(t.LogoStyle.GetForeground()).
41+
Background(t.SubtleStyle.GetForeground())
42+
43+
h.Focused.TextInput.Cursor = h.Focused.TextInput.Cursor.Foreground(t.SelectedStyle.GetForeground())
44+
h.Focused.TextInput.Placeholder = h.Focused.TextInput.Placeholder.Foreground(t.SubtleStyle.GetForeground())
45+
h.Focused.TextInput.Prompt = h.Focused.TextInput.Prompt.Foreground(t.SelectedStyle.GetForeground())
46+
47+
h.Blurred = h.Focused
48+
h.Blurred.Base = h.Focused.Base.BorderStyle(lipgloss.HiddenBorder())
49+
h.Blurred.Card = h.Blurred.Base
50+
h.Blurred.NextIndicator = lipgloss.NewStyle()
51+
h.Blurred.PrevIndicator = lipgloss.NewStyle()
52+
53+
h.Group.Title = h.Focused.Title
54+
h.Group.Description = h.Focused.Description
55+
56+
return h
57+
}

0 commit comments

Comments
 (0)