From 300021cc93c32956342107b754b5406d5a030413 Mon Sep 17 00:00:00 2001 From: Vitaly Ovchinnikov Date: Fri, 7 Jul 2023 10:19:00 +0300 Subject: open: add option -d to automatically delete temporary files By default `:open` leaves its temporary files in the temp directory. The patch adds an option `-d` that defers the deletion of the temporary file when the opener is started. This works well with "sync" openers that don't exit until the user is done with the preview, but may not work with "async" openers that pass the file to their parent process and exit. That's why the automatic deletion needs to be intentionally enabled by using the option. Suggested-by: Robin Jarry Signed-off-by: Vitaly Ovchinnikov Reviewed-by: Moritz Poldrack --- commands/msgview/open.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'commands/msgview/open.go') diff --git a/commands/msgview/open.go b/commands/msgview/open.go index 9ceccb5a..b66456cc 100644 --- a/commands/msgview/open.go +++ b/commands/msgview/open.go @@ -7,6 +7,8 @@ import ( "os" "path/filepath" + "git.sr.ht/~sircmpwn/getopt" + "git.sr.ht/~rjarry/aerc/lib" "git.sr.ht/~rjarry/aerc/log" "git.sr.ht/~rjarry/aerc/widgets" @@ -18,6 +20,10 @@ func init() { register(Open{}) } +func (Open) Options() string { + return "d" +} + func (Open) Aliases() []string { return []string{"open"} } @@ -26,7 +32,20 @@ func (Open) Complete(aerc *widgets.Aerc, args []string) []string { return nil } -func (Open) Execute(aerc *widgets.Aerc, args []string) error { +func (o Open) Execute(aerc *widgets.Aerc, args []string) error { + opts, optind, err := getopt.Getopts(args, o.Options()) + if err != nil { + return err + } + + del := false + + for _, opt := range opts { + if opt.Option == 'd' { + del = true + } + } + mv := aerc.SelectedTabContent().(*widgets.MessageViewer) if mv == nil { return errors.New("open only supported selected message parts") @@ -65,7 +84,10 @@ func (Open) Execute(aerc *widgets.Aerc, args []string) error { go func() { defer log.PanicHandler() - err = lib.XDGOpenMime(tmpFile.Name(), mimeType, args[1:]) + if del { + defer os.Remove(tmpFile.Name()) + } + err = lib.XDGOpenMime(tmpFile.Name(), mimeType, args[optind:]) if err != nil { aerc.PushError("open: " + err.Error()) } -- cgit