feat: make the paginator key bindings customizable

This commit is contained in:
Raphael 'kena' Poss 2022-10-07 17:38:23 +02:00 committed by Maas Lalani
parent aea42690e7
commit 85da9addba

View File

@ -7,6 +7,7 @@ package paginator
import ( import (
"fmt" "fmt"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
) )
@ -19,19 +20,48 @@ const (
Dots Dots
) )
// KeyMap is the key bindings for different actions within the paginator.
type KeyMap struct {
PrevPage key.Binding
NextPage key.Binding
}
// DefaultKeyMap is the default set of key bindings for navigating and acting
// upon the paginator.
var DefaultKeyMap = KeyMap{
PrevPage: key.NewBinding(key.WithKeys("pgup", "left", "h")),
NextPage: key.NewBinding(key.WithKeys("pgdown", "right", "l")),
}
// Model is the Bubble Tea model for this user interface. // Model is the Bubble Tea model for this user interface.
type Model struct { type Model struct {
// Type configures how the pagination is rendered (Arabic, Dots).
Type Type Type Type
// Page is the current page number.
Page int Page int
// PerPage is the number of items per page.
PerPage int PerPage int
// TotalPages is the total number of pages.
TotalPages int TotalPages int
// ActiveDot is used to mark the current page under the Dots display type.
ActiveDot string ActiveDot string
// InactiveDot is used to mark inactive pages under the Dots display type.
InactiveDot string InactiveDot string
// ArabicFormat is the printf-style format to use for the Arabic display type.
ArabicFormat string ArabicFormat string
// KeyMap encodes the keybindings recognized by the widget.
KeyMap KeyMap
// Deprecated: customize KeyMap instead.
UsePgUpPgDownKeys bool UsePgUpPgDownKeys bool
// Deprecated: customize KeyMap instead.
UseLeftRightKeys bool UseLeftRightKeys bool
// Deprecated: customize KeyMap instead.
UseUpDownKeys bool UseUpDownKeys bool
// Deprecated: customize KeyMap instead.
UseHLKeys bool UseHLKeys bool
// Deprecated: customize KeyMap instead.
UseJKKeys bool UseJKKeys bool
} }
@ -102,14 +132,10 @@ func New() Model {
Page: 0, Page: 0,
PerPage: 1, PerPage: 1,
TotalPages: 1, TotalPages: 1,
KeyMap: DefaultKeyMap,
ActiveDot: "•", ActiveDot: "•",
InactiveDot: "○", InactiveDot: "○",
ArabicFormat: "%d/%d", ArabicFormat: "%d/%d",
UsePgUpPgDownKeys: true,
UseLeftRightKeys: true,
UseUpDownKeys: false,
UseHLKeys: true,
UseJKKeys: false,
} }
} }
@ -122,45 +148,11 @@ var NewModel = New
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
if m.UsePgUpPgDownKeys { switch {
switch msg.String() { case key.Matches(msg, m.KeyMap.NextPage):
case "pgup":
m.PrevPage()
case "pgdown":
m.NextPage() m.NextPage()
} case key.Matches(msg, m.KeyMap.PrevPage):
}
if m.UseLeftRightKeys {
switch msg.String() {
case "left":
m.PrevPage() m.PrevPage()
case "right":
m.NextPage()
}
}
if m.UseUpDownKeys {
switch msg.String() {
case "up":
m.PrevPage()
case "down":
m.NextPage()
}
}
if m.UseHLKeys {
switch msg.String() {
case "h":
m.PrevPage()
case "l":
m.NextPage()
}
}
if m.UseJKKeys {
switch msg.String() {
case "j":
m.PrevPage()
case "k":
m.NextPage()
}
} }
} }