diff --git a/ui/toolkit/keymap.go b/ui/toolkit/keymap.go new file mode 100644 index 0000000..1e5813c --- /dev/null +++ b/ui/toolkit/keymap.go @@ -0,0 +1,30 @@ +package toolkit + +import "github.com/charmbracelet/bubbles/key" + +func (tk *ToolKit) KeymapAdd(id string, help string, keys ...string) { + keysview := "" + for i, k := range keys { + if i > 0 { + keysview += "/" + } + keysview += k + } + + binding := key.NewBinding( + key.WithKeys(keys...), + key.WithHelp(keysview, help), + ) + + tk.keybindings[id] = binding + + return +} + +func (tk *ToolKit) KeymapGet(id string) key.Binding { + if k, ok := tk.keybindings[id]; ok { + return k + } + + return key.NewBinding() +} diff --git a/ui/toolkit/toolkit.go b/ui/toolkit/toolkit.go index 6506a45..da4013a 100644 --- a/ui/toolkit/toolkit.go +++ b/ui/toolkit/toolkit.go @@ -1,6 +1,7 @@ package toolkit import ( + "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea" "github.com/mrusme/gobbs/ui/cmd" "github.com/mrusme/gobbs/ui/theme" @@ -18,6 +19,8 @@ type ToolKit struct { wh [2]int focused bool + keybindings map[string]key.Binding + viewfunc ViewFunc viewcache string } @@ -31,6 +34,8 @@ func New(winID string, t *theme.Theme, l *zap.SugaredLogger) *ToolKit { tk.wh = [2]int{0, 0} tk.focused = false + tk.keybindings = make(map[string]key.Binding) + return tk } diff --git a/ui/windows/postshow/postshow.go b/ui/windows/postshow/postshow.go index 1ffecb7..b3a7fb8 100644 --- a/ui/windows/postshow/postshow.go +++ b/ui/windows/postshow/postshow.go @@ -31,21 +31,9 @@ var ( BorderBottom(false) ) -type KeyMap struct { - Reply key.Binding -} - -var DefaultKeyMap = KeyMap{ - Reply: key.NewBinding( - key.WithKeys("r"), - key.WithHelp("r", "reply"), - ), -} - type Model struct { ctx *ctx.Ctx tk *toolkit.ToolKit - keymap KeyMap viewport viewport.Model a *aggregator.Aggregator @@ -71,7 +59,6 @@ func NewModel(c *ctx.Ctx) Model { c.Theme, c.Logger, ), - keymap: DefaultKeyMap, buffer: "", replyIDs: []string{}, @@ -80,6 +67,8 @@ func NewModel(c *ctx.Ctx) Model { m.tk.SetViewFunc(buildView) m.a, _ = aggregator.New(m.ctx) + m.tk.KeymapAdd("reply", "reply", "r") + return m } @@ -95,7 +84,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch { - case key.Matches(msg, m.keymap.Reply): + case key.Matches(msg, m.tk.KeymapGet("reply")): var replyToIdx int = 0 var err error