mirror of
https://github.com/mrusme/neonmodem.git
synced 2024-12-04 14:46:37 -05:00
Implemented draft Keymap management
This commit is contained in:
parent
8e57103040
commit
3587cb9ccc
30
ui/toolkit/keymap.go
Normal file
30
ui/toolkit/keymap.go
Normal file
@ -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()
|
||||
}
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user