mirror of
https://github.com/Maks1mS/bubbles.git
synced 2024-12-23 14:22:58 +03:00
feat: make the paginator key bindings customizable
This commit is contained in:
parent
aea42690e7
commit
85da9addba
@ -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,20 +20,49 @@ 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 Type
|
// Type configures how the pagination is rendered (Arabic, Dots).
|
||||||
Page int
|
Type Type
|
||||||
PerPage int
|
// Page is the current page number.
|
||||||
TotalPages int
|
Page int
|
||||||
ActiveDot string
|
// PerPage is the number of items per page.
|
||||||
InactiveDot string
|
PerPage int
|
||||||
ArabicFormat string
|
// TotalPages is the total number of pages.
|
||||||
|
TotalPages int
|
||||||
|
// ActiveDot is used to mark the current page under the Dots display type.
|
||||||
|
ActiveDot string
|
||||||
|
// InactiveDot is used to mark inactive pages under the Dots display type.
|
||||||
|
InactiveDot string
|
||||||
|
// ArabicFormat is the printf-style format to use for the Arabic display type.
|
||||||
|
ArabicFormat string
|
||||||
|
|
||||||
|
// KeyMap encodes the keybindings recognized by the widget.
|
||||||
|
KeyMap KeyMap
|
||||||
|
|
||||||
|
// Deprecated: customize KeyMap instead.
|
||||||
UsePgUpPgDownKeys bool
|
UsePgUpPgDownKeys bool
|
||||||
UseLeftRightKeys bool
|
// Deprecated: customize KeyMap instead.
|
||||||
UseUpDownKeys bool
|
UseLeftRightKeys bool
|
||||||
UseHLKeys bool
|
// Deprecated: customize KeyMap instead.
|
||||||
UseJKKeys bool
|
UseUpDownKeys bool
|
||||||
|
// Deprecated: customize KeyMap instead.
|
||||||
|
UseHLKeys bool
|
||||||
|
// Deprecated: customize KeyMap instead.
|
||||||
|
UseJKKeys bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetTotalPages is a helper function for calculating the total number of pages
|
// SetTotalPages is a helper function for calculating the total number of pages
|
||||||
@ -98,18 +128,14 @@ func (m Model) OnLastPage() bool {
|
|||||||
// New creates a new model with defaults.
|
// New creates a new model with defaults.
|
||||||
func New() Model {
|
func New() Model {
|
||||||
return Model{
|
return Model{
|
||||||
Type: Arabic,
|
Type: Arabic,
|
||||||
Page: 0,
|
Page: 0,
|
||||||
PerPage: 1,
|
PerPage: 1,
|
||||||
TotalPages: 1,
|
TotalPages: 1,
|
||||||
ActiveDot: "•",
|
KeyMap: DefaultKeyMap,
|
||||||
InactiveDot: "○",
|
ActiveDot: "•",
|
||||||
ArabicFormat: "%d/%d",
|
InactiveDot: "○",
|
||||||
UsePgUpPgDownKeys: true,
|
ArabicFormat: "%d/%d",
|
||||||
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.NextPage()
|
||||||
m.PrevPage()
|
case key.Matches(msg, m.KeyMap.PrevPage):
|
||||||
case "pgdown":
|
m.PrevPage()
|
||||||
m.NextPage()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if m.UseLeftRightKeys {
|
|
||||||
switch msg.String() {
|
|
||||||
case "left":
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user