Skip to content

Commit

Permalink
progress: add new Clear() method to the progressBar
Browse files Browse the repository at this point in the history
This commit adds a new `Clear()` method on the progressBar so
that we can clear subprogress easily. This is useful when switching
from image building to image uploading, here we need a single
progress again.
  • Loading branch information
mvo5 committed Dec 18, 2024
1 parent b93521a commit 2083ecc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
40 changes: 35 additions & 5 deletions bib/internal/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"strings"
"sync"
"time"

"github.com/cheggaaa/pb/v3"
Expand Down Expand Up @@ -39,10 +40,6 @@ type ProgressBar interface {
// SetProgress sets the progress details at the given "level".
// Levels should start with "0" and increase as the nesting
// gets deeper.
//
// Note that reducing depth is currently not supported, once
// a sub-progress is added it cannot be removed/hidden
// (but if required it can be added, its a SMOP)
SetProgress(level int, msg string, done int, total int) error

// The high-level message that is displayed in a spinner
Expand All @@ -58,6 +55,10 @@ type ProgressBar interface {
// like "Starting module org.osbuild.selinux"
SetMessagef(fmt string, args ...interface{})

// Clear clears all the subprogress/pulse/messages. This is
// useful when there is a need to reduce the sublevels.
Clear()

// Start will start rendering the progress information
Start()

Expand Down Expand Up @@ -89,7 +90,8 @@ type terminalProgressBar struct {

shutdownCh chan bool

out io.Writer
outMu sync.Mutex
out io.Writer
}

// NewTerminalProgressBar creates a new default pb3 based progressbar suitable for
Expand Down Expand Up @@ -149,7 +151,29 @@ func (b *terminalProgressBar) SetMessagef(msg string, args ...interface{}) {
b.msgPb.Set("msg", shorten(fmt.Sprintf(msg, args...)))
}

func (b *terminalProgressBar) Clear() {
// ensure anything pending is output
b.render()

b.outMu.Lock()
defer b.outMu.Unlock()

// pulseMsg line + subLevel lines + message line
linesToClear := len(b.subLevelPbs) + 2
for i := 0; i < linesToClear; i++ {
fmt.Fprintf(b.out, "%s\n", ERASE_LINE)
}
fmt.Fprint(b.out, cursorUp(linesToClear))

b.subLevelPbs = nil
b.SetPulseMsgf("")
b.SetMessagef("")
}

func (b *terminalProgressBar) render() {
b.outMu.Lock()
defer b.outMu.Unlock()

var renderedLines int
fmt.Fprintf(b.out, "%s%s\n", ERASE_LINE, b.spinnerPb.String())
renderedLines++
Expand Down Expand Up @@ -262,6 +286,9 @@ func (b *plainProgressBar) SetProgress(subLevel int, msg string, done int, total
return nil
}

func (b *plainProgressBar) Clear() {
}

type debugProgressBar struct {
w io.Writer
}
Expand Down Expand Up @@ -300,6 +327,9 @@ func (b *debugProgressBar) SetProgress(subLevel int, msg string, done int, total
fmt.Fprintf(b.w, "\n")
return nil
}
func (b *debugProgressBar) Clear() {
fmt.Fprintf(b.w, "Clear progressbar\n")
}

// XXX: merge variant back into images/pkg/osbuild/osbuild-exec.go
func RunOSBuild(pb ProgressBar, manifest []byte, store, outputDirectory string, exports, extraEnv []string) error {
Expand Down
9 changes: 9 additions & 0 deletions bib/internal/progress/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func TestPlainProgress(t *testing.T) {
assert.Equal(t, "", buf.String())
pbar.Stop()
assert.Equal(t, "", buf.String())
pbar.Clear()
assert.Equal(t, "", buf.String())
}

func TestDebugProgress(t *testing.T) {
Expand Down Expand Up @@ -87,6 +89,10 @@ func TestDebugProgress(t *testing.T) {
pbar.Stop()
assert.Equal(t, "Stop progressbar\n", buf.String())
buf.Reset()

pbar.Clear()
assert.Equal(t, "Clear progressbar\n", buf.String())
buf.Reset()
}

func TestTermProgress(t *testing.T) {
Expand All @@ -102,12 +108,15 @@ func TestTermProgress(t *testing.T) {
pbar.SetMessagef("some-message")
err = pbar.SetProgress(0, "set-progress-msg", 0, 5)
assert.NoError(t, err)
pbar.Clear()
pbar.Stop()
assert.NoError(t, pbar.(*progress.TerminalProgressBar).Err())

assert.Contains(t, buf.String(), "[1 / 6] set-progress-msg")
assert.Contains(t, buf.String(), "[|] pulse-msg\n")
assert.Contains(t, buf.String(), "Message: some-message\n")
// check clear (clear 3 lines)
assert.Contains(t, buf.String(), "\x1b[2K\n\x1b[2K\n\x1b[2K\n")
// check shutdown
assert.Contains(t, buf.String(), progress.CURSOR_SHOW)
}

0 comments on commit 2083ecc

Please sign in to comment.