postpone: avoid calling WriteMessage twice

Postpone will currently call composer.WriteMessage twice: once for
counting the bytes and another time for appending the message.

Signed-off-by: Koni Marti <koni.marti@gmail.com>
Acked-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
Koni Marti 2022-07-05 21:42:42 +02:00 committed by Robin Jarry
parent 4821425933
commit 99b74dbcc9

View file

@ -1,8 +1,7 @@
package compose package compose
import ( import (
"io" "bytes"
"io/ioutil"
"time" "time"
"github.com/miolini/datacounter" "github.com/miolini/datacounter"
@ -82,33 +81,29 @@ func (Postpone) Execute(aerc *widgets.Aerc, args []string) error {
} }
aerc.RemoveTab(composer) aerc.RemoveTab(composer)
ctr := datacounter.NewWriterCounter(ioutil.Discard) var buf bytes.Buffer
ctr := datacounter.NewWriterCounter(&buf)
err = composer.WriteMessage(header, ctr) err = composer.WriteMessage(header, ctr)
if err != nil { if err != nil {
handleErr(errors.Wrap(err, "WriteMessage")) handleErr(errors.Wrap(err, "WriteMessage"))
return return
} }
nbytes := int(ctr.Count()) nbytes := int(ctr.Count())
r, w := io.Pipe()
worker.PostAction(&types.AppendMessage{ worker.PostAction(&types.AppendMessage{
Destination: config.Postpone, Destination: config.Postpone,
Flags: []models.Flag{models.SeenFlag}, Flags: []models.Flag{models.SeenFlag},
Date: time.Now(), Date: time.Now(),
Reader: r, Reader: &buf,
Length: int(nbytes), Length: int(nbytes),
}, func(msg types.WorkerMessage) { }, func(msg types.WorkerMessage) {
switch msg := msg.(type) { switch msg := msg.(type) {
case *types.Done: case *types.Done:
aerc.PushStatus("Message postponed.", 10*time.Second) aerc.PushStatus("Message postponed.", 10*time.Second)
r.Close()
composer.Close() composer.Close()
case *types.Error: case *types.Error:
r.Close()
handleErr(msg.Error) handleErr(msg.Error)
} }
}) })
composer.WriteMessage(header, w)
w.Close()
}() }()
if !alreadyCreated { if !alreadyCreated {