diff options
author | Robin Jarry <robin@jarry.cc> | 2023-10-10 00:08:31 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-10-10 11:38:01 +0200 |
commit | bc176bd61ba726351a489cabf4da16a47dc5ec3b (patch) | |
tree | bbf06f731592d072f3d6f76f1648d61989375f2e /commands/compose/attach.go | |
parent | 598e4a5803578ab3e291f232d6aad31b4efd8ea4 (diff) | |
download | aerc-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/attach.go')
-rw-r--r-- | commands/compose/attach.go | 38 |
1 files changed, 19 insertions, 19 deletions
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 } |