Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

progress: auto-select progress based on $PS1 #755

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions bib/internal/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ type ProgressBar interface {
// New creates a new progressbar based on the requested type
func New(typ string) (ProgressBar, error) {
switch typ {
// XXX: autoseelct based on PS1 value (i.e. use term in
// interactive shells only?)
case "", "plain":
case "":
// autoselect based on if we are on an interactive
// shell, use plain progress for scripts
if _, ok := os.LookupEnv("PS1"); ok {
return NewTerminalProgressBar()
}
return NewPlainProgressBar()
case "plain":
return NewPlainProgressBar()
case "term":
return NewTerminalProgressBar()
Expand Down
21 changes: 21 additions & 0 deletions bib/internal/progress/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"reflect"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -111,3 +112,23 @@ func TestTermProgress(t *testing.T) {
// check shutdown
assert.Contains(t, buf.String(), progress.CURSOR_SHOW)
}

func TestProgressNewAutoselect(t *testing.T) {
for _, tc := range []struct {
env string
expected interface{}
}{
{"", &progress.PlainProgressBar{}},
{"PS1=foo", &progress.TerminalProgressBar{}},
} {

if l := strings.SplitN(tc.env, "=", 2); len(l) == 2 {
t.Setenv(l[0], l[1])
}

pb, err := progress.New("")
assert.NoError(t, err)
assert.Equal(t, reflect.TypeOf(pb), reflect.TypeOf(tc.expected), fmt.Sprintf("[%v] %T not the expected %T", tc.env, pb, tc.expected))
}

}
Loading