aboutsummaryrefslogtreecommitdiffstats
path: root/commands/compose
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-10-10 00:08:31 +0200
committerRobin Jarry <robin@jarry.cc>2023-10-10 11:38:01 +0200
commitbc176bd61ba726351a489cabf4da16a47dc5ec3b (patch)
treebbf06f731592d072f3d6f76f1648d61989375f2e /commands/compose
parent598e4a5803578ab3e291f232d6aad31b4efd8ea4 (diff)
downloadaerc-bc176bd61ba726351a489cabf4da16a47dc5ec3b.tar.gz
app: export global functions
The single Aerc object is passed around in almost all command functions. This hinders readability. Store the single Aerc instance as a global variable. Export public functions from the app package to access methods of that object. Remove all explicit references to *app.Aerc and replace them with calls to these functions. For references to private/unexported fields and functions from within the app package, directly access the global aerc object. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'commands/compose')
-rw-r--r--commands/compose/abort.go8
-rw-r--r--commands/compose/attach-key.go6
-rw-r--r--commands/compose/attach.go38
-rw-r--r--commands/compose/cc-bcc.go6
-rw-r--r--commands/compose/detach.go10
-rw-r--r--commands/compose/edit.go6
-rw-r--r--commands/compose/encrypt.go6
-rw-r--r--commands/compose/header.go8
-rw-r--r--commands/compose/multipart.go8
-rw-r--r--commands/compose/next-field.go6
-rw-r--r--commands/compose/postpone.go24
-rw-r--r--commands/compose/send.go28
-rw-r--r--commands/compose/sign.go8
-rw-r--r--commands/compose/switch.go16
14 files changed, 89 insertions, 89 deletions
diff --git a/commands/compose/abort.go b/commands/compose/abort.go
index d6ceae6d..a11c06c5 100644
--- a/commands/compose/abort.go
+++ b/commands/compose/abort.go
@@ -16,17 +16,17 @@ func (Abort) Aliases() []string {
return []string{"abort"}
}
-func (Abort) Complete(aerc *app.Aerc, args []string) []string {
+func (Abort) Complete(args []string) []string {
return nil
}
-func (Abort) Execute(aerc *app.Aerc, args []string) error {
+func (Abort) Execute(args []string) error {
if len(args) != 1 {
return errors.New("Usage: abort")
}
- composer, _ := aerc.SelectedTabContent().(*app.Composer)
+ composer, _ := app.SelectedTabContent().(*app.Composer)
- aerc.RemoveTab(composer, true)
+ app.RemoveTab(composer, true)
return nil
}
diff --git a/commands/compose/attach-key.go b/commands/compose/attach-key.go
index 29237374..74e05bb7 100644
--- a/commands/compose/attach-key.go
+++ b/commands/compose/attach-key.go
@@ -16,16 +16,16 @@ func (AttachKey) Aliases() []string {
return []string{"attach-key"}
}
-func (AttachKey) Complete(aerc *app.Aerc, args []string) []string {
+func (AttachKey) Complete(args []string) []string {
return nil
}
-func (AttachKey) Execute(aerc *app.Aerc, args []string) error {
+func (AttachKey) Execute(args []string) error {
if len(args) != 1 {
return errors.New("Usage: attach-key")
}
- composer, _ := aerc.SelectedTabContent().(*app.Composer)
+ composer, _ := app.SelectedTabContent().(*app.Composer)
return composer.SetAttachKey(!composer.AttachKey())
}
diff --git a/commands/compose/attach.go b/commands/compose/attach.go
index fd84b4ea..fb533941 100644
--- a/commands/compose/attach.go
+++ b/commands/compose/attach.go
@@ -32,12 +32,12 @@ func (Attach) Aliases() []string {
return []string{"attach"}
}
-func (Attach) Complete(aerc *app.Aerc, args []string) []string {
+func (Attach) Complete(args []string) []string {
path := strings.Join(args, " ")
return commands.CompletePath(path)
}
-func (a Attach) Execute(aerc *app.Aerc, args []string) error {
+func (a Attach) Execute(args []string) error {
var (
menu bool
read bool
@@ -66,23 +66,23 @@ func (a Attach) Execute(aerc *app.Aerc, args []string) error {
args = args[optind:]
if menu {
- return a.openMenu(aerc, args)
+ return a.openMenu(args)
}
if read {
if len(args) < 2 {
return fmt.Errorf("Usage: :attach -r <name> <cmd> [args...]")
}
- return a.readCommand(aerc, args[0], args[1:])
+ return a.readCommand(args[0], args[1:])
}
if len(args) == 0 {
return fmt.Errorf("Usage: :attach <path>")
}
- return a.addPath(aerc, strings.Join(args, " "))
+ return a.addPath(strings.Join(args, " "))
}
-func (a Attach) addPath(aerc *app.Aerc, path string) error {
+func (a Attach) addPath(path string) error {
path = xdg.ExpandHome(path)
attachments, err := filepath.Glob(path)
if err != nil && errors.Is(err, filepath.ErrBadPattern) {
@@ -103,17 +103,17 @@ func (a Attach) addPath(aerc *app.Aerc, path string) error {
}
}
- composer, _ := aerc.SelectedTabContent().(*app.Composer)
+ composer, _ := app.SelectedTabContent().(*app.Composer)
for _, attach := range attachments {
log.Debugf("attaching '%s'", attach)
pathinfo, err := os.Stat(attach)
if err != nil {
log.Errorf("failed to stat file: %v", err)
- aerc.PushError(err.Error())
+ app.PushError(err.Error())
return err
} else if pathinfo.IsDir() && len(attachments) == 1 {
- aerc.PushError("Attachment must be a file, not a directory")
+ app.PushError("Attachment must be a file, not a directory")
return nil
}
@@ -121,15 +121,15 @@ func (a Attach) addPath(aerc *app.Aerc, path string) error {
}
if len(attachments) == 1 {
- aerc.PushSuccess(fmt.Sprintf("Attached %s", path))
+ app.PushSuccess(fmt.Sprintf("Attached %s", path))
} else {
- aerc.PushSuccess(fmt.Sprintf("Attached %d files", len(attachments)))
+ app.PushSuccess(fmt.Sprintf("Attached %d files", len(attachments)))
}
return nil
}
-func (a Attach) openMenu(aerc *app.Aerc, args []string) error {
+func (a Attach) openMenu(args []string) error {
filePickerCmd := config.Compose.FilePickerCmd
if filePickerCmd == "" {
return fmt.Errorf("no file-picker-cmd defined")
@@ -172,7 +172,7 @@ func (a Attach) openMenu(aerc *app.Aerc, args []string) error {
}
}()
- aerc.CloseDialog()
+ app.CloseDialog()
if err != nil {
log.Errorf("terminal closed with error: %v", err)
@@ -192,7 +192,7 @@ func (a Attach) openMenu(aerc *app.Aerc, args []string) error {
continue
}
log.Tracef("File picker attaches: %v", f)
- err := a.addPath(aerc, f)
+ err := a.addPath(f)
if err != nil {
log.Errorf("attach failed for file %s: %v", f, err)
}
@@ -200,8 +200,8 @@ func (a Attach) openMenu(aerc *app.Aerc, args []string) error {
}
}
- aerc.AddDialog(app.NewDialog(
- ui.NewBox(t, "File Picker", "", aerc.SelectedAccountUiConfig()),
+ app.AddDialog(app.NewDialog(
+ ui.NewBox(t, "File Picker", "", app.SelectedAccountUiConfig()),
// start pos on screen
func(h int) int {
return h / 8
@@ -215,7 +215,7 @@ func (a Attach) openMenu(aerc *app.Aerc, args []string) error {
return nil
}
-func (a Attach) readCommand(aerc *app.Aerc, name string, args []string) error {
+func (a Attach) readCommand(name string, args []string) error {
args = append([]string{"-c"}, args...)
cmd := exec.Command("sh", args...)
@@ -233,13 +233,13 @@ func (a Attach) readCommand(aerc *app.Aerc, name string, args []string) error {
mimeParams["name"] = name
- composer, _ := aerc.SelectedTabContent().(*app.Composer)
+ composer, _ := app.SelectedTabContent().(*app.Composer)
err = composer.AddPartAttachment(name, mimeType, mimeParams, reader)
if err != nil {
return errors.Wrap(err, "AddPartAttachment")
}
- aerc.PushSuccess(fmt.Sprintf("Attached %s", name))
+ app.PushSuccess(fmt.Sprintf("Attached %s", name))
return nil
}
diff --git a/commands/compose/cc-bcc.go b/commands/compose/cc-bcc.go
index 8f8d4aac..e1f94c1e 100644
--- a/commands/compose/cc-bcc.go
+++ b/commands/compose/cc-bcc.go
@@ -16,16 +16,16 @@ func (CC) Aliases() []string {
return []string{"cc", "bcc"}
}
-func (CC) Complete(aerc *app.Aerc, args []string) []string {
+func (CC) Complete(args []string) []string {
return nil
}
-func (CC) Execute(aerc *app.Aerc, args []string) error {
+func (CC) Execute(args []string) error {
var addrs string
if len(args) > 1 {
addrs = strings.Join(args[1:], " ")
}
- composer, _ := aerc.SelectedTabContent().(*app.Composer)
+ composer, _ := app.SelectedTabContent().(*app.Composer)
switch args[0] {
case "cc":
diff --git a/commands/compose/detach.go b/commands/compose/detach.go
index 014301f2..4847713f 100644
--- a/commands/compose/detach.go
+++ b/commands/compose/detach.go
@@ -17,14 +17,14 @@ func (Detach) Aliases() []string {
return []string{"detach"}
}
-func (Detach) Complete(aerc *app.Aerc, args []string) []string {
- composer, _ := aerc.SelectedTabContent().(*app.Composer)
+func (Detach) Complete(args []string) []string {
+ composer, _ := app.SelectedTabContent().(*app.Composer)
return composer.GetAttachments()
}
-func (Detach) Execute(aerc *app.Aerc, args []string) error {
+func (Detach) Execute(args []string) error {
var path string
- composer, _ := aerc.SelectedTabContent().(*app.Composer)
+ composer, _ := app.SelectedTabContent().(*app.Composer)
if len(args) > 1 {
path = strings.Join(args[1:], " ")
@@ -42,7 +42,7 @@ func (Detach) Execute(aerc *app.Aerc, args []string) error {
return err
}
- aerc.PushSuccess(fmt.Sprintf("Detached %s", path))
+ app.PushSuccess(fmt.Sprintf("Detached %s", path))
return nil
}
diff --git a/commands/compose/edit.go b/commands/compose/edit.go
index 485b3098..2948e964 100644
--- a/commands/compose/edit.go
+++ b/commands/compose/edit.go
@@ -18,12 +18,12 @@ func (Edit) Aliases() []string {
return []string{"edit"}
}
-func (Edit) Complete(aerc *app.Aerc, args []string) []string {
+func (Edit) Complete(args []string) []string {
return nil
}
-func (Edit) Execute(aerc *app.Aerc, args []string) error {
- composer, ok := aerc.SelectedTabContent().(*app.Composer)
+func (Edit) Execute(args []string) error {
+ composer, ok := app.SelectedTabContent().(*app.Composer)
if !ok {
return errors.New("only valid while composing")
}
diff --git a/commands/compose/encrypt.go b/commands/compose/encrypt.go
index 3c852dc4..b9094c5e 100644
--- a/commands/compose/encrypt.go
+++ b/commands/compose/encrypt.go
@@ -16,16 +16,16 @@ func (Encrypt) Aliases() []string {
return []string{"encrypt"}
}
-func (Encrypt) Complete(aerc *app.Aerc, args []string) []string {
+func (Encrypt) Complete(args []string) []string {
return nil
}
-func (Encrypt) Execute(aerc *app.Aerc, args []string) error {
+func (Encrypt) Execute(args []string) error {
if len(args) != 1 {
return errors.New("Usage: encrypt")
}
- composer, _ := aerc.SelectedTabContent().(*app.Composer)
+ composer, _ := app.SelectedTabContent().(*app.Composer)
composer.SetEncrypt(!composer.Encrypt())
return nil
diff --git a/commands/compose/header.go b/commands/compose/header.go
index e66df149..5c13fde5 100644
--- a/commands/compose/header.go
+++ b/commands/compose/header.go
@@ -33,11 +33,11 @@ func (Header) Options() string {
return "fd"
}
-func (Header) Complete(aerc *app.Aerc, args []string) []string {
- return commands.CompletionFromList(aerc, headers, args)
+func (Header) Complete(args []string) []string {
+ return commands.CompletionFromList(headers, args)
}
-func (h Header) Execute(aerc *app.Aerc, args []string) error {
+func (h Header) Execute(args []string) error {
opts, optind, err := getopt.Getopts(args, h.Options())
args = args[optind:]
if err == nil && len(args) < 1 {
@@ -58,7 +58,7 @@ func (h Header) Execute(aerc *app.Aerc, args []string) error {
}
}
- composer, _ := aerc.SelectedTabContent().(*app.Composer)
+ composer, _ := app.SelectedTabContent().(*app.Composer)
name := strings.TrimRight(args[0], ":")
diff --git a/commands/compose/multipart.go b/commands/compose/multipart.go
index 13ee69e5..0ad1dc4d 100644
--- a/commands/compose/multipart.go
+++ b/commands/compose/multipart.go
@@ -20,17 +20,17 @@ func (Multipart) Aliases() []string {
return []string{"multipart"}
}
-func (Multipart) Complete(aerc *app.Aerc, args []string) []string {
+func (Multipart) Complete(args []string) []string {
var completions []string
completions = append(completions, "-d")
for mime := range config.Converters {
completions = append(completions, mime)
}
- return commands.CompletionFromList(aerc, completions, args)
+ return commands.CompletionFromList(completions, args)
}
-func (a Multipart) Execute(aerc *app.Aerc, args []string) error {
- composer, ok := aerc.SelectedTabContent().(*app.Composer)
+func (a Multipart) Execute(args []string) error {
+ composer, ok := app.SelectedTabContent().(*app.Composer)
if !ok {
return fmt.Errorf(":multipart is only available on the compose::review screen")
}
diff --git a/commands/compose/next-field.go b/commands/compose/next-field.go
index d029b50a..be5f8e53 100644
--- a/commands/compose/next-field.go
+++ b/commands/compose/next-field.go
@@ -16,15 +16,15 @@ func (NextPrevField) Aliases() []string {
return []string{"next-field", "prev-field"}
}
-func (NextPrevField) Complete(aerc *app.Aerc, args []string) []string {
+func (NextPrevField) Complete(args []string) []string {
return nil
}
-func (NextPrevField) Execute(aerc *app.Aerc, args []string) error {
+func (NextPrevField) Execute(args []string) error {
if len(args) > 2 {
return nextPrevFieldUsage(args[0])
}
- composer, _ := aerc.SelectedTabContent().(*app.Composer)
+ composer, _ := app.SelectedTabContent().(*app.Composer)
if args[0] == "prev-field" {
composer.PrevField()
} else {
diff --git a/commands/compose/postpone.go b/commands/compose/postpone.go
index e33c9ab7..ac16d904 100644
--- a/commands/compose/postpone.go
+++ b/commands/compose/postpone.go
@@ -29,29 +29,29 @@ func (Postpone) Options() string {
return "t:"
}
-func (Postpone) CompleteOption(aerc *app.Aerc, r rune, arg string) []string {
+func (Postpone) CompleteOption(r rune, arg string) []string {
var valid []string
if r == 't' {
- valid = commands.GetFolders(aerc, []string{arg})
+ valid = commands.GetFolders([]string{arg})
}
- return commands.CompletionFromList(aerc, valid, []string{arg})
+ return commands.CompletionFromList(valid, []string{arg})
}
-func (Postpone) Complete(aerc *app.Aerc, args []string) []string {
+func (Postpone) Complete(args []string) []string {
return nil
}
-func (p Postpone) Execute(aerc *app.Aerc, args []string) error {
+func (p Postpone) Execute(args []string) error {
opts, optind, err := getopt.Getopts(args, p.Options())
if err != nil {
return err
}
- acct := aerc.SelectedAccount()
+ acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
- tab := aerc.SelectedTab()
+ tab := app.SelectedTab()
if tab == nil {
return errors.New("No tab selected")
}
@@ -104,17 +104,17 @@ func (p Postpone) Execute(aerc *app.Aerc, args []string) error {
errStr := <-errChan
if errStr != "" {
- aerc.PushError(errStr)
+ app.PushError(errStr)
return
}
handleErr := func(err error) {
- aerc.PushError(err.Error())
+ app.PushError(err.Error())
log.Errorf("Postponing failed: %v", err)
- aerc.NewTab(composer, tabName)
+ app.NewTab(composer, tabName)
}
- aerc.RemoveTab(composer, false)
+ app.RemoveTab(composer, false)
buf := &bytes.Buffer{}
err = composer.WriteMessage(header, buf)
@@ -131,7 +131,7 @@ func (p Postpone) Execute(aerc *app.Aerc, args []string) error {
}, func(msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
- aerc.PushStatus("Message postponed.", 10*time.Second)
+ app.PushStatus("Message postponed.", 10*time.Second)
composer.SetPostponed()
composer.Close()
case *types.Error:
diff --git a/commands/compose/send.go b/commands/compose/send.go
index c9b843a7..6e104529 100644
--- a/commands/compose/send.go
+++ b/commands/compose/send.go
@@ -36,11 +36,11 @@ func (Send) Aliases() []string {
return []string{"send"}
}
-func (Send) Complete(aerc *app.Aerc, args []string) []string {
+func (Send) Complete(args []string) []string {
return nil
}
-func (Send) Execute(aerc *app.Aerc, args []string) error {
+func (Send) Execute(args []string) error {
opts, optind, err := getopt.Getopts(args, "a:")
if err != nil {
return err
@@ -54,7 +54,7 @@ func (Send) Execute(aerc *app.Aerc, args []string) error {
archive = opt.Value
}
}
- tab := aerc.SelectedTab()
+ tab := app.SelectedTab()
if tab == nil {
return errors.New("No selected tab")
}
@@ -129,7 +129,7 @@ func (Send) Execute(aerc *app.Aerc, args []string) error {
msg+" Abort send? [Y/n] ",
func(text string) {
if text == "n" || text == "N" {
- send(aerc, composer, ctx, header, tabName, archive)
+ send(composer, ctx, header, tabName, archive)
}
}, func(cmd string) ([]string, string) {
if cmd == "" {
@@ -140,21 +140,21 @@ func (Send) Execute(aerc *app.Aerc, args []string) error {
},
)
- aerc.PushPrompt(prompt)
+ app.PushPrompt(prompt)
} else {
- send(aerc, composer, ctx, header, tabName, archive)
+ send(composer, ctx, header, tabName, archive)
}
return nil
}
-func send(aerc *app.Aerc, composer *app.Composer, ctx sendCtx,
+func send(composer *app.Composer, ctx sendCtx,
header *mail.Header, tabName string, archive string,
) {
// we don't want to block the UI thread while we are sending
// so we do everything in a goroutine and hide the composer from the user
- aerc.RemoveTab(composer, false)
- aerc.PushStatus("Sending...", 10*time.Second)
+ app.RemoveTab(composer, false)
+ app.PushStatus("Sending...", 10*time.Second)
log.Debugf("send uri: %s", ctx.uri.String())
// enter no-quit mode
@@ -211,12 +211,12 @@ func send(aerc *app.Aerc, composer *app.Composer, ctx sendCtx,
err := <-failCh
if err != nil {
- aerc.PushError(strings.ReplaceAll(err.Error(), "\n", " "))
- aerc.NewTab(composer, tabName)
+ app.PushError(strings.ReplaceAll(err.Error(), "\n", " "))
+ app.NewTab(composer, tabName)
return
}
if config.CopyTo != "" && ctx.scheme != "jmap" {
- aerc.PushStatus("Copying to "+config.CopyTo, 10*time.Second)
+ app.PushStatus("Copying to "+config.CopyTo, 10*time.Second)
errch := copyToSent(composer.Worker(), config.CopyTo,
copyBuf.Len(), &copyBuf)
err = <-errch
@@ -224,13 +224,13 @@ func send(aerc *app.Aerc, composer *app.Composer, ctx sendCtx,
errmsg := fmt.Sprintf(
"message sent, but copying to %v failed: %v",
config.CopyTo, err.Error())
- aerc.PushError(errmsg)
+ app.PushError(errmsg)
composer.SetSent(archive)
composer.Close()
return
}
}
- aerc.PushStatus("Message sent.", 10*time.Second)
+ app.PushStatus("Message sent.", 10*time.Second)
composer.SetSent(archive)
composer.Close()
}()
diff --git a/commands/compose/sign.go b/commands/compose/sign.go
index f44c33ec..ae7c3417 100644
--- a/commands/compose/sign.go
+++ b/commands/compose/sign.go
@@ -17,16 +17,16 @@ func (Sign) Aliases() []string {
return []string{"sign"}
}
-func (Sign) Complete(aerc *app.Aerc, args []string) []string {
+func (Sign) Complete(args []string) []string {
return nil
}
-func (Sign) Execute(aerc *app.Aerc, args []string) error {
+func (Sign) Execute(args []string) error {
if len(args) != 1 {
return errors.New("Usage: sign")
}
- composer, _ := aerc.SelectedTabContent().(*app.Composer)
+ composer, _ := app.SelectedTabContent().(*app.Composer)
err := composer.SetSign(!composer.Sign())
if err != nil {
@@ -41,7 +41,7 @@ func (Sign) Execute(aerc *app.Aerc, args []string) error {
statusline = "Message will not be signed."
}
- aerc.PushStatus(statusline, 10*time.Second)
+ app.PushStatus(statusline, 10*time.Second)
return nil
}
diff --git a/commands/compose/switch.go b/commands/compose/switch.go
index f442d6b0..1cc388f1 100644
--- a/commands/compose/switch.go
+++ b/commands/compose/switch.go
@@ -22,11 +22,11 @@ func (SwitchAccount) Aliases() []string {
return []string{"switch-account"}
}
-func (SwitchAccount) Complete(aerc *app.Aerc, args []string) []string {
- return aerc.AccountNames()
+func (SwitchAccount) Complete(args []string) []string {
+ return app.AccountNames()
}
-func (SwitchAccount) Execute(aerc *app.Aerc, args []string) error {
+func (SwitchAccount) Execute(args []string) error {
opts, optind, err := getopt.Getopts(args, "np")
if err != nil {
return err
@@ -46,13 +46,13 @@ func (SwitchAccount) Execute(aerc *app.Aerc, args []string) error {
// NOT ((prev || next) XOR (len(posargs) == 1))
if (prev || next) == (len(posargs) == 1) {
name := ""
- if acct := aerc.SelectedAccount(); acct != nil {
+ if acct := app.SelectedAccount(); acct != nil {
name = fmt.Sprintf("Current account: %s. ", acct.Name())
}
return errors.New(name + "Usage: switch-account [-np] <account-name>")
}
- switcher, ok := aerc.SelectedTabContent().(AccountSwitcher)
+ switcher, ok := app.SelectedTabContent().(AccountSwitcher)
if !ok {
return errors.New("this tab cannot switch accounts")
}
@@ -61,11 +61,11 @@ func (SwitchAccount) Execute(aerc *app.Aerc, args []string) error {
switch {
case prev:
- acct, err = aerc.PrevAccount()
+ acct, err = app.PrevAccount()
case next:
- acct, err = aerc.NextAccount()
+ acct, err = app.NextAccount()
default:
- acct, err = aerc.Account(posargs[0])
+ acct, err = app.Account(posargs[0])
}
if err != nil {
return err