aboutsummaryrefslogtreecommitdiffstats
path: root/commands/msgview/open-link.go
blob: ad2a7cc28fe4e730456abd8a253cd39f6cbe0a4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package msgview

import (
	"fmt"
	"net/url"

	"git.sr.ht/~rjarry/aerc/app"
	"git.sr.ht/~rjarry/aerc/commands"
	"git.sr.ht/~rjarry/aerc/lib"
	"git.sr.ht/~rjarry/aerc/log"
)

type OpenLink struct {
	Url *url.URL `opt:"url" action:"ParseUrl"`
	Cmd []string `opt:"..." required:"false"`
}

func init() {
	register(OpenLink{})
}

func (OpenLink) Aliases() []string {
	return []string{"open-link"}
}

func (OpenLink) Complete(args []string) []string {
	mv := app.SelectedTabContent().(*app.MessageViewer)
	if mv != nil {
		if p := mv.SelectedMessagePart(); p != nil {
			return commands.CompletionFromList(p.Links, args)
		}
	}
	return nil
}

func (o *OpenLink) ParseUrl(arg string) error {
	u, err := url.Parse(arg)
	if err != nil {
		return err
	}
	o.Url = u
	return nil
}

func (o OpenLink) Execute(args []string) error {
	mime := fmt.Sprintf("x-scheme-handler/%s", o.Url.Scheme)
	go func() {
		defer log.PanicHandler()
		if err := lib.XDGOpenMime(o.Url.String(), mime, o.Cmd); err != nil {
			app.PushError("open-link: " + err.Error())
		}
	}()
	return nil
}