Debian Codegraph @

commands/account/next.go

raw
1 package account
2
3 import (
4 "errors"
5 "fmt"
6 "strconv"
7 "strings"
8
9 "git.sr.ht/~rjarry/aerc/app"
10 "git.sr.ht/~rjarry/aerc/commands"
11 "git.sr.ht/~rjarry/aerc/lib"
12 "git.sr.ht/~rjarry/aerc/lib/ui"
13 "git.sr.ht/~rjarry/aerc/models"
14 "git.sr.ht/~rjarry/aerc/worker/types"
15 )
16
17 type NextPrevMsg struct {
18 Amount int `opt:"n" minus:"true" default:"1" metavar:"<n>[%]" action:"ParseAmount"`
19 Percent bool
20 }
21
22 func init() {
23 commands.Register(NextPrevMsg{})
24 }
25
26 func (NextPrevMsg) Description() string {
27 return "Select the next or previous message in the message list."
28 }
29
30 func (NextPrevMsg) Context() commands.CommandContext {
31 return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER
32 }
33
34 func (np *NextPrevMsg) ParseAmount(arg string) error {
35 if strings.HasSuffix(arg, "%") {
36 np.Percent = true
37 arg = strings.TrimSuffix(arg, "%")
38 }
39 i, err := strconv.ParseInt(arg, 10, 64)
40 if err != nil {
41 return err
42 }
43 np.Amount = int(i)
44 return nil
45 }
46
47 func (NextPrevMsg) Aliases() []string {
48 return []string{"next", "next-message", "prev", "prev-message"}
49 }
50
51 func (np NextPrevMsg) Execute(args []string) error {
52 acct := app.SelectedAccount()
53 if acct == nil {
54 return errors.New("No account selected")
55 }
56 store := acct.Store()
57 if store == nil {
58 return fmt.Errorf("No message store set.")
59 }
60
61 n := np.Amount
62 if np.Percent {
63 n = int(float64(acct.Messages().Height()) * (float64(n) / 100.0))
64 }
65 if args[0] == "prev-message" || args[0] == "prev" {
66 store.NextPrev(-n)
67 } else {
68 store.NextPrev(n)
69 }
70
71 if mv, ok := app.SelectedTabContent().(*app.MessageViewer); ok {
72 reloadViewer := func(nextMsg *models.MessageInfo) {
73 if nextMsg.Error != nil {
74 app.PushError(nextMsg.Error.Error())
75 return
76 }
77 lib.NewMessageStoreView(nextMsg, mv.MessageView().SeenFlagSet(),
78 store, app.CryptoProvider(), app.DecryptKeys,
79 func(view lib.MessageView, err error) {
80 if err != nil {
81 app.PushError(err.Error())
82 return
83 }
84 nextMv, err := app.NewMessageViewer(acct, view)
85 if err != nil {
86 app.PushError(err.Error())
87 return
88 }
89 app.ReplaceTab(mv, nextMv,
90 nextMsg.Envelope.Subject, true)
91 })
92 }
93 if nextMsg := store.Selected(); nextMsg != nil {
94 reloadViewer(nextMsg)
95 } else {
96 store.FetchHeaders([]models.UID{store.SelectedUid()},
97 func(msg types.WorkerMessage) {
98 if m, ok := msg.(*types.MessageInfo); ok {
99 reloadViewer(m.Info)
100 }
101 })
102 }
103 }
104
105 ui.Invalidate()
106
107 return nil
108 }