Skip to content

Commit

Permalink
fix: keymap interface (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
prgres committed May 28, 2024
1 parent 1721eb4 commit cb0ec89
Show file tree
Hide file tree
Showing 13 changed files with 433 additions and 120 deletions.
18 changes: 9 additions & 9 deletions ui/common/keymaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/charmbracelet/bubbles/key"
)

func NewEmptyKeyMap() help.KeyMap {
return NewKeyMap(
func NewEmptyHelp() help.KeyMap {
return NewHelp(
func() [][]key.Binding { return [][]key.Binding{} },
func() []key.Binding { return []key.Binding{} },
)
Expand All @@ -19,20 +19,20 @@ var KeyBindingBack = key.NewBinding(
key.WithHelp("escape", "back to previous view"),
)

type KeyMap struct {
type Help struct {
fullHelp func() [][]key.Binding
shortHelp func() []key.Binding
}

func NewKeyMap(fullHelp func() [][]key.Binding, shortHelp func() []key.Binding) KeyMap {
return KeyMap{
func NewHelp(fullHelp func() [][]key.Binding, shortHelp func() []key.Binding) Help {
return Help{
fullHelp: fullHelp,
shortHelp: shortHelp,
}
}

func (km KeyMap) With(kb key.Binding) KeyMap {
return NewKeyMap(
func (km Help) With(kb key.Binding) Help {
return NewHelp(
func() [][]key.Binding {
return append(km.FullHelp(), []key.Binding{kb})
},
Expand All @@ -42,11 +42,11 @@ func (km KeyMap) With(kb key.Binding) KeyMap {
)
}

func (km KeyMap) FullHelp() [][]key.Binding {
func (km Help) FullHelp() [][]key.Binding {
return km.fullHelp()
}

func (km KeyMap) ShortHelp() []key.Binding {
func (km Help) ShortHelp() []key.Binding {
return km.shortHelp()
}

Expand Down
2 changes: 1 addition & 1 deletion ui/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type UIElement interface {
BubblesElem

Id() Id
KeyMap() help.KeyMap
Help() help.KeyMap
SetSize(Size)
Size() Size
}
Expand Down
88 changes: 78 additions & 10 deletions ui/components/folders-list/folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package folderslist

import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
Expand All @@ -20,24 +21,84 @@ type Model struct {
log *log.Logger
SelectedFolder clickup.Folder
folders []clickup.Folder
keyMap KeyMap
}

type KeyMap struct {
CursorUp key.Binding
CursorUpAndSelect key.Binding
CursorDown key.Binding
CursorDownAndSelect key.Binding
Select key.Binding
}

func (m Model) KeyMap() KeyMap {
return m.keyMap
}

func (m Model) Id() common.Id {
return m.id
}

func (m Model) KeyMap() help.KeyMap {
return common.NewKeyMap(
m.list.FullHelp,
m.list.ShortHelp,
func DefaultKeyMap() KeyMap {
return KeyMap{
CursorUp: key.NewBinding(
key.WithKeys("k", "up"),
key.WithHelp("k, up", "up"),
),
CursorUpAndSelect: key.NewBinding(
key.WithKeys("K", "shift+up"),
key.WithHelp("K, shift+up", "up and select"),
),
CursorDown: key.NewBinding(
key.WithKeys("j", "down"),
key.WithHelp("j, down", "down"),
),
CursorDownAndSelect: key.NewBinding(
key.WithKeys("J", "shift+down"),
key.WithHelp("J, down", "down and select"),
),
Select: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "select"),
),
}
}

func (m Model) Help() help.KeyMap {
return common.NewHelp(
func() [][]key.Binding {
return append(
m.list.FullHelp(),
[]key.Binding{
m.keyMap.CursorUp,
m.keyMap.CursorUpAndSelect,
m.keyMap.CursorDown,
m.keyMap.CursorDownAndSelect,
m.keyMap.Select,
},
)
},
func() []key.Binding {
return append(
m.list.ShortHelp(),
m.keyMap.CursorUp,
m.keyMap.CursorDown,
m.keyMap.Select,
)
},
).With(common.KeyBindingBack)
}

func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
l := list.New([]list.Item{},
list.NewDefaultDelegate(),
0, 0)

l.KeyMap.Quit.Unbind()
l.KeyMap.CursorUp.Unbind()
l.KeyMap.CursorDown.Unbind()

l.SetShowHelp(false)
l.Title = "Folders"

Expand All @@ -49,6 +110,7 @@ func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
ctx: ctx,
SelectedFolder: clickup.Folder{},
folders: []clickup.Folder{},
keyMap: DefaultKeyMap(),
log: log,
}
}
Expand Down Expand Up @@ -77,8 +139,8 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {

switch msg := msg.(type) {
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "enter":
switch {
case key.Matches(msg, m.keyMap.Select):
if m.list.SelectedItem() == nil {
m.log.Info("List is empty")
break
Expand All @@ -88,8 +150,11 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
m.SelectedFolder = selectedFolder
return FolderChangeCmd(selectedFolder.Id)

case "J", "shift+down":
m.list.CursorDown()
case key.Matches(msg, m.keyMap.CursorUp):
m.list.CursorUp()

case key.Matches(msg, m.keyMap.CursorUpAndSelect):
m.list.CursorUp()
if m.list.SelectedItem() == nil {
m.log.Info("List is empty")
break
Expand All @@ -99,8 +164,11 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
m.SelectedFolder = selectedFolder
return common.FolderPreviewCmd(selectedFolder.Id)

case "K", "shift+up":
m.list.CursorUp()
case key.Matches(msg, m.keyMap.CursorDown):
m.list.CursorDown()

case key.Matches(msg, m.keyMap.CursorDownAndSelect):
m.list.CursorDown()
if m.list.SelectedItem() == nil {
m.log.Info("List is empty")
break
Expand Down
83 changes: 75 additions & 8 deletions ui/components/lists-list/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package listslist

import (
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
Expand All @@ -20,6 +21,7 @@ type Model struct {
log *log.Logger
SelectedList clickup.List
lists []clickup.List
keyMap KeyMap
}

func (m Model) Id() common.Id {
Expand All @@ -30,7 +32,11 @@ func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
l := list.New([]list.Item{},
list.NewDefaultDelegate(),
0, 0)

l.KeyMap.Quit.Unbind()
l.KeyMap.CursorUp.Unbind()
l.KeyMap.CursorDown.Unbind()

l.SetShowHelp(false)
l.Title = "Lists"

Expand All @@ -46,10 +52,65 @@ func InitialModel(ctx *context.UserContext, logger *log.Logger) Model {
}
}

func (m Model) KeyMap() help.KeyMap {
return common.NewKeyMap(
m.list.FullHelp,
m.list.ShortHelp,
type KeyMap struct {
CursorUp key.Binding
CursorUpAndSelect key.Binding
CursorDown key.Binding
CursorDownAndSelect key.Binding
Select key.Binding
}

func DefaultKeyMap() KeyMap {
return KeyMap{
CursorUp: key.NewBinding(
key.WithKeys("k", "up"),
key.WithHelp("k, up", "up"),
),
CursorUpAndSelect: key.NewBinding(
key.WithKeys("K", "shift+up"),
key.WithHelp("K, shift+up", "up and select"),
),
CursorDown: key.NewBinding(
key.WithKeys("j", "down"),
key.WithHelp("j, down", "down"),
),
CursorDownAndSelect: key.NewBinding(
key.WithKeys("J", "shift+down"),
key.WithHelp("J, down", "down and select"),
),
Select: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "select"),
),
}
}

func (m Model) KeyMap() KeyMap {
return m.keyMap
}

func (m Model) Help() help.KeyMap {
return common.NewHelp(
func() [][]key.Binding {
return append(
m.list.FullHelp(),
[]key.Binding{
m.keyMap.CursorUp,
m.keyMap.CursorUpAndSelect,
m.keyMap.CursorDown,
m.keyMap.CursorDownAndSelect,
m.keyMap.Select,
},
)
},
func() []key.Binding {
return append(
m.list.ShortHelp(),
m.keyMap.CursorUp,
m.keyMap.CursorDown,
m.keyMap.Select,
)
},
).With(common.KeyBindingBack)
}

Expand All @@ -70,8 +131,8 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {

switch msg := msg.(type) {
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "enter":
switch {
case key.Matches(msg, m.keyMap.Select):
if m.list.SelectedItem() == nil {
m.log.Info("List is empty")
break
Expand All @@ -81,7 +142,10 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
m.SelectedList = selectedList
return ListChangedCmd(m.SelectedList.Id)

case "J", "shift+down":
case key.Matches(msg, m.keyMap.CursorDown):
m.list.CursorDown()

case key.Matches(msg, m.keyMap.CursorDownAndSelect):
m.list.CursorDown()
if m.list.SelectedItem() == nil {
m.log.Info("List is empty")
Expand All @@ -92,7 +156,10 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
m.SelectedList = selectedList
return common.ListPreviewCmd(m.SelectedList.Id)

case "K", "shift+up":
case key.Matches(msg, m.keyMap.CursorUp):
m.list.CursorUp()

case key.Matches(msg, m.keyMap.CursorUpAndSelect):
m.list.CursorUp()
if m.list.SelectedItem() == nil {
m.log.Info("List is empty")
Expand Down
Loading

0 comments on commit cb0ec89

Please sign in to comment.