1
0
Fork 0
mirror of https://git.sr.ht/~rjarry/aerc synced 2025-02-22 23:23:57 +01:00
aerc/commands/msgview/open-link.go
Robin Jarry 26033eaecf completion: add commands descriptions
Update the Command interface to include a Description() method.
Implement the method for all commands using short descriptions inspired
from the aerc(1) man page.

Return the description values along with command names so that they can
be displayed in completion choices.

Implements: https://todo.sr.ht/~rjarry/aerc/271
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Bojan Gabric <bojan@bojangabric.com>
Tested-by: Jason Cox <me@jasoncarloscox.com>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
2024-10-23 10:22:51 +02:00

62 lines
1.2 KiB
Go

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/lib/log"
)
type OpenLink struct {
Url *url.URL `opt:"url" action:"ParseUrl" complete:"CompleteUrl"`
Cmd string `opt:"..." required:"false"`
}
func init() {
commands.Register(OpenLink{})
}
func (OpenLink) Description() string {
return "Open the specified URL with an external program."
}
func (OpenLink) Context() commands.CommandContext {
return commands.MESSAGE_VIEWER
}
func (OpenLink) Aliases() []string {
return []string{"open-link"}
}
func (*OpenLink) CompleteUrl(arg string) []string {
mv := app.SelectedTabContent().(*app.MessageViewer)
if mv != nil {
if p := mv.SelectedMessagePart(); p != nil {
return commands.FilterList(p.Links, arg, nil)
}
}
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
}