Implement :next-message n%

This commit is contained in:
Drew DeVault 2019-03-15 21:41:18 -04:00
parent ef6178a12a
commit e780c6ee96
2 changed files with 16 additions and 1 deletions

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"strconv" "strconv"
"strings"
"git.sr.ht/~sircmpwn/aerc2/widgets" "git.sr.ht/~sircmpwn/aerc2/widgets"
) )
@ -14,7 +15,7 @@ func init() {
} }
func nextPrevMessageUsage(cmd string) error { func nextPrevMessageUsage(cmd string) error {
return errors.New(fmt.Sprintf("Usage: %s [n]", cmd)) return errors.New(fmt.Sprintf("Usage: %s [<n>[%]]", cmd))
} }
func NextPrevMessage(aerc *widgets.Aerc, args []string) error { func NextPrevMessage(aerc *widgets.Aerc, args []string) error {
@ -24,14 +25,22 @@ func NextPrevMessage(aerc *widgets.Aerc, args []string) error {
var ( var (
n int = 1 n int = 1
err error err error
pct bool
) )
if len(args) > 1 { if len(args) > 1 {
if strings.HasSuffix(args[1], "%") {
pct = true
args[1] = args[1][:len(args[1])-1]
}
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])
} }
} }
acct := aerc.SelectedAccount() acct := aerc.SelectedAccount()
if pct {
n = int(float64(acct.Messages().Height()) * (float64(n) / 100.0))
}
for ; n > 0; n-- { for ; n > 0; n-- {
if args[0] == "prev-message" { if args[0] == "prev-message" {
acct.Messages().Prev() acct.Messages().Prev()

View file

@ -13,6 +13,7 @@ import (
type MessageList struct { type MessageList struct {
conf *config.AercConfig conf *config.AercConfig
logger *log.Logger logger *log.Logger
height int
onInvalidate func(d ui.Drawable) onInvalidate func(d ui.Drawable)
selected int selected int
spinner *Spinner spinner *Spinner
@ -45,6 +46,7 @@ func (ml *MessageList) Invalidate() {
} }
func (ml *MessageList) Draw(ctx *ui.Context) { func (ml *MessageList) Draw(ctx *ui.Context) {
ml.height = ctx.Height()
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault) ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
if ml.store == nil { if ml.store == nil {
@ -91,6 +93,10 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
} }
} }
func (ml *MessageList) Height() int {
return ml.height
}
func (ml *MessageList) SetStore(store *lib.MessageStore) { func (ml *MessageList) SetStore(store *lib.MessageStore) {
ml.store = store ml.store = store
if store != nil { if store != nil {