From 3587cb9ccceab4fc7bee839e3454323c1512cb2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Tue, 3 Jan 2023 15:16:34 -0500 Subject: [PATCH] Implemented draft Keymap management --- ui/toolkit/keymap.go | 30 ++++++++++++++++++++++++++++++ ui/toolkit/toolkit.go | 5 +++++ ui/windows/postshow/postshow.go | 17 +++-------------- 3 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 ui/toolkit/keymap.go 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