From 076d5c2f1aab9e192f98fbfa90aca3e2162fbd71 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Fri, 31 Jul 2020 17:53:22 -0400 Subject: [PATCH] fix ui handlers to write before first event --- ui/event_handlers.go | 73 +++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/ui/event_handlers.go b/ui/event_handlers.go index 8c94ec99f9c..32f726e4ae3 100644 --- a/ui/event_handlers.go +++ b/ui/event_handlers.go @@ -47,24 +47,29 @@ func FetchImageHandler(ctx context.Context, fr *frame.Frame, event partybus.Even if err != nil { return err } - wg.Add(1) + formatter, spinner := startProcess() + stream := progress.Stream(ctx, prog, 150*time.Millisecond) + title := tileFormat.Sprint("Fetching image...") + + formatFn := func(p progress.Progress) { + progStr, err := formatter.Format(p) + spin := color.Magenta.Sprint(spinner.Next()) + if err != nil { + _, _ = io.WriteString(line, fmt.Sprintf("Error: %+v", err)) + } else { + auxInfo := auxInfoFormat.Sprintf("[%s]", prog.Stage()) + _, _ = io.WriteString(line, fmt.Sprintf(statusTitleTemplate+"%s %s", spin, title, progStr, auxInfo)) + } + } + go func() { defer wg.Done() - formatter, spinner := startProcess() - stream := progress.Stream(ctx, prog, 150*time.Millisecond) - title := tileFormat.Sprint("Fetching image...") + formatFn(progress.Progress{}) for p := range stream { - progStr, err := formatter.Format(p) - spin := color.Magenta.Sprint(spinner.Next()) - if err != nil { - _, _ = io.WriteString(line, fmt.Sprintf("Error: %+v", err)) - } else { - auxInfo := auxInfoFormat.Sprintf("[%s]", prog.Stage()) - _, _ = io.WriteString(line, fmt.Sprintf(statusTitleTemplate+"%s %s", spin, title, progStr, auxInfo)) - } + formatFn(p) } spin := color.Green.Sprint(completedStatus) @@ -87,20 +92,26 @@ func ReadImageHandler(ctx context.Context, fr *frame.Frame, event partybus.Event wg.Add(1) + formatter, spinner := startProcess() + stream := progress.Stream(ctx, prog, 150*time.Millisecond) + title := tileFormat.Sprint("Reading image...") + + formatFn := func(p progress.Progress) { + progStr, err := formatter.Format(p) + spin := color.Magenta.Sprint(spinner.Next()) + if err != nil { + _, _ = io.WriteString(line, fmt.Sprintf("Error: %+v", err)) + } else { + _, _ = io.WriteString(line, fmt.Sprintf(statusTitleTemplate+"%s", spin, title, progStr)) + } + } + go func() { defer wg.Done() - formatter, spinner := startProcess() - stream := progress.Stream(ctx, prog, 150*time.Millisecond) - title := tileFormat.Sprint("Reading image...") + formatFn(progress.Progress{}) for p := range stream { - progStr, err := formatter.Format(p) - spin := color.Magenta.Sprint(spinner.Next()) - if err != nil { - _, _ = io.WriteString(line, fmt.Sprintf("Error: %+v", err)) - } else { - _, _ = io.WriteString(line, fmt.Sprintf(statusTitleTemplate+"%s", spin, title, progStr)) - } + formatFn(p) } spin := color.Green.Sprint(completedStatus) @@ -124,16 +135,22 @@ func CatalogerStartedHandler(ctx context.Context, fr *frame.Frame, event partybu wg.Add(1) + _, spinner := startProcess() + stream := progress.StreamMonitors(ctx, []progress.Monitorable{monitor.FilesProcessed, monitor.PackagesDiscovered}, 50*time.Millisecond) + title := tileFormat.Sprint("Cataloging image...") + + formatFn := func(p int64) { + spin := color.Magenta.Sprint(spinner.Next()) + auxInfo := auxInfoFormat.Sprintf("[packages %d]", p) + _, _ = io.WriteString(line, fmt.Sprintf(statusTitleTemplate+"%s", spin, title, auxInfo)) + } + go func() { defer wg.Done() - _, spinner := startProcess() - stream := progress.StreamMonitors(ctx, []progress.Monitorable{monitor.FilesProcessed, monitor.PackagesDiscovered}, 50*time.Millisecond) - title := tileFormat.Sprint("Cataloging image...") + formatFn(0) for p := range stream { - spin := color.Magenta.Sprint(spinner.Next()) - auxInfo := auxInfoFormat.Sprintf("[packages %d]", p[1]) - _, _ = io.WriteString(line, fmt.Sprintf(statusTitleTemplate+"%s", spin, title, auxInfo)) + formatFn(p[1]) } spin := color.Green.Sprint(completedStatus)