Skip to content

Commit

Permalink
Bubbletea initialization error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Mar 25, 2024
1 parent cf1573f commit b82832b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pkg/output/logging/context.go
Expand Up @@ -132,7 +132,7 @@ func createDefaultLogger(ctx context.Context) *zap.Logger {
zapcore.AddSync(errout),
lvl,
))
if !term.IsTerminal(errout) {
if !term.IsFancy(errout) {
ec = zap.NewProductionEncoderConfig()
logger = zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(ec),
Expand Down
14 changes: 10 additions & 4 deletions pkg/output/term/io.go
Expand Up @@ -30,11 +30,17 @@ func IsReaderTerminal(r io.Reader) bool {
return ok && term.IsTerminal(int(f.Fd()))
}

// IsTerminal returns true if the given writer is a real terminal.
func IsTerminal(w io.Writer) bool {
// IsWriterTerminal returns true if the given writer is a real terminal.
func IsWriterTerminal(w io.Writer) bool {
f, ok := w.(*os.File)
return ok && term.IsTerminal(int(f.Fd()))
}

// IsFancy returns true if the given writer is a real terminal, or the color
// is forced by the environment variables.
func IsFancy(w io.Writer) bool {
if environment.ColorIsForced() {
return true
}
f, ok := w.(*os.File)
return ok && environment.NonColorRequested() && term.IsTerminal(int(f.Fd()))
return !environment.NonColorRequested() && IsWriterTerminal(w)
}
15 changes: 11 additions & 4 deletions pkg/output/tui/progress.go
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"go.uber.org/multierr"
"knative.dev/client-pkg/pkg/output"
"knative.dev/client-pkg/pkg/output/term"
)
Expand Down Expand Up @@ -79,12 +80,16 @@ type BubbleProgress struct {
prevSpeed []int
err error
quitChan chan struct{}
teaErr error
}

func (b *BubbleProgress) With(fn func(ProgressControl) error) error {
b.start()
defer b.stop()
return fn(b)
err := func() error {
defer b.stop()
return fn(b)
}()
return multierr.Combine(err, b.teaErr)
}

func (b *BubbleProgress) Error(err error) {
Expand Down Expand Up @@ -244,9 +249,11 @@ func (b *BubbleProgress) start() {
b.quitChan = make(chan struct{})
go func() {
t := b.tea
_, _ = t.Run()
if _, err := t.Run(); err != nil {
b.teaErr = err
}
close(b.quitChan)
if term.IsTerminal(out) {
if term.IsWriterTerminal(out) {
if err := t.ReleaseTerminal(); err != nil {
panic(err)
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/output/tui/progress_test.go
Expand Up @@ -33,9 +33,7 @@ import (
)

func TestProgress(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
testing.Short()
ctx := context.TestContext(t)
prt := output.NewTestPrinter()
ctx = output.WithContext(ctx, prt)
Expand Down
15 changes: 11 additions & 4 deletions pkg/output/tui/spinner.go
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"go.uber.org/multierr"
"knative.dev/client-pkg/pkg/output"
"knative.dev/client-pkg/pkg/output/term"
)
Expand All @@ -46,12 +47,16 @@ type BubbleSpinner struct {
spin spinner.Model
tea *tea.Program
quitChan chan struct{}
teaErr error
}

func (b *BubbleSpinner) With(fn func(Spinner) error) error {
b.start()
defer b.stop()
return fn(b)
err := func() error {
defer b.stop()
return fn(b)
}()
return multierr.Combine(err, b.teaErr)
}

func (b *BubbleSpinner) Init() tea.Cmd {
Expand Down Expand Up @@ -81,9 +86,11 @@ func (b *BubbleSpinner) start() {
b.quitChan = make(chan struct{})
go func() {
t := b.tea
_, _ = t.Run()
if _, err := t.Run(); err != nil {
b.teaErr = err
}
close(b.quitChan)
if term.IsTerminal(out) {
if term.IsWriterTerminal(out) {
_ = t.ReleaseTerminal()
}
}()
Expand Down
3 changes: 0 additions & 3 deletions pkg/output/tui/spinner_test.go
Expand Up @@ -26,9 +26,6 @@ import (
)

func TestSpinner(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
ctx := context.TestContext(t)
prt := output.NewTestPrinter()
ctx = output.WithContext(ctx, prt)
Expand Down

0 comments on commit b82832b

Please sign in to comment.