aboutsummaryrefslogtreecommitdiffstats
path: root/commands/account
diff options
context:
space:
mode:
Diffstat (limited to 'commands/account')
-rw-r--r--commands/account/cf.go16
-rw-r--r--commands/account/clear.go25
-rw-r--r--commands/account/compose.go85
-rw-r--r--commands/account/connection.go2
-rw-r--r--commands/account/expand-folder.go8
-rw-r--r--commands/account/export-mbox.go25
-rw-r--r--commands/account/import-mbox.go19
-rw-r--r--commands/account/mkdir.go14
-rw-r--r--commands/account/next-folder.go29
-rw-r--r--commands/account/next-result.go8
-rw-r--r--commands/account/next.go57
-rw-r--r--commands/account/recover.go56
-rw-r--r--commands/account/rmdir.go28
-rw-r--r--commands/account/search.go4
-rw-r--r--commands/account/select.go22
-rw-r--r--commands/account/sort.go4
-rw-r--r--commands/account/split.go63
-rw-r--r--commands/account/view.go24
18 files changed, 159 insertions, 330 deletions
diff --git a/commands/account/cf.go b/commands/account/cf.go
index cd93ed29..59203a89 100644
--- a/commands/account/cf.go
+++ b/commands/account/cf.go
@@ -2,7 +2,6 @@ package account
import (
"errors"
- "strings"
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/commands"
@@ -11,7 +10,9 @@ import (
var history map[string]string
-type ChangeFolder struct{}
+type ChangeFolder struct {
+ Folder string `opt:"..." metavar:"<folder>"`
+}
func init() {
history = make(map[string]string)
@@ -26,24 +27,21 @@ func (ChangeFolder) Complete(args []string) []string {
return commands.GetFolders(args)
}
-func (ChangeFolder) Execute(args []string) error {
- if len(args) == 1 {
- return errors.New("Usage: cf <folder>")
- }
+func (c ChangeFolder) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
previous := acct.Directories().Selected()
- joinedArgs := strings.Join(args[1:], " ")
- if joinedArgs == "-" {
+
+ if c.Folder == "-" {
if dir, ok := history[acct.Name()]; ok {
acct.Directories().Select(dir)
} else {
return errors.New("No previous folder to return to")
}
} else {
- acct.Directories().Select(joinedArgs)
+ acct.Directories().Select(c.Folder)
}
history[acct.Name()] = previous
diff --git a/commands/account/clear.go b/commands/account/clear.go
index b67454dd..fd209d07 100644
--- a/commands/account/clear.go
+++ b/commands/account/clear.go
@@ -5,10 +5,11 @@ import (
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/lib/state"
- "git.sr.ht/~sircmpwn/getopt"
)
-type Clear struct{}
+type Clear struct {
+ Selected bool `opt:"-s"`
+}
func init() {
register(Clear{})
@@ -22,7 +23,7 @@ func (Clear) Complete(args []string) []string {
return nil
}
-func (Clear) Execute(args []string) error {
+func (c Clear) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
@@ -32,23 +33,7 @@ func (Clear) Execute(args []string) error {
return errors.New("Cannot perform action. Messages still loading")
}
- clearSelected := false
- opts, optind, err := getopt.Getopts(args, "s")
- if err != nil {
- return err
- }
-
- for _, opt := range opts {
- if opt.Option == 's' {
- clearSelected = true
- }
- }
-
- if len(args) != optind {
- return errors.New("Usage: clear [-s]")
- }
-
- if clearSelected {
+ if c.Selected {
defer store.Select(0)
}
store.ApplyClear()
diff --git a/commands/account/compose.go b/commands/account/compose.go
index 56079868..81eb3de0 100644
--- a/commands/account/compose.go
+++ b/commands/account/compose.go
@@ -12,15 +12,31 @@ import (
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/config"
- "git.sr.ht/~sircmpwn/getopt"
)
-type Compose struct{}
+type Compose struct {
+ Headers string `opt:"-H" action:"ParseHeader"`
+ Template string `opt:"-T"`
+ Edit bool `opt:"-e"`
+ NoEdit bool `opt:"-E"`
+ Body string `opt:"..." required:"false"`
+}
func init() {
register(Compose{})
}
+func (c *Compose) ParseHeader(arg string) error {
+ if strings.Contains(arg, ":") {
+ // ensure first colon is followed by a single space
+ re := regexp.MustCompile(`^(.*?):\s*(.*)`)
+ c.Headers += re.ReplaceAllString(arg, "$1: $2\r\n")
+ } else {
+ c.Headers += arg + ":\r\n"
+ }
+ return nil
+}
+
func (Compose) Aliases() []string {
return []string{"compose"}
}
@@ -29,20 +45,25 @@ func (Compose) Complete(args []string) []string {
return nil
}
-func (Compose) Execute(args []string) error {
- body, template, editHeaders, err := buildBody(args)
- if err != nil {
- return err
+func (c Compose) Execute(args []string) error {
+ if c.Headers != "" {
+ if c.Body != "" {
+ c.Body = c.Headers + "\r\n" + c.Body
+ } else {
+ c.Body = c.Headers + "\r\n\r\n"
+ }
}
+ if c.Template == "" {
+ c.Template = config.Templates.NewMessage
+ }
+ editHeaders := (config.Compose.EditHeaders || c.Edit) && !c.NoEdit
+
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
- if template == "" {
- template = config.Templates.NewMessage
- }
- msg, err := gomail.ReadMessage(strings.NewReader(body))
+ msg, err := gomail.ReadMessage(strings.NewReader(c.Body))
if errors.Is(err, io.EOF) { // completely empty
msg = &gomail.Message{Body: strings.NewReader("")}
} else if err != nil {
@@ -52,52 +73,10 @@ func (Compose) Execute(args []string) error {
composer, err := app.NewComposer(acct,
acct.AccountConfig(), acct.Worker(), editHeaders,
- template, &headers, nil, msg.Body)
+ c.Template, &headers, nil, msg.Body)
if err != nil {
return err
}
composer.Tab = app.NewTab(composer, "New email")
return nil
}
-
-func buildBody(args []string) (string, string, bool, error) {
- var body, template, headers string
- editHeaders := config.Compose.EditHeaders
- opts, optind, err := getopt.Getopts(args, "H:T:eE")
- if err != nil {
- return "", "", false, err
- }
- for _, opt := range opts {
- switch opt.Option {
- case 'H':
- if strings.Contains(opt.Value, ":") {
- // ensure first colon is followed by a single space
- re := regexp.MustCompile(`^(.*?):\s*(.*)`)
- headers += re.ReplaceAllString(opt.Value, "$1: $2") + "\n"
- } else {
- headers += opt.Value + ":\n"
- }
- case 'T':
- template = opt.Value
- case 'e':
- editHeaders = true
- case 'E':
- editHeaders = false
- }
- }
- posargs := args[optind:]
- if len(posargs) > 1 {
- return "", "", false, errors.New("Usage: compose [-H header] [-T template] [-e|-E] [body]")
- }
- if len(posargs) == 1 {
- body = posargs[0]
- }
- if headers != "" {
- if len(body) > 0 {
- body = headers + "\n" + body
- } else {
- body = headers + "\n\n"
- }
- }
- return body, template, editHeaders, nil
-}
diff --git a/commands/account/connection.go b/commands/account/connection.go
index 3b30dbda..b9cd887b 100644
--- a/commands/account/connection.go
+++ b/commands/account/connection.go
@@ -22,7 +22,7 @@ func (Connection) Complete(args []string) []string {
return nil
}
-func (Connection) Execute(args []string) error {
+func (c Connection) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
diff --git a/commands/account/expand-folder.go b/commands/account/expand-folder.go
index 29203adc..f26da70a 100644
--- a/commands/account/expand-folder.go
+++ b/commands/account/expand-folder.go
@@ -2,7 +2,6 @@ package account
import (
"errors"
- "fmt"
"git.sr.ht/~rjarry/aerc/app"
)
@@ -22,9 +21,6 @@ func (ExpandCollapseFolder) Complete(args []string) []string {
}
func (ExpandCollapseFolder) Execute(args []string) error {
- if len(args) > 1 {
- return expandCollapseFolderUsage(args[0])
- }
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
@@ -36,7 +32,3 @@ func (ExpandCollapseFolder) Execute(args []string) error {
}
return nil
}
-
-func expandCollapseFolderUsage(cmd string) error {
- return fmt.Errorf("Usage: %s", cmd)
-}
diff --git a/commands/account/export-mbox.go b/commands/account/export-mbox.go
index a17b8a29..00e03ca6 100644
--- a/commands/account/export-mbox.go
+++ b/commands/account/export-mbox.go
@@ -16,7 +16,9 @@ import (
"git.sr.ht/~rjarry/aerc/worker/types"
)
-type ExportMbox struct{}
+type ExportMbox struct {
+ Filename string `opt:"filename"`
+}
func init() {
register(ExportMbox{})
@@ -30,12 +32,7 @@ func (ExportMbox) Complete(args []string) []string {
return commands.CompletePath(filepath.Join(args...))
}
-func (ExportMbox) Execute(args []string) error {
- if len(args) != 2 {
- return exportFolderUsage(args[0])
- }
- filename := args[1]
-
+func (e ExportMbox) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
@@ -45,16 +42,16 @@ func (ExportMbox) Execute(args []string) error {
return errors.New("No message store selected")
}
- fi, err := os.Stat(filename)
+ fi, err := os.Stat(e.Filename)
if err == nil && fi.IsDir() {
if path := acct.SelectedDirectory(); path != "" {
if f := filepath.Base(path); f != "" {
- filename += f + ".mbox"
+ e.Filename = filepath.Join(e.Filename, f+".mbox")
}
}
}
- app.PushStatus("Exporting to "+filename, 10*time.Second)
+ app.PushStatus("Exporting to "+e.Filename, 10*time.Second)
// uids of messages to export
var uids []uint32
@@ -85,7 +82,7 @@ func (ExportMbox) Execute(args []string) error {
go func() {
defer log.PanicHandler()
- file, err := os.Create(filename)
+ file, err := os.Create(e.Filename)
if err != nil {
log.Errorf("failed to create file: %v", err)
app.PushError(err.Error())
@@ -147,7 +144,7 @@ func (ExportMbox) Execute(args []string) error {
}
retries++
}
- statusInfo := fmt.Sprintf("Exported %d of %d messages to %s.", ctr, total, filename)
+ statusInfo := fmt.Sprintf("Exported %d of %d messages to %s.", ctr, total, e.Filename)
app.PushStatus(statusInfo, 10*time.Second)
log.Debugf(statusInfo)
}()
@@ -155,10 +152,6 @@ func (ExportMbox) Execute(args []string) error {
return nil
}
-func exportFolderUsage(cmd string) error {
- return fmt.Errorf("Usage: %s <filename>", cmd)
-}
-
func sortMarkedUids(marked []uint32, store *lib.MessageStore) ([]uint32, error) {
lookup := map[uint32]bool{}
for _, uid := range marked {
diff --git a/commands/account/import-mbox.go b/commands/account/import-mbox.go
index 4ede8c26..b3ad2a08 100644
--- a/commands/account/import-mbox.go
+++ b/commands/account/import-mbox.go
@@ -18,7 +18,9 @@ import (
"git.sr.ht/~rjarry/aerc/worker/types"
)
-type ImportMbox struct{}
+type ImportMbox struct {
+ Filename string `opt:"filename"`
+}
func init() {
register(ImportMbox{})
@@ -32,12 +34,7 @@ func (ImportMbox) Complete(args []string) []string {
return commands.CompletePath(filepath.Join(args...))
}
-func (ImportMbox) Execute(args []string) error {
- if len(args) != 2 {
- return importFolderUsage(args[0])
- }
- filename := args[1]
-
+func (i ImportMbox) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
@@ -54,10 +51,10 @@ func (ImportMbox) Execute(args []string) error {
importFolder := func() {
defer log.PanicHandler()
- statusInfo := fmt.Sprintln("Importing", filename, "to folder", folder)
+ statusInfo := fmt.Sprintln("Importing", i.Filename, "to folder", folder)
app.PushStatus(statusInfo, 10*time.Second)
log.Debugf(statusInfo)
- f, err := os.Open(filename)
+ f, err := os.Open(i.Filename)
if err != nil {
app.PushError(err.Error())
return
@@ -147,7 +144,3 @@ func (ImportMbox) Execute(args []string) error {
return nil
}
-
-func importFolderUsage(cmd string) error {
- return fmt.Errorf("Usage: %s <filename>", cmd)
-}
diff --git a/commands/account/mkdir.go b/commands/account/mkdir.go
index 7a1f7b9e..9beeb01a 100644
--- a/commands/account/mkdir.go
+++ b/commands/account/mkdir.go
@@ -9,7 +9,9 @@ import (
"git.sr.ht/~rjarry/aerc/worker/types"
)
-type MakeDir struct{}
+type MakeDir struct {
+ Folder string `opt:"..." metavar:"<folder>"`
+}
func init() {
register(MakeDir{})
@@ -41,22 +43,18 @@ func (MakeDir) Complete(args []string) []string {
return inboxes
}
-func (MakeDir) Execute(args []string) error {
- if len(args) == 0 {
- return errors.New("Usage: :mkdir <name>")
- }
+func (m MakeDir) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
- name := strings.Join(args[1:], " ")
acct.Worker().PostAction(&types.CreateDirectory{
- Directory: name,
+ Directory: m.Folder,
}, func(msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
app.PushStatus("Directory created.", 10*time.Second)
- acct.Directories().Select(name)
+ acct.Directories().Select(m.Folder)
case *types.Error:
app.PushError(msg.Error.Error())
}
diff --git a/commands/account/next-folder.go b/commands/account/next-folder.go
index 2b23e6da..1b5651c8 100644
--- a/commands/account/next-folder.go
+++ b/commands/account/next-folder.go
@@ -2,13 +2,13 @@ package account
import (
"errors"
- "fmt"
- "strconv"
"git.sr.ht/~rjarry/aerc/app"
)
-type NextPrevFolder struct{}
+type NextPrevFolder struct {
+ Offset int `opt:"n" default:"1"`
+}
func init() {
register(NextPrevFolder{})
@@ -22,32 +22,15 @@ func (NextPrevFolder) Complete(args []string) []string {
return nil
}
-func (NextPrevFolder) Execute(args []string) error {
- if len(args) > 2 {
- return nextPrevFolderUsage(args[0])
- }
- var (
- n int = 1
- err error
- )
- if len(args) > 1 {
- n, err = strconv.Atoi(args[1])
- if err != nil {
- return nextPrevFolderUsage(args[0])
- }
- }
+func (np NextPrevFolder) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
if args[0] == "prev-folder" {
- acct.Directories().NextPrev(-n)
+ acct.Directories().NextPrev(-np.Offset)
} else {
- acct.Directories().NextPrev(n)
+ acct.Directories().NextPrev(np.Offset)
}
return nil
}
-
-func nextPrevFolderUsage(cmd string) error {
- return fmt.Errorf("Usage: %s [n]", cmd)
-}
diff --git a/commands/account/next-result.go b/commands/account/next-result.go
index ef4078da..e841899f 100644
--- a/commands/account/next-result.go
+++ b/commands/account/next-result.go
@@ -2,7 +2,6 @@ package account
import (
"errors"
- "fmt"
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/lib/ui"
@@ -23,9 +22,6 @@ func (NextPrevResult) Complete(args []string) []string {
}
func (NextPrevResult) Execute(args []string) error {
- if len(args) > 1 {
- return nextPrevResultUsage(args[0])
- }
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
@@ -45,7 +41,3 @@ func (NextPrevResult) Execute(args []string) error {
}
return nil
}
-
-func nextPrevResultUsage(cmd string) error {
- return fmt.Errorf("Usage: %s [<n>[%%]]", cmd)
-}
diff --git a/commands/account/next.go b/commands/account/next.go
index fff36bd1..14679b1b 100644
--- a/commands/account/next.go
+++ b/commands/account/next.go
@@ -2,7 +2,6 @@ package account
import (
"errors"
- "fmt"
"strconv"
"strings"
@@ -10,12 +9,28 @@ import (
"git.sr.ht/~rjarry/aerc/lib/ui"
)
-type NextPrevMsg struct{}
+type NextPrevMsg struct {
+ Amount int `opt:"n" default:"1" metavar:"<n>[%]" action:"ParseAmount"`
+ Percent bool
+}
func init() {
register(NextPrevMsg{})
}
+func (np *NextPrevMsg) ParseAmount(arg string) error {
+ if strings.HasSuffix(arg, "%") {
+ np.Percent = true
+ arg = strings.TrimSuffix(arg, "%")
+ }
+ i, err := strconv.ParseInt(arg, 10, 64)
+ if err != nil {
+ return err
+ }
+ np.Amount = int(i)
+ return nil
+}
+
func (NextPrevMsg) Aliases() []string {
return []string{"next", "next-message", "prev", "prev-message"}
}
@@ -24,42 +39,14 @@ func (NextPrevMsg) Complete(args []string) []string {
return nil
}
-func (NextPrevMsg) Execute(args []string) error {
- n, pct, err := ParseNextPrevMessage(args)
- if err != nil {
- return err
- }
+func (np NextPrevMsg) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
- return ExecuteNextPrevMessage(args, acct, pct, n)
-}
-
-func ParseNextPrevMessage(args []string) (int, bool, error) {
- if len(args) > 2 {
- return 0, false, nextPrevMessageUsage(args[0])
- }
- var (
- n int = 1
- err error
- pct bool
- )
- 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])
- if err != nil {
- return 0, false, nextPrevMessageUsage(args[0])
- }
- }
- return n, pct, nil
-}
-func ExecuteNextPrevMessage(args []string, acct *app.AccountView, pct bool, n int) error {
- if pct {
+ n := np.Amount
+ if np.Percent {
n = int(float64(acct.Messages().Height()) * (float64(n) / 100.0))
}
if args[0] == "prev-message" || args[0] == "prev" {
@@ -77,7 +64,3 @@ func ExecuteNextPrevMessage(args []string, acct *app.AccountView, pct bool, n in
}
return nil
}
-
-func nextPrevMessageUsage(cmd string) error {
- return fmt.Errorf("Usage: %s [<n>[%%]]", cmd)
-}
diff --git a/commands/account/recover.go b/commands/account/recover.go
index e0d1c6eb..dae3d807 100644
--- a/commands/account/recover.go
+++ b/commands/account/recover.go
@@ -10,10 +10,14 @@ import (
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/config"
- "git.sr.ht/~sircmpwn/getopt"
)
-type Recover struct{}
+type Recover struct {
+ Force bool `opt:"-f"`
+ Edit bool `opt:"-e"`
+ NoEdit bool `opt:"-E"`
+ File string `opt:"file"`
+}
func init() {
register(Recover{})
@@ -40,31 +44,14 @@ func (r Recover) Complete(args []string) []string {
}
func (r Recover) Execute(args []string) error {
- // Complete() expects to be passed only the arguments, not including the command name
- if len(Recover{}.Complete(args[1:])) == 0 {
- return errors.New("No messages to recover.")
- }
-
- force := false
- editHeaders := config.Compose.EditHeaders
-
- opts, optind, err := getopt.Getopts(args, r.Options())
+ file, err := os.Open(r.File)
if err != nil {
return err
}
- for _, opt := range opts {
- switch opt.Option {
- case 'f':
- force = true
- case 'e':
- editHeaders = true
- case 'E':
- editHeaders = false
- }
- }
-
- if len(args) <= optind {
- return errors.New("Usage: recover [-f] [-E|-e] <file>")
+ defer file.Close()
+ data, err := io.ReadAll(file)
+ if err != nil {
+ return err
}
acct := app.SelectedAccount()
@@ -72,22 +59,7 @@ func (r Recover) Execute(args []string) error {
return errors.New("No account selected")
}
- readData := func() ([]byte, error) {
- recoverFile, err := os.Open(args[optind])
- if err != nil {
- return nil, err
- }
- defer recoverFile.Close()
- data, err := io.ReadAll(recoverFile)
- if err != nil {
- return nil, err
- }
- return data, nil
- }
- data, err := readData()
- if err != nil {
- return err
- }
+ editHeaders := (config.Compose.EditHeaders || r.Edit) && !r.NoEdit
composer, err := app.NewComposer(acct,
acct.AccountConfig(), acct.Worker(), editHeaders,
@@ -98,8 +70,8 @@ func (r Recover) Execute(args []string) error {
composer.Tab = app.NewTab(composer, "Recovered")
// remove file if force flag is set
- if force {
- err = os.Remove(args[optind])
+ if r.Force {
+ err = os.Remove(r.File)
if err != nil {
return err
}
diff --git a/commands/account/rmdir.go b/commands/account/rmdir.go
index 3b109a67..36bfc699 100644
--- a/commands/account/rmdir.go
+++ b/commands/account/rmdir.go
@@ -4,13 +4,13 @@ import (
"errors"
"time"
- "git.sr.ht/~sircmpwn/getopt"
-
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/worker/types"
)
-type RemoveDir struct{}
+type RemoveDir struct {
+ Force bool `opt:"-f"`
+}
func init() {
register(RemoveDir{})
@@ -24,30 +24,14 @@ func (RemoveDir) Complete(args []string) []string {
return nil
}
-func (RemoveDir) Execute(args []string) error {
+func (r RemoveDir) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
- force := false
-
- opts, optind, err := getopt.Getopts(args, "f")
- if err != nil {
- return err
- }
- for _, opt := range opts {
- if opt.Option == 'f' {
- force = true
- }
- }
-
- if len(args) != optind {
- return errors.New("Usage: rmdir [-f]")
- }
-
// Check for any messages in the directory.
- if !acct.Messages().Empty() && !force {
+ if !acct.Messages().Empty() && !r.Force {
return errors.New("Refusing to remove non-empty directory; use -f")
}
@@ -80,7 +64,7 @@ func (RemoveDir) Execute(args []string) error {
acct.Worker().PostAction(&types.RemoveDirectory{
Directory: curDir,
- Quiet: force,
+ Quiet: r.Force,
}, func(msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
diff --git a/commands/account/search.go b/commands/account/search.go
index 51bd3042..7b98d98b 100644
--- a/commands/account/search.go
+++ b/commands/account/search.go
@@ -12,7 +12,9 @@ import (
"git.sr.ht/~rjarry/aerc/worker/types"
)
-type SearchFilter struct{}
+type SearchFilter struct {
+ Unused struct{} `opt:"-"`
+}
func init() {
register(SearchFilter{})
diff --git a/commands/account/select.go b/commands/account/select.go
index aea17e0e..efd8f53b 100644
--- a/commands/account/select.go
+++ b/commands/account/select.go
@@ -2,12 +2,13 @@ package account
import (
"errors"
- "strconv"
"git.sr.ht/~rjarry/aerc/app"
)
-type SelectMessage struct{}
+type SelectMessage struct {
+ Index int `opt:"n"`
+}
func init() {
register(SelectMessage{})
@@ -21,20 +22,7 @@ func (SelectMessage) Complete(args []string) []string {
return nil
}
-func (SelectMessage) Execute(args []string) error {
- if len(args) != 2 {
- return errors.New("Usage: :select-message <n>")
- }
- var (
- n int = 1
- err error
- )
- if len(args) > 1 {
- n, err = strconv.Atoi(args[1])
- if err != nil {
- return errors.New("Usage: :select-message <n>")
- }
- }
+func (s SelectMessage) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
@@ -42,6 +30,6 @@ func (SelectMessage) Execute(args []string) error {
if acct.Messages().Empty() {
return nil
}
- acct.Messages().Select(n)
+ acct.Messages().Select(s.Index)
return nil
}
diff --git a/commands/account/sort.go b/commands/account/sort.go
index d9eaf16a..2adfbf19 100644
--- a/commands/account/sort.go
+++ b/commands/account/sort.go
@@ -11,7 +11,9 @@ import (
"git.sr.ht/~rjarry/aerc/worker/types"
)
-type Sort struct{}
+type Sort struct {
+ Unused struct{} `opt:"-"`
+}
func init() {
register(Sort{})
diff --git a/commands/account/split.go b/commands/account/split.go
index 4a17b8b2..4774297f 100644
--- a/commands/account/split.go
+++ b/commands/account/split.go
@@ -8,12 +8,27 @@ import (
"git.sr.ht/~rjarry/aerc/app"
)
-type Split struct{}
+type Split struct {
+ Size int `opt:"n" required:"false" action:"ParseSize"`
+ Delta bool
+}
func init() {
register(Split{})
}
+func (s *Split) ParseSize(arg string) error {
+ i, err := strconv.ParseInt(arg, 10, 64)
+ if err != nil {
+ return err
+ }
+ s.Size = int(i)
+ if strings.HasPrefix(arg, "+") || strings.HasPrefix(arg, "-") {
+ s.Delta = true
+ }
+ return nil
+}
+
func (Split) Aliases() []string {
return []string{"split", "vsplit", "hsplit"}
}
@@ -22,10 +37,7 @@ func (Split) Complete(args []string) []string {
return nil
}
-func (Split) Execute(args []string) error {
- if len(args) > 2 {
- return errors.New("Usage: [v|h]split n")
- }
+func (s Split) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
@@ -34,45 +46,32 @@ func (Split) Execute(args []string) error {
if store == nil {
return errors.New("Cannot perform action. Messages still loading")
}
- n := 0
- if acct.SplitSize() == 0 {
- if args[0] == "split" {
- n = app.SelectedAccount().Messages().Height() / 4
+
+ if s.Size == 0 && acct.SplitSize() == 0 {
+ if args[0] == "split" || args[0] == "hsplit" {
+ s.Size = app.SelectedAccount().Messages().Height() / 4
} else {
- n = app.SelectedAccount().Messages().Width() / 2
+ s.Size = app.SelectedAccount().Messages().Width() / 2
}
}
-
- var err error
- if len(args) > 1 {
- delta := false
- if strings.HasPrefix(args[1], "+") || strings.HasPrefix(args[1], "-") {
- delta = true
- }
- n, err = strconv.Atoi(args[1])
- if err != nil {
- return errors.New("Usage: [v|h]split n")
- }
- if delta {
- n = acct.SplitSize() + n
- acct.SetSplitSize(n)
- return nil
- }
+ if s.Delta {
+ acct.SetSplitSize(acct.SplitSize() + s.Size)
+ return nil
}
- if n == acct.SplitSize() {
+ if s.Size == acct.SplitSize() {
// Repeated commands of the same size have the effect of
// toggling the split
- n = 0
+ s.Size = 0
}
- if n < 0 {
+ if s.Size < 0 {
// Don't allow split to go negative
- n = 1
+ s.Size = 1
}
switch args[0] {
case "split", "hsplit":
- return acct.Split(n)
+ return acct.Split(s.Size)
case "vsplit":
- return acct.Vsplit(n)
+ return acct.Vsplit(s.Size)
}
return nil
}
diff --git a/commands/account/view.go b/commands/account/view.go
index 8d5bc6ed..701a7738 100644
--- a/commands/account/view.go
+++ b/commands/account/view.go
@@ -5,10 +5,11 @@ import (
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/lib"
- "git.sr.ht/~sircmpwn/getopt"
)
-type ViewMessage struct{}
+type ViewMessage struct {
+ Peek bool `opt:"-p"`
+}
func init() {
register(ViewMessage{})
@@ -22,22 +23,7 @@ func (ViewMessage) Complete(args []string) []string {
return nil
}
-func (ViewMessage) Execute(args []string) error {
- peek := false
- opts, optind, err := getopt.Getopts(args, "p")
- if err != nil {
- return err
- }
-
- for _, opt := range opts {
- if opt.Option == 'p' {
- peek = true
- }
- }
-
- if len(args) != optind {
- return errors.New("Usage: view-message [-p]")
- }
+func (v ViewMessage) Execute(args []string) error {
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
@@ -58,7 +44,7 @@ func (ViewMessage) Execute(args []string) error {
app.PushError(msg.Error.Error())
return nil
}
- lib.NewMessageStoreView(msg, !peek && acct.UiConfig().AutoMarkRead,
+ lib.NewMessageStoreView(msg, !v.Peek && acct.UiConfig().AutoMarkRead,
store, app.CryptoProvider(), app.DecryptKeys,
func(view lib.MessageView, err error) {
if err != nil {