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
|
package toolkit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/charmbracelet/bubbles/key"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
"github.com/mrusme/gobbs/ui/cmd"
|
"github.com/mrusme/gobbs/ui/cmd"
|
||||||
"github.com/mrusme/gobbs/ui/theme"
|
"github.com/mrusme/gobbs/ui/theme"
|
||||||
@ -18,6 +19,8 @@ type ToolKit struct {
|
|||||||
wh [2]int
|
wh [2]int
|
||||||
focused bool
|
focused bool
|
||||||
|
|
||||||
|
keybindings map[string]key.Binding
|
||||||
|
|
||||||
viewfunc ViewFunc
|
viewfunc ViewFunc
|
||||||
viewcache string
|
viewcache string
|
||||||
}
|
}
|
||||||
@ -31,6 +34,8 @@ func New(winID string, t *theme.Theme, l *zap.SugaredLogger) *ToolKit {
|
|||||||
tk.wh = [2]int{0, 0}
|
tk.wh = [2]int{0, 0}
|
||||||
tk.focused = false
|
tk.focused = false
|
||||||
|
|
||||||
|
tk.keybindings = make(map[string]key.Binding)
|
||||||
|
|
||||||
return tk
|
return tk
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,21 +31,9 @@ var (
|
|||||||
BorderBottom(false)
|
BorderBottom(false)
|
||||||
)
|
)
|
||||||
|
|
||||||
type KeyMap struct {
|
|
||||||
Reply key.Binding
|
|
||||||
}
|
|
||||||
|
|
||||||
var DefaultKeyMap = KeyMap{
|
|
||||||
Reply: key.NewBinding(
|
|
||||||
key.WithKeys("r"),
|
|
||||||
key.WithHelp("r", "reply"),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
|
|
||||||
type Model struct {
|
type Model struct {
|
||||||
ctx *ctx.Ctx
|
ctx *ctx.Ctx
|
||||||
tk *toolkit.ToolKit
|
tk *toolkit.ToolKit
|
||||||
keymap KeyMap
|
|
||||||
viewport viewport.Model
|
viewport viewport.Model
|
||||||
|
|
||||||
a *aggregator.Aggregator
|
a *aggregator.Aggregator
|
||||||
@ -71,7 +59,6 @@ func NewModel(c *ctx.Ctx) Model {
|
|||||||
c.Theme,
|
c.Theme,
|
||||||
c.Logger,
|
c.Logger,
|
||||||
),
|
),
|
||||||
keymap: DefaultKeyMap,
|
|
||||||
|
|
||||||
buffer: "",
|
buffer: "",
|
||||||
replyIDs: []string{},
|
replyIDs: []string{},
|
||||||
@ -80,6 +67,8 @@ func NewModel(c *ctx.Ctx) Model {
|
|||||||
m.tk.SetViewFunc(buildView)
|
m.tk.SetViewFunc(buildView)
|
||||||
m.a, _ = aggregator.New(m.ctx)
|
m.a, _ = aggregator.New(m.ctx)
|
||||||
|
|
||||||
|
m.tk.KeymapAdd("reply", "reply", "r")
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +84,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch {
|
switch {
|
||||||
|
|
||||||
case key.Matches(msg, m.keymap.Reply):
|
case key.Matches(msg, m.tk.KeymapGet("reply")):
|
||||||
var replyToIdx int = 0
|
var replyToIdx int = 0
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user