From d451b66bae1f024b1da287e41d73f9182e52378f Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Tue, 2 Apr 2019 16:41:07 -0700 Subject: [PATCH] progressui: improve plain progress Signed-off-by: Tonis Tiigi --- util/progress/progressui/display.go | 22 +++++------- util/progress/progressui/printer.go | 56 ++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/util/progress/progressui/display.go b/util/progress/progressui/display.go index d456b025..6ff97af4 100644 --- a/util/progress/progressui/display.go +++ b/util/progress/progressui/display.go @@ -175,34 +175,30 @@ func (t *trace) triggerVertexEvent(v *client.Vertex) { old = *v } - var ev []string + changed := false if v.Digest != old.Digest { - ev = append(ev, fmt.Sprintf("%13s %s", "digest:", v.Digest)) + changed = true } if v.Name != old.Name { - ev = append(ev, fmt.Sprintf("%13s %q", "name:", v.Name)) + changed = true } if v.Started != old.Started { if v.Started != nil && old.Started == nil || !v.Started.Equal(*old.Started) { - ev = append(ev, fmt.Sprintf("%13s %v", "started:", v.Started)) + changed = true } } if v.Completed != old.Completed && v.Completed != nil { - ev = append(ev, fmt.Sprintf("%13s %v", "completed:", v.Completed)) - if v.Started != nil { - ev = append(ev, fmt.Sprintf("%13s %v", "duration:", v.Completed.Sub(*v.Started))) - } + changed = true } if v.Cached != old.Cached { - ev = append(ev, fmt.Sprintf("%13s %v", "cached:", v.Cached)) + changed = true } if v.Error != old.Error { - ev = append(ev, fmt.Sprintf("%13s %q", "error:", v.Error)) + changed = true } - if len(ev) > 0 { - vtx.events = append(vtx.events, ev...) - vtx.update(len(ev)) + if changed { + vtx.update(1) t.updates[v.Digest] = struct{}{} } diff --git a/util/progress/progressui/printer.go b/util/progress/progressui/printer.go index 3794105a..20bc4d90 100644 --- a/util/progress/progressui/printer.go +++ b/util/progress/progressui/printer.go @@ -1,8 +1,11 @@ package progressui import ( + "context" "fmt" "io" + "sort" + "strings" "time" digest "github.com/opencontainers/go-digest" @@ -20,9 +23,10 @@ type lastStatus struct { } type textMux struct { - w io.Writer - current digest.Digest - last map[string]lastStatus + w io.Writer + current digest.Digest + last map[string]lastStatus + notFirst bool } func (p *textMux) printVtx(t *trace, dgst digest.Digest) { @@ -43,10 +47,16 @@ func (p *textMux) printVtx(t *trace, dgst digest.Digest) { } old.logsOffset = 0 old.count = 0 - fmt.Fprintf(p.w, "#%d ...\n", v.index) + fmt.Fprintf(p.w, "#%d ...\n", old.index) } - fmt.Fprintf(p.w, "\n#%d %s\n", v.index, limitString(v.Name, 72)) + if p.notFirst { + fmt.Fprintln(p.w, "") + } else { + p.notFirst = true + } + + fmt.Fprintf(p.w, "#%d %s\n", v.index, limitString(v.Name, 72)) } if len(v.events) != 0 { @@ -127,18 +137,46 @@ func (p *textMux) printVtx(t *trace, dgst digest.Digest) { } p.current = dgst - if v.Completed != nil { p.current = "" v.count = 0 - fmt.Fprintf(p.w, "\n") + + if v.Error != "" { + if v.logsPartial { + fmt.Fprintln(p.w, "") + } + if strings.HasSuffix(v.Error, context.Canceled.Error()) { + fmt.Fprintf(p.w, "#%d CANCELED\n", v.index) + } else { + fmt.Fprintf(p.w, "#%d ERROR: %s\n", v.index, v.Error) + } + } else if v.Cached { + fmt.Fprintf(p.w, "#%d CACHED\n", 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) + } + } delete(t.updates, dgst) } -func (p *textMux) print(t *trace) { +func sortCompleted(t *trace, m map[digest.Digest]struct{}) []digest.Digest { + out := make([]digest.Digest, 0, len(m)) + for k := range m { + out = append(out, k) + } + sort.Slice(out, func(i, j int) bool { + return t.byDigest[out[i]].Completed.Before(*t.byDigest[out[j]].Completed) + }) + return out +} +func (p *textMux) print(t *trace) { completed := map[digest.Digest]struct{}{} rest := map[digest.Digest]struct{}{} @@ -161,7 +199,7 @@ func (p *textMux) print(t *trace) { p.printVtx(t, current) } - for dgst := range completed { + for _, dgst := range sortCompleted(t, completed) { if dgst != current { p.printVtx(t, dgst) }