pkg/progressui: customization callbacks

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-02-23 15:34:41 -08:00
parent e40983b7b7
commit a9443bc0d2
3 changed files with 137 additions and 72 deletions

View File

@@ -3,8 +3,6 @@ package progressui
import (
"context"
"fmt"
"io"
"os"
"sort"
"strings"
"time"
@@ -24,7 +22,10 @@ type lastStatus struct {
}
type textMux struct {
w io.Writer
vertexPrintCb VertexPrintFunc
statusPrintCb StatusPrintFunc
logPrintCb LogPrintFunc
current digest.Digest
last map[string]lastStatus
notFirst bool
@@ -44,33 +45,27 @@ func (p *textMux) printVtx(t *trace, dgst digest.Digest) {
if p.current != "" {
old := t.byDigest[p.current]
if old.logsPartial {
fmt.Fprintln(p.w, "")
p.statusPrintCb(v.Vertex, "")
}
old.logsOffset = 0
old.count = 0
fmt.Fprintf(p.w, "#%d ...\n", old.index)
p.statusPrintCb(v.Vertex, "#%d ...", old.index)
}
if p.notFirst {
fmt.Fprintln(p.w, "")
p.statusPrintCb(v.Vertex, "")
} else {
p.notFirst = true
}
if os.Getenv("PROGRESS_NO_TRUNC") == "0" {
fmt.Fprintf(p.w, "#%d %s\n", v.index, limitString(v.Name, 72))
} else {
fmt.Fprintf(p.w, "#%d %s\n", v.index, v.Name)
fmt.Fprintf(p.w, "#%d %s\n", v.index, v.Digest)
}
p.vertexPrintCb(v.Vertex, v.index)
}
if len(v.events) != 0 {
v.logsOffset = 0
}
for _, ev := range v.events {
fmt.Fprintf(p.w, "#%d %s\n", v.index, ev)
p.statusPrintCb(v.Vertex, "#%d %s", v.index, ev)
}
v.events = v.events[:0]
@@ -118,25 +113,24 @@ func (p *textMux) printVtx(t *trace, dgst digest.Digest) {
if s.Completed != nil {
tm += " done"
}
fmt.Fprintf(p.w, "#%d %s%s%s\n", v.index, s.ID, bytes, tm)
p.statusPrintCb(v.Vertex, "#%d %s%s%s", v.index, s.ID, bytes, tm)
}
}
v.statusUpdates = map[string]struct{}{}
for i, l := range v.logs {
line := l.line
if i == 0 {
l = l[v.logsOffset:]
}
fmt.Fprintf(p.w, "%s", []byte(l))
if i != len(v.logs)-1 || !v.logsPartial {
fmt.Fprintln(p.w, "")
line = line[v.logsOffset:]
}
complete := i != len(v.logs)-1 || !v.logsPartial
p.logPrintCb(v.Vertex, l.stream, !complete, "%s", line)
}
if len(v.logs) > 0 {
if v.logsPartial {
v.logs = v.logs[len(v.logs)-1:]
v.logsOffset = len(v.logs[0])
v.logsOffset = len(v.logs[0].line)
} else {
v.logs = nil
v.logsOffset = 0
@@ -150,23 +144,22 @@ func (p *textMux) printVtx(t *trace, dgst digest.Digest) {
if v.Error != "" {
if v.logsPartial {
fmt.Fprintln(p.w, "")
p.statusPrintCb(v.Vertex, "")
}
if strings.HasSuffix(v.Error, context.Canceled.Error()) {
fmt.Fprintf(p.w, "#%d CANCELED\n", v.index)
p.statusPrintCb(v.Vertex, "#%d CANCELED", v.index)
} else {
fmt.Fprintf(p.w, "#%d ERROR: %s\n", v.index, v.Error)
p.statusPrintCb(v.Vertex, "#%d ERROR: %s", v.index, v.Error)
}
} else if v.Cached {
fmt.Fprintf(p.w, "#%d CACHED\n", v.index)
p.statusPrintCb(v.Vertex, "#%d CACHED", v.index)
} else {
tm := ""
if v.Started != nil {
tm = fmt.Sprintf(" %.1fs", v.Completed.Sub(*v.Started).Seconds())
}
fmt.Fprintf(p.w, "#%d DONE%s\n", v.index, tm)
p.statusPrintCb(v.Vertex, "#%d DONE%s", v.index, tm)
}
}
delete(t.updates, dgst)