Skip to content

Commit

Permalink
progress: auto-select progress type on "" based on if there is a pty
Browse files Browse the repository at this point in the history
This commit adds support to auto-select the terminal based on if
it is a pty or not.
  • Loading branch information
mvo5 committed Dec 11, 2024
1 parent ee9e7b9 commit 503f610
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 9 additions & 1 deletion bib/internal/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ type ProgressBar interface {
// New creates a new progressbar based on the requested type
func New(typ string) (ProgressBar, error) {
switch typ {
case "", "plain":
case "":
// auto-select
if f, ok := osStderr.(*os.File); ok {
if isatty.IsTerminal(f.Fd()) {
return NewTermProgressBar()
}
}
return NewPlainProgressBar()
case "plain":
return NewPlainProgressBar()
case "term":
return NewTermProgressBar()
Expand Down
15 changes: 14 additions & 1 deletion bib/internal/progress/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/osbuild/bootc-image-builder/bib/internal/progress"
)
Expand All @@ -24,6 +23,7 @@ func TestProgressNew(t *testing.T) {
{"term", &progress.TermProgressBar{}, ""},
{"debug", &progress.DebugProgressBar{}, ""},
{"plain", &progress.PlainProgressBar{}, ""},
// unknown progress type
{"bad", nil, `unknown progress type: "bad"`},
} {
pb, err := progress.New(tc.typ)
Expand All @@ -36,6 +36,19 @@ func TestProgressNew(t *testing.T) {
}
}

func TestProgressNewAutoSelect(t *testing.T) {
var buf bytes.Buffer
restore := progress.MockOsStderr(&buf)
defer restore()

// XXX: test auto-select terminal case by mocking osStderr via
// terminal
pb, err := progress.New("")
assert.NoError(t, err)
expected := &progress.PlainProgressBar{}
assert.Equal(t, reflect.TypeOf(pb), reflect.TypeOf(expected))
}

func TestPlainProgress(t *testing.T) {
var buf bytes.Buffer
restore := progress.MockOsStderr(&buf)
Expand Down

0 comments on commit 503f610

Please sign in to comment.