aerc/commands/account/pipe.go

67 lines
1.5 KiB
Go
Raw Normal View History

2019-03-30 16:58:24 +01:00
package account
import (
"errors"
"io"
"os/exec"
"time"
2019-05-18 02:57:10 +02:00
"git.sr.ht/~sircmpwn/aerc/widgets"
2019-03-30 16:58:24 +01:00
"github.com/gdamore/tcell"
)
func init() {
register("pipe", Pipe)
}
func Pipe(aerc *widgets.Aerc, args []string) error {
if len(args) < 2 {
return errors.New("Usage: :pipe <cmd> [args...]")
}
acct := aerc.SelectedAccount()
store := acct.Messages().Store()
msg := acct.Messages().Selected()
store.FetchFull([]uint32{msg.Uid}, func(reader io.Reader) {
2019-03-30 16:58:24 +01:00
cmd := exec.Command(args[1], args[2:]...)
pipe, err := cmd.StdinPipe()
if err != nil {
aerc.PushStatus(" "+err.Error(), 10*time.Second).
2019-03-30 18:05:00 +01:00
Color(tcell.ColorDefault, tcell.ColorRed)
2019-03-30 16:58:24 +01:00
return
}
term, err := widgets.NewTerminal(cmd)
if err != nil {
aerc.PushStatus(" "+err.Error(), 10*time.Second).
2019-03-30 18:05:00 +01:00
Color(tcell.ColorDefault, tcell.ColorRed)
2019-03-30 16:58:24 +01:00
return
}
2019-03-31 21:13:47 +02:00
name := args[1] + " <" + msg.Envelope.Subject
aerc.NewTab(term, name)
2019-03-30 16:58:24 +01:00
term.OnClose = func(err error) {
if err != nil {
aerc.PushStatus(" "+err.Error(), 10*time.Second).
2019-03-30 18:05:00 +01:00
Color(tcell.ColorDefault, tcell.ColorRed)
2019-03-30 16:58:24 +01:00
} else {
aerc.PushStatus("Process complete, press any key to close.",
10*time.Second)
term.OnEvent = func(event tcell.Event) bool {
aerc.RemoveTab(term)
return true
}
2019-03-30 16:58:24 +01:00
}
}
term.OnStart = func() {
go func() {
_, err := io.Copy(pipe, reader)
if err != nil {
aerc.PushStatus(" "+err.Error(), 10*time.Second).
2019-03-30 18:05:00 +01:00
Color(tcell.ColorDefault, tcell.ColorRed)
2019-03-30 16:58:24 +01:00
}
pipe.Close()
}()
}
})
return nil
}