Implement :{next,prev}-message

This commit is contained in:
Drew DeVault 2019-03-14 23:41:25 -04:00
parent 4c8feb9aa5
commit 24daef89e0
4 changed files with 70 additions and 3 deletions

View File

@ -13,13 +13,13 @@ func init() {
Register("prev-folder", NextPrevFolder)
}
func usage(cmd string) error {
func nextPrevFolderUsage(cmd string) error {
return errors.New(fmt.Sprintf("Usage: %s [n]", cmd))
}
func NextPrevFolder(aerc *widgets.Aerc, args []string) error {
if len(args) > 2 {
return usage(args[0])
return nextPrevFolderUsage(args[0])
}
var (
n int = 1
@ -28,7 +28,7 @@ func NextPrevFolder(aerc *widgets.Aerc, args []string) error {
if len(args) > 1 {
n, err = strconv.Atoi(args[1])
if err != nil {
return usage(args[0])
return nextPrevFolderUsage(args[0])
}
}
acct := aerc.SelectedAccount()

43
commands/next-message.go Normal file
View File

@ -0,0 +1,43 @@
package commands
import (
"errors"
"fmt"
"strconv"
"git.sr.ht/~sircmpwn/aerc2/widgets"
)
func init() {
Register("next-message", NextPrevMessage)
Register("prev-message", NextPrevMessage)
}
func nextPrevMessageUsage(cmd string) error {
return errors.New(fmt.Sprintf("Usage: %s [n]", cmd))
}
func NextPrevMessage(aerc *widgets.Aerc, args []string) error {
if len(args) > 2 {
return nextPrevMessageUsage(args[0])
}
var (
n int = 1
err error
)
if len(args) > 1 {
n, err = strconv.Atoi(args[1])
if err != nil {
return nextPrevMessageUsage(args[0])
}
}
acct := aerc.SelectedAccount()
for ; n > 0; n-- {
if args[0] == "prev-message" {
acct.Messages().Prev()
} else {
acct.Messages().Next()
}
}
return nil
}

View File

@ -172,6 +172,10 @@ func (acct *AccountView) Directories() *DirectoryList {
return acct.dirlist
}
func (acct *AccountView) Messages() *MessageList {
return acct.msglist
}
func (acct *AccountView) onMessage(msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:

View File

@ -180,3 +180,23 @@ func (ml *MessageList) SetStore(store *MessageStore) {
}
ml.Invalidate()
}
func (ml *MessageList) nextPrev(delta int) {
ml.selected += delta
if ml.selected < 0 {
ml.selected = len(ml.store.Uids) - 1
}
if ml.selected >= len(ml.store.Uids) {
ml.selected = 0
}
// TODO: scrolling
ml.Invalidate()
}
func (ml *MessageList) Next() {
ml.nextPrev(1)
}
func (ml *MessageList) Prev() {
ml.nextPrev(-1)
}