1
0
mirror of https://github.com/go-task/task.git synced 2025-11-23 22:24:45 +02:00

refactor: improve code of group output

This commit is contained in:
Andrey Nering
2025-11-11 14:37:16 -03:00
parent 39c86992bd
commit 43074c20f2

View File

@@ -24,7 +24,6 @@ func (g Group) WrapWriter(stdOut, _ io.Writer, _ string, cache *templater.Cache)
if g.ErrorOnly && err == nil { if g.ErrorOnly && err == nil {
return nil return nil
} }
return gw.close() return gw.close()
} }
} }
@@ -40,19 +39,22 @@ func (gw *groupWriter) Write(p []byte) (int, error) {
} }
func (gw *groupWriter) close() error { func (gw *groupWriter) close() error {
if gw.buff.Len() == 0 { switch {
// don't print begin/end messages if there's no buffered entries case gw.buff.Len() == 0:
return nil return nil
} case gw.begin == "" && gw.end == "":
if len(gw.begin) > 0 {
// Rewrite the gw.buff with the beginning text.
s := gw.buff.String()
gw.buff.Reset()
gw.buff.WriteString(gw.begin)
gw.buff.WriteString(s)
}
gw.buff.WriteString(gw.end)
// Return the entire gw.buff to ensure the group is written atomically to stdout.
_, err := io.Copy(gw.writer, &gw.buff) _, err := io.Copy(gw.writer, &gw.buff)
return err return err
default:
_, err := io.Copy(gw.writer, gw.combinedBuff())
return err
}
}
func (gw *groupWriter) combinedBuff() io.Reader {
var b bytes.Buffer
_, _ = b.WriteString(gw.begin)
_, _ = io.Copy(&b, &gw.buff)
_, _ = b.WriteString(gw.end)
return &b
} }