2019-03-30 16:58:24 +01:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc2/widgets"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
2019-03-31 21:13:47 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
2019-03-30 16:58:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
2019-03-31 18:17:57 +02:00
|
|
|
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, runewidth.Truncate(name, 32, "…"))
|
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 {
|
|
|
|
// TODO: Tab-specific status stacks
|
|
|
|
aerc.PushStatus("Process complete, press any key to close.",
|
|
|
|
10*time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|