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

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