Add more keybinding options to pager

This commit is contained in:
Christian Rocha 2020-04-09 16:47:12 -04:00
parent b931aeaf0f
commit 821de006ee
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018

View File

@ -28,6 +28,10 @@ type Model struct {
InactiveDot string
ArabicFormat string
RTL bool
UseLeftRightKeys bool
UseUpDownKeys bool
UseHLKeys bool
UseJKKeys bool
}
// SetTotalPages is a helper method for calculatng the total number of pages
@ -45,6 +49,18 @@ func (m *Model) SetTotalPages(items int) int {
return n
}
func (m *Model) prevPage() {
if m.Page > 0 {
m.Page--
}
}
func (m *Model) nextPage() {
if m.Page < m.TotalPages-1 {
m.Page++
}
}
// NewModel creates a new model with defaults
func NewModel() Model {
return Model{
@ -55,28 +71,51 @@ func NewModel() Model {
InactiveDot: "○",
ArabicFormat: "%d/%d",
RTL: false,
UseLeftRightKeys: true,
UseUpDownKeys: false,
UseHLKeys: true,
UseJKKeys: false,
}
}
// Update is the Tea update function which binds keystrokes to pagination
func Update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
m, ok := model.(Model)
if !ok {
return tea.ModelAssertionErr, nil
}
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if m.UseLeftRightKeys {
switch msg.String() {
case "left":
if m.Page > 0 {
m.Page--
}
m.prevPage()
case "right":
if m.Page < m.TotalPages-1 {
m.Page++
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()
}
}
}
return m, nil
}