Skip to content

Commit

Permalink
Replaced raw string key at
Browse files Browse the repository at this point in the history
 finder screen to constant keymap
  • Loading branch information
grrlopes committed Aug 15, 2023
1 parent e4f981c commit 63f3d6f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
8 changes: 4 additions & 4 deletions helper/keymaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,25 @@ var HotKeysHome = Keymap{
key.WithHelp("ctrl+s", "Prev Page"),
),
MoveUp: key.NewBinding(
key.WithKeys("shift+tab", "MoveUp"),
key.WithKeys("shift+tab", "up", "j"),
key.WithHelp("tab/Up", "↑"),
),
MoveDown: key.NewBinding(
key.WithKeys("tab", "MoveDown"),
key.WithKeys("tab", "down", "k"),
key.WithHelp("tab/Down", "↓"),
),
}

var HotKeysFinder = Keymap{
Enter: HotKeysHome.Enter,
Quit: key.NewBinding(
key.WithKeys("ctrl+c", "q"),
key.WithKeys("ctrl+c"),
key.WithHelp("ctrl+c", "quit"),
),
PageNext: HotKeysHome.PageNext,
PagePrev: HotKeysHome.PagePrev,
ResetFinder: key.NewBinding(
key.WithKeys("ctrl+r", "Reset"),
key.WithKeys("ctrl+r"),
key.WithHelp("ctrl+r", "Reset Finder"),
),
MoveUp: HotKeysHome.MoveUp,
Expand Down
16 changes: 16 additions & 0 deletions ui/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"strings"

"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/grrlopes/storydb/entity"
Expand All @@ -13,6 +14,7 @@ import (
type (
finderMsg []entity.Commands
finderCountMsg int
finderPagMsg struct{}
)

func finderCmd(filter textinput.Model, limit int, offset int) tea.Cmd {
Expand All @@ -35,6 +37,20 @@ func finderCount(filter string) tea.Cmd {
}
}

func finderPaginatorCmd(paginator paginator.Model, msg tea.Msg) (paginator.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
model, cmd := paginator.Update(msg)
cmds = append(cmds, cmd)
cmd = func() tea.Msg {
return finderPagMsg{}
}
cmds = append(cmds, cmd)
return model, tea.Batch(cmds...)
}

func finderUpdate(msg tea.Msg, m ModelHome) (*ModelHome, tea.Cmd) {
var cmd tea.Cmd

Expand Down
35 changes: 21 additions & 14 deletions ui/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/paginator"
"github.com/charmbracelet/bubbles/progress"
"github.com/charmbracelet/bubbles/textinput"
Expand Down Expand Up @@ -114,27 +115,30 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
m.home.PageTotal = len(msg)
case tea.KeyMsg:
if m.home.Finder.Focused() {
switch msg.String() {
case "up", "k", "shift+tab":
if m.home.Cursor > 0 {
m.home.Content = "arrow"
m.home.Cursor--
}
case "down", "j", "tab":
switch {
case key.Matches(msg, helper.HotKeysFinder.Enter):
return &m, tea.Quit
case key.Matches(msg, helper.HotKeysFinder.PageNext):
m.home.Cursor = 0
case key.Matches(msg, helper.HotKeysFinder.ResetFinder):
m.home.Finder.Reset()
case key.Matches(msg, helper.HotKeysFinder.PagePrev):
m.home.Cursor = 0
case key.Matches(msg, helper.HotKeysFinder.MoveDown):
if m.home.Cursor < m.home.PageTotal-1 {
m.home.Content = "arrow"
m.home.Cursor++
}
case "enter":
return &m, tea.Quit
case "ctrl+r":
m.home.Finder.Reset()
}
if msg.String() == "ctrl+c" {
case key.Matches(msg, helper.HotKeysHome.MoveUp):
if m.home.Cursor > 0 {
m.home.Content = "arrow"
m.home.Cursor--
}
case key.Matches(msg, helper.HotKeysFinder.Quit):
m.home.Finder.Reset()
m.home.Finder.Blur()
}
*m.home.Pagination, cmd = m.home.Pagination.Update(msg)
*m.home.Pagination, cmd = finderPaginatorCmd(*m.home.Pagination, msg)
cmds = append(cmds, cmd)
m.home.Finder, cmd = m.home.Finder.Update(msg)
cmd = finderCount(m.home.Finder.Value())
Expand Down Expand Up @@ -170,6 +174,9 @@ func (m ModelHome) Update(msg tea.Msg) (*ModelHome, tea.Cmd) {
case "f":
m.home.Finder.Focus()
m.home.Pagination.Page = 0
m.home.Cursor = 0
cmd = finderCmd(m.home.Finder, m.home.Viewport.Height-2, 1)
cmds = append(cmds, cmd)
}
}
case tea.WindowSizeMsg:
Expand Down

0 comments on commit 63f3d6f

Please sign in to comment.