Allow open to be asynchronous

This stops the ui being blocked while the resource is opened. The wait
ensures that resources are reclaimed when the process finishes while
aerc is still running.
This commit is contained in:
Andrew Jeffery 2020-07-06 20:14:15 +01:00 committed by Reto Brunner
parent 3e6189f243
commit fda3f43e7c
4 changed files with 29 additions and 8 deletions

View file

@ -115,6 +115,6 @@ func unsubscribeMailto(aerc *widgets.Aerc, u *url.URL) error {
} }
func unsubscribeHTTP(u *url.URL) error { func unsubscribeHTTP(u *url.URL) error {
go lib.OpenFile(u.String()) lib.OpenFile(u.String(), nil)
return nil return nil
} }

View file

@ -60,10 +60,9 @@ func (Open) Execute(aerc *widgets.Aerc, args []string) error {
return return
} }
err = lib.OpenFile(tmpFile.Name()) lib.OpenFile(tmpFile.Name(), func(err error) {
if err != nil {
aerc.PushError(" " + err.Error()) aerc.PushError(" " + err.Error())
} })
aerc.PushStatus("Opened", 10*time.Second) aerc.PushStatus("Opened", 10*time.Second)
}) })

View file

@ -6,7 +6,18 @@ import (
"os/exec" "os/exec"
) )
func OpenFile(filename string) error { func OpenFile(filename string, onErr func(error)) {
cmd := exec.Command("xdg-open", filename) cmd := exec.Command("xdg-open", filename)
return cmd.Run() err := cmd.Start()
if err != nil && onErr != nil {
onErr(err)
return
}
go func() {
err := cmd.Wait()
if err != nil && onErr != nil {
onErr(err)
}
}()
} }

View file

@ -4,7 +4,18 @@ import (
"os/exec" "os/exec"
) )
func OpenFile(filename string) error { func OpenFile(filename string, onErr func(error)) {
cmd := exec.Command("open", filename) cmd := exec.Command("open", filename)
return cmd.Run() err := cmd.Start()
if err != nil && onErr != nil {
onErr(err)
return
}
go func() {
err := cmd.Wait()
if err != nil && onErr != nil {
onErr(err)
}
}()
} }