Implement next-message in msgview using account

This makes sure that the next-message command accepts the same arguments
in the account view and the msgview
This commit is contained in:
Jelle Besseling 2019-08-04 16:09:48 +02:00 committed by Drew DeVault
parent 3650b72ca6
commit 507c90537c
2 changed files with 37 additions and 28 deletions

View File

@ -24,8 +24,20 @@ func (_ NextPrevMsg) Complete(aerc *widgets.Aerc, args []string) []string {
} }
func (_ NextPrevMsg) Execute(aerc *widgets.Aerc, args []string) error { func (_ NextPrevMsg) Execute(aerc *widgets.Aerc, args []string) error {
var err, n, pct = ParseNextPrevMessage(args)
if err != nil {
return err
}
acct := aerc.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
return ExecuteNextPrevMessage(args, acct, pct, n)
}
func ParseNextPrevMessage(args []string) (error, int, bool) {
if len(args) > 2 { if len(args) > 2 {
return nextPrevMessageUsage(args[0]) return nextPrevMessageUsage(args[0]), 0, false
} }
var ( var (
n int = 1 n int = 1
@ -39,30 +51,28 @@ func (_ NextPrevMsg) Execute(aerc *widgets.Aerc, args []string) error {
} }
n, err = strconv.Atoi(args[1]) n, err = strconv.Atoi(args[1])
if err != nil { if err != nil {
return nextPrevMessageUsage(args[0]) return nextPrevMessageUsage(args[0]), 0, false
} }
} }
acct := aerc.SelectedAccount() return nil, n, pct
if acct == nil { }
return errors.New("No account selected")
} func ExecuteNextPrevMessage(args []string, acct *widgets.AccountView, pct bool, n int) error {
if pct { if pct {
n = int(float64(acct.Messages().Height()) * (float64(n) / 100.0)) n = int(float64(acct.Messages().Height()) * (float64(n) / 100.0))
} }
for ; n > 0; n-- { if args[0] == "prev-message" || args[0] == "prev" {
if args[0] == "prev-message" || args[0] == "prev" { store := acct.Store()
store := acct.Store() if store != nil {
if store != nil { store.NextPrev(-n)
store.Prev()
}
acct.Messages().Scroll()
} else {
store := acct.Store()
if store != nil {
store.Next()
}
acct.Messages().Scroll()
} }
acct.Messages().Scroll()
} else {
store := acct.Store()
if store != nil {
store.NextPrev(n)
}
acct.Messages().Scroll()
} }
return nil return nil
} }

View File

@ -1,8 +1,7 @@
package msgview package msgview
import ( import (
"errors" "git.sr.ht/~sircmpwn/aerc/commands/account"
"git.sr.ht/~sircmpwn/aerc/widgets" "git.sr.ht/~sircmpwn/aerc/widgets"
) )
@ -21,16 +20,16 @@ func (_ NextPrevMsg) Complete(aerc *widgets.Aerc, args []string) []string {
} }
func (_ NextPrevMsg) Execute(aerc *widgets.Aerc, args []string) error { func (_ NextPrevMsg) Execute(aerc *widgets.Aerc, args []string) error {
err, n, pct := account.ParseNextPrevMessage(args)
if err != nil {
return err
}
mv, _ := aerc.SelectedTab().(*widgets.MessageViewer) mv, _ := aerc.SelectedTab().(*widgets.MessageViewer)
acct := mv.SelectedAccount() acct := mv.SelectedAccount()
store := mv.Store() store := mv.Store()
if acct == nil { err = account.ExecuteNextPrevMessage(args, acct, pct, n)
return errors.New("No account selected") if err != nil {
} return err
if args[0] == "prev-message" || args[0] == "prev" {
store.Prev()
} else {
store.Next()
} }
nextMsg := store.Selected() nextMsg := store.Selected()
if nextMsg == nil { if nextMsg == nil {